Monday, September 19, 2016

android - Why does my position interpolation code result in "jumpy" motion?


I am developing an android game with box2d and use a fixed timestep system for advancing the physics.


However as I use this system it requires the box2d positions to be interpolates. I read this article and have implemented an interpolation method very much like the one in the article.


The method seems to work nicely on the computer but on my phone the positions of objects are very jumpy. There is of course a big frame rate difference between PC and phone, but I think this algorithm should not mind that.


Here is the just of the code if you don't feel like looking at the article :



void PhysicsSystem::smoothStates_ ()
{
const float oneMinusRatio = 1.f - fixedTimestepAccumulatorRatio_;

for (b2Body * b = world_->GetBodyList (); b != NULL; b = b->GetNext ())
{
if (b->GetType () == b2_staticBody)
{
continue;
}


PhysicsComponent & c = PhysicsComponent::b2BodyToPhysicsComponent (* b);
c.smoothedPosition_ =
fixedTimestepAccumulatorRatio_ * b->GetPosition () +
oneMinusRatio * c.previousPosition_;
c.smoothedAngle_ =
fixedTimestepAccumulatorRatio_ * b->GetAngle () +
oneMinusRatio * c.previousAngle_;
}
}


Does anyone know why my game is acting like this?


Thanks for the help


EDIT: Here is some logging as suggested in the comments bellow log data


EDIT2 : This log shows the jumpy effect I think ( you can see the position diff is sometimes positive and sometimes negative - and the object are just falling, it should be constant)log data 2



Answer



Looking at your second log file, I'm wondering if you're calling resetSmoothStates() in the right place?


On lines 42, 46, 50, and 54 you can see that the original position stays at a constant [661.2183], indicating there hasn't been a physics update. On line 43, it looks like you're smoothing between the previous original position, [671.2361], and the new one, [661.2183]. But on line 47 the interpolated value snaps to the new position, [661.2183], and stays there until you get a new physics update.


Make sure previousPosition_ is only being updated when there's a new physics update, not every frame. (i.e make sure you're only calling resetSmoothStates() inside of the for (int i = 0; i < nStepsClamped; ++ i) block)


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...