XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Vector2 from magnitude and direction

Last post 08/11/2009 22:08 by cable729. 6 replies.
  • 06/11/2009 4:11

    Vector2 from magnitude and direction

    Simple, stupid question. How do I get a vector2 from a magnitude and an angle (should I use radians or degrees, btw?).

    Thanks in advance!
    This is the spot where I'm supposed to identify myself as a unique individual, right?
  • 06/11/2009 4:29 In reply to

    Re: Vector2 from magnitude and direction

    Answer
    Reply Quote
    Vector2 position = new Vector2(magnitude * (float)Math.Sin(angle), magnitude * (float)Math.Cos(angle));

    Assuming angle is in radians.  If it's in degrees, multiply it by (2 * Math.PI)/360 but I always try and stick to rads but that's the physicist in me :0)

    Regards,
    Mike
    Space Pirates Dev Blog and Space Pirates playtest Give a hoot - review a game!
  • 06/11/2009 4:54 In reply to

    Re: Vector2 from magnitude and direction

    Answer
    Reply Quote
    Taking his method, it is actually:

    Vector2 position = new Vector2(magnitude * (float)Math.Cos(angle), magnitude * (float)Math.Sin(angle));

    Cosine will give you your X value and sine will give Y value.
  • 06/11/2009 5:56 In reply to

    Re: Vector2 from magnitude and direction

    Answer
    Reply Quote
    There are, as usual, many conventions.

    A common math convention is that "0 degrees" is at X=1, Y=0, and that positive rotations go counter-clockwise (right-handed).
    However, there is also a convention that "0 degrees" is at X=0, Y=1, and that positive rotations go clockwise (left-handed).
    It turns out that Math.Atan2() in the CLR library actually uses the later, so you probably want to write your rotation-creation functions to match. Which means that X = sin(phi) and Y = cos(phi) (and phi is in radians).
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 06/11/2009 16:01 In reply to

    Re: Vector2 from magnitude and direction

    Wow, didn't realize that. Any particular reason for the differences?
  • 06/11/2009 16:19 In reply to

    Re: Vector2 from magnitude and direction

    The orientation of axes is entirely arbitary and so is ruled by convention.  For things like free-body diagrams, converting polar co-ords to cartesian and so on, right is X+ and down is Y-, sin gives you your X component and cosine gives you Y.  Since what we're doing is converting polar co-ords to cartesian, I just followed the convention I was taught.  For folk from other backgrounds, there can be any number of variations of X, Y, up, down, sin, cos and so on because as long as you're still using the rules of geometry it doesn't *really* matter what else you do.  A picture is still a picture, no matter how it's oriented or where you put it on the wall.  It's down to you to manage your equations so they're consistent - the numbers don't give a slippery fig!

    My method is entirely arbitary as I say, but that's the convention I've always both used and seen.  Confusion normally comes because onscreen Y is inverted (Y- is UP) and the origin is top-left, but the origin of a co-ord system is arbitary too.  If I stray from convention I always pop a postit on my monitor with the axes and origin marked because it's all too easy to slip back to 0,0 at bottom left and so on.  Screen co-ords are even more arbitary (from our perspective) because there's rarely a functional relationship between X and Y, it's just us saying "plonk a sprite on the screen here" so there's no *mathematical* need to have 0,0 on hard top-left.  There's even an argument for having the top-left of the viewport safe area as 0,0 ;0)

    Also, I use theta for the angle, but I'm just causing trouble now!

    Regards,
    Mike


    Space Pirates Dev Blog and Space Pirates playtest Give a hoot - review a game!
  • 08/11/2009 22:08 In reply to

    Re: Vector2 from magnitude and direction

    Thanks for the help you guys!
    This is the spot where I'm supposed to identify myself as a unique individual, right?
Page 1 of 1 (7 items) Previous Next