# screwed up movement angles

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

## Recommended Posts

I've been using the standard way of moving a first person camera for a while but I'm getting a slight error that I either never noticed before or has just appeared. Here is my code:

 float yrotrad = (ry / 180 * PI); if(to.z != 0) { vel.x += float(cos(yrotrad)) * -to.z; vel.z += float(sin(yrotrad)) * -to.z; } 
ry is the rotation captured from mouse input in degrees
vel is a 3d vector that gets sent to my physics engine, it is the camera's velocity
to is another vector telling the camera which way to move in object space, -1 is backwards and +1 is forwards
PI is obviously equal to 3.14159

The trouble I'm getting is that my object is moving about 45 degrees right of the direction its supposed to be going. Any ideas why?

##### Share on other sites
How exactly are you calculating ry? I don't really see anything wrong with your math so I'm not quite sure what is causing the 45 degree discrepancy, so it may be that. I use this quick method to calculate the direction of my camera.

[source lang="cpp"]
horizontalAngle += mouseSpeed * float(ScreenWidth / 2 - xpos );
verticalAngle += mouseSpeed * float(ScreenHeight / 2 - ypos ); [/source]

Where mouseSpeed is some small floating point number(something like .005f) and xpos and ypos is your mouses x and y coordinates (Note: In this example my X axis is right to left and my Y axis is up and down. Judging from your vel.x and vel.z calculations your Z axis appears to be up and down while your Y is right to left). Then to calculate direction just use your found angles to convert from spherical coordinates to Cartesian as you do.

[source lang="cpp"]vec3 direction(
cos(verticalAngle) * sin(horizontalAngle), // X-Axis left to right
sin(verticalAngle), // Y-Axis up and down
cos(verticalAngle) * cos(horizontalAngle) // Z-Axis in and out of screen
);[/source]

You can restrict any look directions as you see fit.

Then when moving the camera you can just use position += direction*deltatime*speed and position -= direction*deltatime*speed or something similar to move forward and backward. So when you make your view matrix you can just add your direction vector to your position vector to find your "look at" vector. I hope that helped!

##### Share on other sites
Thanks for the reply I actually sat down after posting with some graph paper and a protractor and couldn't find any error with the math. ry is 0.8 times the number of pixels the mouse moved in one frame. This discrepancy must be coming from the physics engine (bullet) or my calculation of ry.