943,597 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4489
  • C# RSS
Jan 1st, 2009
0

vectors to angles

Expand Post »
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
Similar Threads
Reputation Points: 34
Solved Threads: 4
Light Poster
midimatt is offline Offline
49 posts
since Mar 2008
Jan 1st, 2009
0

Re: vectors to angles

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.
Last edited by ddanbe; Jan 1st, 2009 at 8:09 am.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008
Jan 1st, 2009
2

Re: vectors to angles

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: <x,y,z> `dot` <x', y', z'> = x*x' + y*y' + z*z'. This works for any number of dimensions.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is online now Online
2,478 posts
since Jun 2005
Jan 1st, 2009
0

Re: vectors to angles

The Vector structure of C# also has a "dotproduct" method. (among many others)
But it may be a bit hard to find.
C# Syntax (Toggle Plain Text)
  1. private Double getDotProductExample()
  2. {
  3. Vector vector1 = new Vector(20, 30);
  4. Vector vector2 = new Vector(45, 70);
  5. Double doubleResult;
  6.  
  7. // Return the dot product of the two specified vectors.
  8. // The dot product is calculated using the following
  9. // formula: (vector1.X * vector2.X) + (vector1.Y * vector2.Y).
  10. // doubleResult is equal to 3000
  11. doubleResult = Vector.Multiply(vector1, vector2);
  12.  
  13. return doubleResult;
  14.  
  15. }

You can also use the overloaded * operator here.
Last edited by ddanbe; Jan 1st, 2009 at 7:14 pm.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,735 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: given path's format is not supported -virtual directory problem-
Next Thread in C# Forum Timeline: open IE with no add-ons





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC