Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on this for hours now and I've finally given in to asking for help :P

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.

Thanks in advance, Dan. cool.png

Share this post

Link to post
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]
x = sin(yaw) * cos(pitch)
z = cos(yaw) * cos(pitch)
y = sin(pitch)

[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 this post

Link to post
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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!