vectors to angles

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

vectors to angles

 
0
  #1
Jan 1st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,055
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 311
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: vectors to angles

 
0
  #2
Jan 1st, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: vectors to angles

 
2
  #3
Jan 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,055
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 311
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: vectors to angles

 
0
  #4
Jan 1st, 2009
The Vector structure of C# also has a "dotproduct" method. (among many others)
But it may be a bit hard to find.
  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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 1493 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC