# Moving a camera forward using x and y rotation

This topic is 2806 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 working on this for hours now and I've finally given in to asking for help

I'm want to make the camera move forward depending on its x and y rotation.
What I have more or less works, it's just not moving in a perfectly straight line:

 const double PI180 = 3.14159265 / 180; double movex, movey, movez, hs; double yRad, xRad; yRad = rotation.y * PI180; xRad = rotation.x * PI180; movex = sin( yRad ); movey = sin( xRad ); movez = -cos( yRad ); // Adjust horizontal speed hs = movey / 1; if ( hs < 0 ) hs *= -1; hs = 1 - hs; hs = hs * speed; position.x += movex * hs; position.y -= movey * speed; position.z += movez * hs; 

I adjust the horizontal speed depending on how steep the camera is looking e.g. looking straight down or up means that the camera doesn't move on either x or z axis.

There's probably a better way of doing this that I'm being slow about but in theory my way should work fine, I'm having trouble understanding why the camera moves down faster than it should.

Hopefully I've not been too confusing. If you know a better way of calculating the forward movement using the x, y and z rotations please let me know, it's been annoying me for ages.

##### Share on other sites
[font=arial,helvetica,sans-serif]Are you trying to get the camera to move in the direction it's facing? If so, use these formulae to get a unit vector in the direction of movement[/font]
[font=arial,helvetica,sans-serif] x = sin(yaw) * cos(pitch) z = cos(yaw) * cos(pitch) y = sin(pitch) [/font]

[font=arial,helvetica,sans-serif]Then multiply the vector by your speed and delta time and add to position.[/font]

[font=arial,helvetica,sans-serif]Edit: y-up, 0 yaw = movement down positive z[/font]

##### Share on other sites
Why does it always have to come down to math?! *shakes fist*

##### Share on other sites

Why does it always have to come down to math?! *shakes fist*

I keep pointing out that it's obviously a conspiracy by the liberal right-wing programming oligarchy, but no one seems to listen :-\

##### Share on other sites
Ahhhhhh that's worked perfectly, thank you I've done all this before but not programmed in a while, it's coming back slowly...
Thanks again

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 11
• 9
• 11
• 15