Results 1 to 7 of 7

Thread: 2D Overhead Pedestrian Direction

  1. #1

    2D Overhead Pedestrian Direction

    Currently my game re-angles the peds to point directly to their destination, however i'd like them to have a max turning angle. i have no idea how todo that. my current code.

    [pascal]
    Player.Direction := ArcTan2( (DestinationY - Player.Y), (DestinationX - Player.X) );
    Speed := 72;
    Player.X := Player.X + cos( Player.Direction ) * (Speed * delta);
    Player.Y := Player.Y + sin( Player.Direction ) * (Speed * delta);
    [/pascal]



    any ideas how i can accomplish this?

    -Colin
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  2. #2

    Re: 2D Overhead Pedestrian Direction

    This topic about bezier splines might be usefull for you.

    http://www.pascalgamedevelopment.com...p?topic=5833.0

    "Maximum angle" is a bit unclear to me. I can see you want to constrain the rotation to some degree because it's unrealistic to turn at once. But do you want a maximum rotation per frame, or per second?

    This is what I would do (for each frame):

    Code:
     Check where the target is with respect to the pedestrian's position using arcTan2().
     If this angle is different from the orientation of the pedestrian?
      Add MaxRotate to the current angle of your pedestrian (or subtract, depending on what is faster to reach the perfect approach angle)
     else
      Leave the angle alone, we are approaching our target in the right direction
    
     Now just move one step with current angle and speed.
    Hope this makes sense to you.


    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #3

    Re: 2D Overhead Pedestrian Direction

    hi, thanks,

    so i am trying as you said, but fail miserably, been trying to solve this for hoursssss lol

    so anyways i did not add check to see which way to turn is faster as for now more important to get it to work.

    [pascal]
    newDir := ArcTan2( (DestinationY - Sprite.Y), (DestinationX - Sprite.X) );

    if Sprite.Direction <> newDir then begin
    //we need to turn.
    Sprite.Direction := Sprite.Direction + (3 * delta);
    end;
    [/pascal]

    by doing this, the peds just go round and round....
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  4. #4

    Re: 2D Overhead Pedestrian Direction

    Maybe this is a interesting read: http://en.wikipedia.org/wiki/B%C3%A9zier_curve
    Especialy take a look at the Quadratic curves with animation.
    Also do take a look at the thread mentioned by chronozphere.
    http://3das.noeska.com - create adventure games without programming

  5. #5

    Re: 2D Overhead Pedestrian Direction

    I use this function to first get difference between the current angle and wanted angle:
    (If you use radians just change all 180 to PI and 360 to 2*PI)
    [pascal]function Angle3(src,dest: double): double;
    begin
    result:=src-dest;
    while result<-180 do result:=result+360;
    while result>180 do result:=result-360;
    end;[/pascal]
    This is because if your current angle is towards 45 degrees and you want to turn to 350 the shortest way is to negative side. This function always returns -180 to 180 that is directly the amount that you have to add to your current angle to end up to destination.

    So if you want to turn slowly, just multiply result from this function by 0.1 or something and add it to current angle. There are different ways to use result from this...
    ...or if result is negative you want to turn left, and if its positive then turn right.

  6. #6

    Re: 2D Overhead Pedestrian Direction

    hi User137

    thank you very much, after trying your method a few differ ways i have put together the following:

    [pascal]
    newDir := ArcTan2( DestinationY - Sprite.Y, DestinationX - Sprite.X );
    Sprite.Direction := Sprite.Direction - ((1 * delta) * Angle3(Sprite.Direction, newDir));
    [/pascal]

    the character automatically turns the shortest angle.... thanks again

    -Colin
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  7. #7

    Re: 2D Overhead Pedestrian Direction

    Colin: it's simple, just detect the new angle you calculated, and clip it to maximum angle change you want per frame.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

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
  •