# Rotation angle from position/vector (2D)

This topic is 4982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Anyone know how to get a rotation value out of a vector? say i have a person whos origin is 0,0 (can change tho) and they are holding a gun at any given direction (which is found by taking X/Y pos of the mouse and going gunpos = bodypos + (mousepos/maxmousepos * maxlength).... all i need to know is how to find what orientation the gun should be (so it is facing away from origin) and rotate it with D3DXMatrixRotationZ any help is appreciated

##### Share on other sites
well, you are not very explicit, but i'll give a try. when you have a 2d vector and you want to find what the rotation is, you must use trigonometric fucntions. so let's say oX axis goes right and oY goes up. we will say that a rotation of 0 degrees is your gun is heading to the right (along oX). let's call this angle Alpha. let's say your coordinates are (X, Y). now
tan(Alpha) = Y/X; what you want to know is Alpha so
Alpha = arctan(Y/X); something like this.

##### Share on other sites

the way he handles the gun is what i want, ive got the hand moving down and what not, just having trouble with the rotation, ill give that tan fxn a shot (i suck at trig...and math in general )

##### Share on other sites
well, if you dont know math, then just leave the graphics programming away. the 2d is VERY VERY simple, but when you'll move to 3d, there the pain comes, with 3d vectors and quaternions(4d vectors). if you don't manage to understand some simple 2d transformations, then 3d will overwhelm you. get a math book first, and then move on to graphics. trigonometry, geometry and analithical geometry are very simple, because all the forumulas are generated using figures. so they are very simple to understand.

##### Share on other sites
nah, ive already done 3d games before, and can deal with all the transformations normally, its just this time i dont know the rotation because of how the game actually plays, which is new to me

(if i have the 3 rotations i can just plug them into a quaternion and be away because DX does it all for me :) )

##### Share on other sites
yeah, you're right. the dx does it for you. but if you want to program in opengl(like i do, i like it more), there is no quaternion support. or, let's say, you want to build the most realistic physics engine. personally, i think this is the most hard thing to do, where physics(mechanics) and math are used intesively, from integration schemes (see Runge-Kutta) to a lot of matrices and vectors. so, finally, you do have to know the math thing too. but, for the beginning, it's ok you know dx. everything must start somewhere, and i think that learning a graphics API is the place to start.

##### Share on other sites
atan does the job nicely, except the gun faces the wrong way pointing left...but ill catch it with an if statement and switch the rotation.... thanks alot

##### Share on other sites
np, i'm happy it helped. there is something about some angle signs i think, but it can be solved. happy coding. bye

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002274
×