Page 1 of 1
Easing the camera
Posted: Fri Jan 21, 2005 4:53 pm
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?
Posted: Fri Jan 21, 2005 5:44 pm
by Emmeran
I'd make the distance dependend by the speed of the car.
Posted: Sat Jan 22, 2005 1:58 am
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.
Ease too slow
Posted: Tue Jan 25, 2005 2:41 pm
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.
Posted: Tue Jan 25, 2005 11:15 pm
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.
Yeah 0.3 is pretty low
Posted: Thu Jan 27, 2005 1:51 pm
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.
Posted: Thu Jan 27, 2005 10:17 pm
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.