Easing the camera

Problems building or running the engine, queries about how to use features etc.
laweya
Kobold
Posts: 28
Joined: Mon Jan 17, 2005 3:48 pm
Location: Kenya

Easing the camera

Post by laweya »

I have two scene nodes, one has a car attached and the other a camera, I've set it up such that when the car's node move so does the camera so basically a third person camera following the car around, I want to ease the camera into the cars position like in NFS, GTA etc. I figure it should work abit like inertia, when the car brakes, the camera keeps going then brakes. Any ideas?
User avatar
Emmeran
Goblin
Posts: 272
Joined: Wed Jun 02, 2004 11:47 am
Location: Erlangen

Post by Emmeran »

I'd make the distance dependend by the speed of the car.
User avatar
alphageek
Gnome
Posts: 365
Joined: Mon Jan 03, 2005 11:56 am

Post by alphageek »

Don't attach the camera scene node to the car scene node, attach it to the scene root node.
Then, every frame change the camera position to a point interpolated between
- current camera position
- world-space position of the desired camera offset from the car

Code: Select all

// camera location behind the car
Vector3 cameraOffset(0.0f, 10.0f, 20.0f);

// world-space desired camera position
Vector3 desiredCameraPosition = carNode.getPosition() + (carNode.getOrientation() * cameraOffset);

Vector3 completePositionCorrection = desiredCameraPosition - cameraNode.getPosition();

const Real correctionValue = 0.3f;

// adjust the full correction for the correctionValue, and also for the time
Vector3 stepPositionCorrection = completePositionCorrection * (correctionValue * timeSinceLastStep);

cameraNode.setPosition(cameraNode.getPosition() + stepPositionCorrection);

// also you probably want something like
// cameraNode.lookAt(carNode.getPosition(), SceneNode::TS_WORLD);

// or more likely (camera looks in front of the car)
Vector3 cameraViewOffset(0.0f, 5.0f, -10.0f);
Vector3 worldSpaceCameraViewOffset = carNode.getPosition() + (carNode.getOrientation() * cameraViewOffset);
cameraNode.lookAt(worldSpaceCameraViewOffset, SceneNode::TS_WORLD);

I just typed that out, so there are likely lot's of typos.
I used lots of locals for the purpose of code-documentation/clarity.
Axiomatic: action-packed space shooter
TaskScheduler 0.3
laweya
Kobold
Posts: 28
Joined: Mon Jan 17, 2005 3:48 pm
Location: Kenya

Ease too slow

Post by laweya »

Hey alphageek, I tried out your code but its not exactly what I had in mind, (Typical programming, as sson as you solve one problem you create another) the camera is left to far behind and only catches up when the car is stopped for a while. I have a new theory, I define 3 vectors, desired distance from the car node, and a maximum and minimum distance the camera can't go beyond. Whenever the camera is beyond any of this 2 distances, the camera starts accelerating towards the car depending on the cars speed and faster so that it eventually catches up even if the car keeps accelerating. I havent tried it out but I'll post the code when I do.
User avatar
alphageek
Gnome
Posts: 365
Joined: Mon Jan 03, 2005 11:56 am

Post by alphageek »

That's what I put the "correctionValue" in for. Increase that, and you increase the speed at which the camera position corrects. You can even have it greater than 1.0, just don't let (correctionValue * timeSinceLastStep) get greater than 1.0.

Haha. I've just checked and I use a value of 5.0f for my correctionValue. So yes 0.3f would be pretty slow.
Axiomatic: action-packed space shooter
TaskScheduler 0.3
laweya
Kobold
Posts: 28
Joined: Mon Jan 17, 2005 3:48 pm
Location: Kenya

Yeah 0.3 is pretty low

Post by laweya »

I tried it out with 5.0, worked like a charm, I had adjusted it to 1.0 before and it was still slow and didn't try a higer value, Thanks.
User avatar
alphageek
Gnome
Posts: 365
Joined: Mon Jan 03, 2005 11:56 am

Post by alphageek »

Cool. Yeah the value looks like it would be a proportion (from 0.0 to 1.0), but since it's also scaled by time that value is more like an inverse-half-life.
Axiomatic: action-packed space shooter
TaskScheduler 0.3