Moving a camera forward using x and y rotation

Started by
3 comments, last by malborojones 12 years, 2 months ago
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
Never refuse a cup of tea, that's how wars are started.
Advertisement
[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]
Why does it always have to come down to math?! *shakes fist*

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 :-\
Ahhhhhh that's worked perfectly, thank you :D I've done all this before but not programmed in a while, it's coming back slowly...
Thanks again :D
Never refuse a cup of tea, that's how wars are started.

This topic is closed to new replies.

Advertisement