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

3D First Person Rotation Problem

Started by TheCrow33 Jun 15, 2011 at 7:52 PM 2 replies 1.8k views
Original Post
TheCrow33
TheCrow33
So I'm trying to code a part of a 3d program that will eventually rotate the camera kinda of like any First Person Shooter would. I.E. you move the mouse up and the head moves up, you move the mouse down and the head moves down looking at the ground, you move the mouse left and the head turns to the left. Anyway you get the picture, I have the left and right movements perfected, and up and down work to an extent, but I've messed up some detail in the math and I can't quite figure out what the hell I've done wrong. For some reason when the player is looking up or down the head moves the opposite direction after spinning (turning left or right) about 90 degrees either direction from the start. Not sure what I messed up but here's the code:

// Rotate the world in the opposite direction of player
glRotatef(-(player.getRotX() - 180), 0f, 1f, 0f);
glRotatef((player.getHeadTilt()), 1f - (float) Math.abs((Math.sin(player.getRotX() * PIOVER180))),
0f, 1f - (float) Math.abs((Math.cos(player.getRotX() * PIOVER180))));


The first call to rotate works without flaw, it's the second one that something has gone wrong in. Player.getRotX() get's the player's degrees of rotation about the Y axis (yes Y, because essentially they have rotated left or right), PIOVER180 is a constant that is essentially PI / 180. player.getHeadTilt() is the degrees of rotation about the X axis (so how far they have moved the head up or down). And obviously Math.abs is the absolute value function, Math.cos is the cosine function and Math.sin is the sine function. Can anyone point out the flaw, or possibly tell me why I should just trash that line and go with something else? Thanks
TheCrow33
TheCrow33
could still use some help on this if anyone has any ideas.
Zakwayda
Zakwayda
I'm not sure what you're doing there, but I can tell you that you don't need all that complexity (you should be able to apply the transforms you want using only rotations about the cardinal axes).
TheCrow33
TheCrow33
I thought the same thing, but in the order that I was calling them, this didn't produce a correct result (hence the complicated mess I made of it). Turns out that I needed to rotate about the x axis first, then about the y axis and it does the correct thing.

Topic Locked

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

Sign in to reply to this topic.