Results 1 to 5 of 5

Thread: Sprite rotation, math problem

  1. #1

    Sprite rotation, math problem


    I'm using Andorra2D ( to make a utility, similar to a map editor, for a game. The utility displays several rectangular zones and I want to connect all of these with lines.

    Instead of simply drawing static lines on the canvas, I am trying to use sprites to connect the rectangular sprites. This way I can drag the lines and manipulate them in various ways along with storing variables in the sprite objects.

    I am having trouble figuring out how to determine the rotation angle of the line sprite. Can you give me pointers as to how I can solve this math problem? I have the x/y positions of two rectangles and I can of course figure out the length between the two, but I'm stuck and can't get any further.

    Hopefully someone can help me, I feel stupid for not being able to solve this on my own ops:

  2. #2

    Sprite rotation, math problem

    I recall having a function for that. I've looked it up:

    function Direction2d(x1,y1,x2,y2: single): single;
    Result := radtodeg( arctan2((x2-x1) , (y2-y1)) )

    The use of arctan2 is the key to the sollution of your problem. You must make sure that the second parameter is not zero. So you should add some if and then's to handle those cases.

    The routine returns the angle in degrees. You only have to figure out, in which direction "zero degrees" points.

    Hope to have helped you
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Sprite rotation, math problem

    [size=9px]moved to math section[/size]

  4. #4

    Sprite rotation, math problem

    { Degree/Radians }
    P2D_RadToDeg = 180.0 / PI;
    P2D_DegToRad = PI / 180.0;

    { TP2DVector3s }
    PP2DVector3s = ^TP2DVector3s;
    TP2DVector3s = packed record
    x: Single;
    y: Single;
    z: Single;

    procedure TP2DMath.VectorSub(aSrcVector, aDestVector: PP2DVector3s);
    aSrcVector.x := aSrcVector.x - aDestVector.x;
    aSrcVector.y := aSrcVector.y - aDestVector.y;
    aSrcVector.z := aSrcVector.z - aDestVector.z;

    function TP2DMath.VectorMag(aSrcVector: PP2DVector3s): Single;
    with aSrcVector^ do
    Result := Sqrt((X*X) + (Y*Y));

    procedure TP2DMath.VectorNorm(aSrcVector: PP2DVector3s);
    Len, OOL: Single;
    Len := VectorMag(aSrcVector);

    if Len <> 0 then
    OOL := 1.0 / Len;
    with aSrcVector^ do
    X := X * OOL;
    Y := Y * OOL;

    function TP2DMath.VectorAngle(aSrcVector, aDestVector: PP2DVector3s): Single;
    xoy: Single;
    R : TP2DVector3s;
    R := aSrcVector^;

    VectorSub(@R, aDestVector);


    if R.y = 0 then
    R.y := 0.001;

    xoy := R.x/R.y;

    Result := ArcTan(xoy) * P2D_RadToDeg;
    if R.y < 0 then
    Result := Result + 180.0;

    This code is from Pyrogine2D which I use in many places in the library. VectorAngle will give you the angle in degrees between two points. I use this in the polygon class (keeps track of rotated line segments) and in the Entity class which provides several rotateToXXX routines (rotate to angle, rotate to position and so on). Note it properly handles the situation that chronozphere pointed out. Hope this helps.
    Jarrod Davis
    Technical Director @ Piradyne Games

  5. #5

    Sprite rotation, math problem

    Thanks so much guys, I'll try out your solutions and report back.

    EDIT: chronozphere and Pyrogine, I tried out your suggestions and it works great - thanks again!

    / Senap


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Comodo SSL