Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vector to rotation matrix

Started by Mulligan Sep 12, 2006 at 12:52 AM 2 replies 850+ views
Original Post
Mulligan
Mulligan
I have a xbox 360 controller and want to take the 2d coordinate that represents the current thumbsrick orientation, and make a rotation matrix that will rotate a sprite on the screen to face the same way as the thumbstick. The coordinate ranges for the thumbstick are -1.0 to 1.0 for both X and Y. For whatever reason, this is stumping me, and I'd like to know how to do it. If I could convert the point into radians, it would solve my problem too, but I can't figure out how.... Thanks
Numsgil
Numsgil
If you divide the thumbstick coordinates to get the slope (y/x) you can then feed that into an arctan function to get the radian measures. You'll need to check which hemisphere you are in still, since arctain goes -pi/2 to pi/2. You can do this by checking the sign of x and using that to add pi to the degrees if x is < 0.

Then you need to double check that you are in the range 0 to 2 pi.

Then you need to watch for the case when x = 0 or you'll get a divide by 0.

It's not the greatest solution by any stretch of the imagination, but it gets the job done, and it's easy to conceptualize with even the basest of trigonometry skill.
[size=2]Darwinbots - [size=2]Artificial life simulation
Mulligan
Mulligan
perfect, works well.

Thanks
haegarr
haegarr
Don't know whether the API you are using provides it, but all these case distinctions are already wrapped into a function called atan2 (perhaps Numsgil knows that it doesn't exist for the xbox). The function is to be invoked as atan2(y,x) and normally results in the range [-pi,+pi], so perhaps you may be lucky with atan2(y,x)+PI.

[Edited by - haegarr on September 12, 2006 3:17:53 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.