Results 1 to 10 of 10

Thread: Motion at an Angle 2D (For the Games FAQ)

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Motion at an Angle 2D (For the Games FAQ)

    Well seen as though this has started to come up quite a lot I thought I'd get round to writing a quick tutorial to show you how it all works .

    Well to start off here is an image of the basic setup:


    This is based on the origin being in the bottom left corner. ie. As Y increases you go up and as X increases you go right.

    So now we need to find the values of dX and dY. This is quite simple actually, its just a right handed triangle and using Sin() & Cos(). Only tricky part is working out where we use Sin() and where we use Cos().

    So lets have a little Trig first:


    Right so on this diagram I can say:
    lX = Cos(B) * l
    lY = Sin(B) * l

    Now hold up a minute, beore you say you have the answer already just take notice of where the angle is. Yep the angle is against the bottom and on our first pic its on the Side. This causes the major differance in where we use the Sin()'s & Cos()'s so to fit this onto our problem we just rotate/flip the triangle to fit the first pic.



    Right so now we have the image rotated to fit the right angle you might notcie that if A == B then lY == dX and lX == dY. Yes I know maybe it wasn't a good idea to label them lX & lY but you see that now this means the Sin() & Cos() have swaped which is on X and Y.

    So now we can say:
    dX = Sin(A) * d
    dY = Cos(A) * d


    Before I end this just want to add because VB and quite a few other things in computing take the origin to be the Top Left that to make this chnage is simple. If you keep the Angle measured from the same place you just use the negative for the Y, giving:
    dX = Sin(A) * d
    dY = -Cos(A) * d

    Given all this you should be able to figure out how to work out which way round these go depending on your axis setup but I've shown the two most popular there.


    Now for the actual motion:
    This is really easy, seen as though we added the d value anyway. This is simply how fast you want the overal speed to be. So each frame we would do:
    Code:
    dX = Sin(A) * d
    dY = -Cos(A) * d
    
    XPos = XPos + dX
    YPos = YPos + dY
    This will cause your object to move with Speed d in direction A.
    Attached Images Attached Images    
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: Motion at an Angle 2D (For the Games FAQ)

    Very nice tutorial.
    I'd add the name of that type of 2D graph into the FAQ -- that really helped my understanding learning that Cartesian and I think Polar (???) are the same yet the way Polar works more closely related a game (usually). As you have a point and a distance or an angle and a distance on a polar plane.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  3. #3
    Computer Science BS Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Motion at an Angle 2D (For the Games FAQ)

    Don't forget to convert the A and B angles to radians

    Cos(A * Pi/180)
    Sin(A * Pi/180)

  4. #4

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: Motion at an Angle 2D (For the Games FAQ)

    Quote Originally Posted by Jacob Roman
    Don't forget to convert the A and B angles to radians

    Cos(A * Pi/180)
    Sin(A * Pi/180)
    Good point yea, but mind I'd expect you'd work in radians from the start anyway if its a game cos having loads of * Pi/180 is gonna slow things down .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Re: Motion at an Angle 2D (For the Games FAQ)

    Note that your trigs are going to slow down much more, and generally in a game you try to avoid polar cordinates as much as possible for that reason, as last resort you use a table of precalculated values on the trigs. For objects in motion you store the cartesian velocity vector, and add the velocity to the position each frame. Polar cordinates also make motion unrealistic. Otherwise if you insist on rotation, especially repeated with the same angle values, then you can save the calculation of trigs by using the same matrix for every rotation, and usually you can do with that unless you have angular accelartion.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    New Member Son of Makuta's Avatar
    Join Date
    Sep 2007
    Posts
    14

    Re: Motion at an Angle 2D (For the Games FAQ)

    Quick question: So how do you find out the angle from dX and dY? Last thing I knew there wasn't a sin/cos/tan inverse function...

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Motion at an Angle 2D (For the Games FAQ)

    There is an ArcTangent function. To avoid losing sign I use this
    Code:
    Option Explicit
    Public Const PI As Double = 3.14159265358979
    Public Const PI½ As Double = 1.5707963267949
    
    Public Function ATan2(ByVal Dy As Double, ByVal Dx As Double) As Double
      If Dx = 0 Then
        ATan2 = PI½ * Sgn(Dy)
      Else
        ATan2 = Atn(Dy / Dx)
        If Dx < 0 Then ATan2 = ATan2 + PI
      End If
    End Function
    Works fine as Single too in most cases

  8. #8
    New Member
    Join Date
    Jan 2008
    Location
    Sunny, Sunny England
    Posts
    4

    Re: Motion at an Angle 2D (For the Games FAQ)

    I think Pi can be calculated near enough with Abs(4) or something like that in VB6.
    Small thing bugging me is the use of dx and dy. The d means small change in but the change could be up to infinity. Just me being picky though (I see it too often in calculus to see it anywhere else )

  9. #9
    New Member Son of Makuta's Avatar
    Join Date
    Sep 2007
    Posts
    14

    Re: Motion at an Angle 2D (For the Games FAQ)

    Aye, but of course ordinary keyboards don't come with triangles for big changes :P

  10. #10
    New Member
    Join Date
    Jan 2008
    Location
    Sunny, Sunny England
    Posts
    4

    Re: Motion at an Angle 2D (For the Games FAQ)

    Too true but "small change in" is used for differentiation and by small change it means small - too small for trig in movement.

Posting Permissions

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



Click Here to Expand Forum to Full Width