Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Moving Objects in direction of an angle

  1. #1

    Moving Objects in direction of an angle

    Hi!

    I want to Move an Object (in 2D) in the direction of a specified angle.
    In the past I used something like this:

    x :=x + Round(Cos256(angle)*Speed);
    y :=y + Round(Sin256(angle)*Speed);

    (Cos/Sin256 are from DelphiX)
    But this allows only an angle from 0 to 255.

    Ho can I realize this for 0 to 360AŹ

  2. #2

    Moving Objects in direction of an angle

    X:=x+cos(Angle);
    Y:=Y+sin(Angle);
    http://www.programuotojas.com

  3. #3

    Moving Objects in direction of an angle

    *g* I have already tried this before, but it doesn't work..
    It leads to weird, random-like results..

  4. #4

    Moving Objects in direction of an angle

    I'm just guessing, but you're probably doing the rounding in a wrong place, keep you're x and y as real numbers (singles preferably) and only use them rounded when drawing, or you're forgetting that the angle is in radians.

  5. #5

    Moving Objects in direction of an angle

    Hm this doesn't fix the problem

    With each increase oder decrease of the angle, the object moves in a totally different direction..

    I know that there are more Cos/Sin function than just Cos()..
    ..maybe Cos256 is based on something different..

  6. #6

    Moving Objects in direction of an angle

    Post yore angle changing code

  7. #7

    Moving Objects in direction of an angle

    ok here we go:

    Code:
    if &#40;links&#41; AND &#40;angle < 360&#41; then angle &#58;= angle +1;
    if &#40;rechts&#41; AND &#40;angle > 0&#41; then angle &#58;= angle -1;
    
    if &#40;links&#41; AND &#40;angle = 360&#41; then angle &#58;= 0;
    if &#40;rechts&#41; AND &#40;angle = 0&#41; then angle &#58;= 360;

    links & rechts are checked in OnKeyDown links is true if VK_LEFT is pressed and rechts if VK_RIGHT..
    (Undone in OnKeyUp)

  8. #8

    Moving Objects in direction of an angle

    I know that there are more Cos/Sin function than just Cos()..
    ..maybe Cos256 is based on something different..
    Nope, checked DelphiX?˘_Ts source, Cos256 simply uses a lookup table precalculated with
    for i:=0 to 255 do
    CosinTable[i] := Cos((i/256)*2*PI);

    As I said the angle is in radians, so changing it by 1 is rotating by a almost 60 degrees, change the angle in much smaller values like one degree (that is pi/180).

  9. #9

    Moving Objects in direction of an angle

    Ah ok thanks, now it is close to a "realistic" movement.

    I'm doing It like this:

    (Angle is increased as done before)

    Code:
    xko &#58;=xko - Cos&#40;360-angle*&#40;pi/180&#41;&#41;*Speed;
    yko &#58;=yko + Sin&#40;360-angle*&#40;pi/180&#41;&#41;*Speed;
    but the movement in a 90AŹ? angle is not totally correct, maybe 10AŹ? more than it should be..

    I have uploaded the thing i am working on here:
    (it is a modified delphiGL example)
    http://www.siedla-design.de/space.zip
    Please have a short look..

  10. #10

    Moving Objects in direction of an angle

    yore not converting 360 degrees to radians in 360-angle*(pi/180), it should be (360-angle)*(pi/180). To say the truth 360 is not needed at all. And keeping the angle variable in radians would be a tiny bit faster than converting from degrees every time you use trigonometry.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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