Spring Camera

Post Reply
User avatar
JohnHardy
Posts: 18
Joined: Mon Mar 05, 2012 11:39 pm
Location: Lancaster, UK
Contact:

Spring Camera

Post by JohnHardy »

Hey all,

I'm trying to get a camera to smoothly follow my RigidBody but get a strange jittering effect. (Not my video, but it looks similar to this: http://www.youtube.com/watch?v=_3R_8xJ7-rE)


So I've tried two things.
Thing one: Updating the camera using the physics time step.

Code: Select all

function updateWorld() // uses framerate independant fixed timestep
{
     var currentTime = getCurrentTime();
     var delta = currentTime - lastTime;
     lastTime = currenTime;

     applyForces(delta)

     world.stepSimulation(delta, 100, 0.1)
     
     updateCamera(delta)

}
Normally, it will run smooth and fine. However, if I move it to fullscreen, interact with the UI, or press PrintScreen (anything to tax the framerate a bit) the shudder will become far more pronounced. This shudder is due to the spring behaviour of the camera, bouncing forwards towards the target.

So when I press PrintScreen the RigidBody will move forward more than a normal frame because of the increased delta time. Then, when I update the camera, it is further behind than it was so it jerks forwards to try and compensate. I can affect the speed of this "jerk" by changing the damping constants of my spring.

However, this really isn't good enough so I tried:

Thing two: Updating the camera in the fixed time step.

Code: Select all

world.setInternalTickCallback(postSubStepTick, null, false);

function postSubStepTick(world, t)
{
     updateCamera(t);
}

function updateWorld() // uses framerate independant fixed timestep
{
     var currentTime = getCurrentTime();
     var delta = currentTime - lastTime;
     lastTime = currenTime;

     applyForces(delta)

     world.stepSimulation(delta, 100, 0.1)
}
The thought behind this is that by placing the camera update in the setInternalTickCallback that it will be simulated after the rest of the physics and account for the interpolated values so the spring should not come out of sync. So this works in so far as the spring is not bouncing around. However, looking at the source, as best I can tell, the motion states are synced AFTER the postSubStepTick callback has fired. The result on screen is a more regular pronounced jitter over a shorter distance.

When I added up all the values of "t" in postSubStepTick, these fell short of the timestep fDelta, instead this seems to get fired for each internal tick callback.

I've looked around a fair bit for an answer to this, but I can't find anything that helps. I'm not sure which direction to go in, so any help / experience would be greatly appreciated!

Thanks,

John
Post Reply