954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

vectors to angles

hey everyone.

Just a quick question i hope i'm using c# with XNA and currently i have created a game which draws a spaceship on the screen and allows me to move it around using an xbox360 controller.

what i'm trying to do is make the sprite of the spaceship face the direction it is traveling.

one way i was thinking of doing this is using the vector generated by the thumbstick

for example if i'm moving full left then the vector from the joystick is x = -1 y = 0.
and if i was moving right the vector would be
x = 1 y = 0.

i was just wondering if there way to convert that to an angle in radians.

maybe i'm looking at this totally the wrong way.

thanks in advance for any advice on this

-Midi

midimatt
Light Poster
49 posts since Mar 2008
Reputation Points: 34
Solved Threads: 4
 

For angle conversions you can look at http://www.daniweb.com/code/snippet976.html
There is also a Vector structure in C# which has an Anglebetween method.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

The dot product of two vectors is equal to the cosine of the angle between them divided by the vectors' magnitudes. For example:

The dot product of <1, 2> and <3, 4> is 1*3 + 2*4, i.e. 11. The magnitudes of the vectors are sqrt(1*1+2*2) and sqrt(3*3+4*4), i.e. sqrt(5) and sqrt(25).

So the cosine of the angle between the vectors is 11 / (sqrt(5)*sqrt(25)), i.e. 11 / (sqrt(5) * 5).

If we take the arccosine of that value, we get the angle:

acos(11/(sqrt(5)*5)).

So in general, the angle between two vectors u and v is

acos(dot(u, v) / sqrt(dot(u, u) * dot(v, v))),

where dot is the dot product function.

(It happens that the magnitude of a vector v can be written as sqrt(dot(v, v)).)

And if you haven't figured it out, the dot product of two vectors is the sum of the products of their constituent parts: `dot` = x*x' + y*y' + z*z'. This works for any number of dimensions.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

The Vector structure of C# also has a "dotproduct" method. (among many others)
But it may be a bit hard to find.

private Double getDotProductExample()
{
    Vector vector1 = new Vector(20, 30);
    Vector vector2 = new Vector(45, 70);
    Double doubleResult;

    // Return the dot product of the two specified vectors.
    // The dot product is calculated using the following 
    // formula: (vector1.X * vector2.X) + (vector1.Y * vector2.Y).
    // doubleResult is equal to 3000
    doubleResult = Vector.Multiply(vector1, vector2);

    return doubleResult;

}


You can also use the overloaded * operator here.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You