Rotation angle from position/vector (2D)

Started by
6 comments, last by meeshoo 19 years, 4 months ago
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
Advertisement
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.
sorry, heres a game http://www.madnesscombat.com/madness.zip

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 )
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.
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 :) )
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.
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
np, i'm happy it helped. there is something about some angle signs i think, but it can be solved. happy coding. bye

This topic is closed to new replies.

Advertisement