| | |
vectors to angles
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
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
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
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.
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.
The Vector structure of C# also has a "dotproduct" method. (among many others)
But it may be a bit hard to find.
You can also use the overloaded * operator here.
But it may be a bit hard to find.
C# Syntax (Toggle Plain Text)
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- 3D Sound, calculating pan (Game Development)
Other Threads in the C# Forum
- Previous Thread: given path's format is not supported -virtual directory problem-
- Next Thread: open IE with no add-ons
Views: 1493 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box button buttons c# chat check checkbox class client code color combobox control conversion csharp custom database datagridview dataset datetime degrees draganddrop drawing encryption enum excel file files form format forms ftp function gcd gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






