Jerky Movement

MetallicGear
Posts: 4
Joined: Fri Dec 03, 2010 10:14 pm

Jerky Movement

Post by MetallicGear »

Hi, I'm working on a project using version 2.7.7 of Bullet. I have set up a zero-gravity, zero drag environment, with a sphere (btSphereShape) which I impulse around using an Xbox controller. (mass = 1)

I am using the motion state to update the engine-level matrix, which I then use to draw my mesh.

When the maxSubStep is set to > 0 , (usually 1 - 10), the motion is noticeably jerky. However, if I change the maxSubStep to 0, the motion is perfect.

The delta time is in seconds (not milliseconds), and I leave the third parameter of stepSimulation at the default (1.0f/60.0f).

My frame-rate is about 30 fps, with a low spike of 22 and high of about 35.

The jerkiness is most apparent when I am watching the sphere with a follow-cam with some smoothing, which tracks the ball but shows every deviation. The effect is not as easy to spot from a static camera. It feels like a bad lerp, but I am unsure of the cause.

Help!
MetallicGear
Posts: 4
Joined: Fri Dec 03, 2010 10:14 pm

Re: Jerky Movement

Post by MetallicGear »

If this is not enough information, I can also post code. (Just let me know what you'd like to see.) This morning I checked the camera code to make sure that my viewport was not the source of the jitter. After changing it to a fixed camera, I was able to see the jitter in the object moving around, even if it was less apparent.
I also eliminated my movement code as a possible the source of the jitter. If my moving sphere strikes another sphere, that sphere will begin to move away, but also with the small jitter. This means that my impulse code is not the source of the issue as the second sphere's movement is an effect of the impact, handled by Bullet.

Somehow Bullet using a fixed time step in my implementation is causing this, and I'm not sure how.
MetallicGear
Posts: 4
Joined: Fri Dec 03, 2010 10:14 pm

Re: Jerky Movement

Post by MetallicGear »

Ok, yet another update:

I have cleared out my test space, and all that I have is a ball impulsed on the first tick by 1 in the X direction. No gravity, no drag. So the ball should travel with constant velocity in the X direction.

My position is determined by Bullet, so I calculate the following:

EVec3 directionVector = currentPosition - m_PrevPosition;
float mySpeed = directionVector.Length();
mySpeed /= fTime;

Since I am at a fixed timestep, then the velocity gathered by querying subsequent positions passed by Bullet, divided by the delta time (fTime), should be fairly consistent. However, I am getting significant spikes. Here is a sample of my data:

Speed: 2.053118
fTime: 0.002259
------------
Speed: 2.008435
fTime: 0.002553
------------
Speed: 1.948355
fTime: 0.002256
------------
Speed: 2.042044
fTime: 0.002272
------------
Speed: 1.952286
fTime: 0.002251
------------
Speed: 2.066603
fTime: 0.002245
------------
Speed: 16.317106 spike
fTime: 0.002244
------------
Speed: 2.004233
fTime: 0.002314
------------
Speed: 2.063043
fTime: 0.002248
------------
Speed: 1.929606
fTime: 0.002277
------------
Speed: 2.063970
fTime: 0.002247
------------
Speed: 2.021967
fTime: 0.002294
------------
Speed: 1.955002
fTime: 0.002248
------------
Speed: 15.624657 spike
fTime: 0.002250
------------
Speed: 2.020771
fTime: 0.002295
------------
Speed: 1.937881
fTime: 0.002268
------------
Speed: 2.048541
fTime: 0.002264
------------
Speed: 1.957748
fTime: 0.002245
------------
Speed: 2.065640
fTime: 0.002246
------------
Speed: 1.953097
fTime: 0.002250
------------
Speed: 1.955870
fTime: 0.002247
------------
Speed: 16.832762 spike
fTime: 0.002248
------------
Speed: 2.007176
fTime: 0.002311
------------
Speed: 2.037962
fTime: 0.002276


If anyone wants to see a larger sample size, I can get as large as you want, and any other parameters.

Oh, and if I change m_pDynamicsWorld->stepSimulation() so that I pass in "0" for MaxSubSteps, then the spikes disappear. (Using this however, causes more problems than it fixes, obviously).

I am pretty stuck at the moment, and I would appreciate even guesses as to what my problem might be.
MetallicGear
Posts: 4
Joined: Fri Dec 03, 2010 10:14 pm

Re: Jerky Movement

Post by MetallicGear »

And the saga comes to a close!

Looking deeper into the engine, I found a place where the bullet transform I was getting was being set back into Bullet whenever I set the transform on my object!

So, imagining that there are 6 ticks this frame and then a physics update, I was getting the motion state position, then (inadvertently) setting that position back into Bullet, then getting the position, etc, until on the physics update it was updating, but incorrectly offset by all the transforms I had been passing in.

So, after removing the offending line of code, everything is a smooth as butter. Phew!


This board has been a great resource while troubleshooting this problem, I appreciate the time everyone takes to maintain it. Keep up the good work!

Chris (MG)