View Full Version : Smooth camera movement question...

11-28-2000, 09:04 AM
I'm not really sure how to describe this effect that I want. I have played a little with dark basic and one of the tutorials it offers is on a smooth camera (It doesn't give the actual math it's doing though).

So if I move left or right in a first person 3d world, (as soon as I let off of the key) the scene will not immediatly stop rotating. There is a bit of "play" in the rotation.

When I let off the key to turn, the scene will still "glide" a little in the direction. I like tis effect a lot and I'm just not sure how to set the trig up for it..

I use this code for moving around...

if(GetAsyncKeyState(VK_UP)) {
x += sin(angle * (3.14 / 180)) * walkSpeed;
z += cos(angle * (3.14 / 180)) * walkSpeed;
if(GetAsyncKeyState(VK_DOWN)) {
x -= sin(angle * (3.14 / 180)) * walkSpeed;
z -= cos(angle * (3.14 / 180)) * walkSpeed;
//Increase angle of rotation
if(GetAsyncKeyState(VK_LEFT)) { angle += 0.5;}
//Decrease angle of rotation
if(GetAsyncKeyState(VK_RIGHT)) { angle -= 0.5;}

..bunch of code...

//Move the scene
glLoadIdentity(); //Reset the scene
rotate = 360 - angle; //Determine wrap around angle
glRotatef(360 - lookAround, 1,0,0); //Rotate up and down
glRotatef(rotate,0,1,0); //Rotate side to side
glTranslatef(x,y,z); //Move to correct position

then draw the scene...

Any suggestions as to how I could modify this to get this effect? I hope I was clear enough on what I'm looking for. Thanks

11-28-2000, 09:44 AM
Hi Ace,

I don't know if this is working, but it's the idea I had just when reading your post:

Add two new variables:
float angleplus, angleminus;

Set these to 0.

Then change your LEFT key / RIGHT key routines:

//Increase angle of rotation
if(GetAsyncKeyState(VK_LEFT)) { angleplus = 0.5; }

//Decrease angle of rotation
if(GetAsyncKeyState(VK_RIGHT)) { angleminus = 0.5;}

And now decrease angleplus/angleminus in every loop by a specific amount, until they reach 0:

if (angleplus > 0) angleplus -= 0.01;
if (angleminus > 0) angleminus -= 0.01;

And then do this in every loop:

angle += angleplus;
angle -= angleminus;

You need to tweak a lot with the numbers until you get this correct. But I think that's one of the easyest ways.

Jens H.

11-28-2000, 10:37 AM
why not treat it as a physics problem? think about it: if the camera has rotational velocity, and it's changing at a linear rate, then it's due to a constant acceleration in the negative direction (in this case, the acceleration would be analogous to a drag force, such the the amount of acceleration would depend on the amount of velocity -- greater velocity creates greater drag, which slows it down until it's almost but not quite stopped.). you'll probably want to have a lower threshold so that when the speed of the camera drops below a certain threshold (call this threshold "perceptible movement"), you just set the velocity to zero, and stop calculating this. otherwise, the camera will slow down for a long long time... until you reach the smallest float...

Michael Steinberg
11-29-2000, 07:35 AM
Might be like to shoot with a rocket thrower at a bird phlake... http://www.opengl.org/discussion_boards/ubb/smile.gif But it's a bit more correct!

11-29-2000, 10:51 AM
i just lerp my camera from where it was to where it wants to go it gives me a very smooth movement alter DOLLYSPEED to change the "smoothness"

VECTOR dd = gotoCAM - old_cameraPOS;
float fracF = (dd.length())/DOLLYSPEED;
// cap it otherwise the camera will be goingbackwards into infinity
if (fracF>1)
float fracFdifference = 1.0 - fracF;

cameraPOS.x = old_cameraPOS.x*fracFdifference + gotoCAM.x*fracF;

11-29-2000, 12:00 PM
Thanks for all the replies...I should be able to get things working now.

11-29-2000, 01:03 PM
gladly. it'll be a neat looking effect, i'm sure.

i found your response highly amusing michael! http://www.opengl.org/discussion_boards/ubb/biggrin.gif i just wanted to present an alternative solution... sure, a little math-heavy maybe, but you can't argue with the accuracy... anyway.