Jittering and shaking when moving a rigidBody

Zaladen
Posts: 25
Joined: Thu May 14, 2015 10:21 am

Jittering and shaking when moving a rigidBody

Post by Zaladen »

Hi, I'm having trouble with objects jittering and shaking when moving them using setLinearVelocity(). It doesn't matter if they move fast or slow, the rendered model is always jittering back and forth slightly. It jitters more when moving faster though. What could be the problem to something like this?
Zaladen
Posts: 25
Joined: Thu May 14, 2015 10:21 am

Re: Jittering and shaking when moving a rigidBody

Post by Zaladen »

I have to bump this, need a quite quick response. :/ I don't understand this, my world is scaled correctly, framerate is stable, and it doesn't matter if the framerate is high or low, the jittering is still there.

Edit: I think this is a substep problem, because if I set the substep to 1 the jittering stops, but then of course everything slows down below 60 fps. When fps is higher than 60 though the jittering starts again even if I have substeps set to 1.
Zaladen
Posts: 25
Joined: Thu May 14, 2015 10:21 am

Re: Jittering and shaking when moving a rigidBody

Post by Zaladen »

Bumping this thread again. How should I implement substeps properly to avoid jittering? This is the way I do it now:

Code: Select all

int maxSubsteps = 1;
	
	if (deltaTime > 1.0f / 60.0f)
	{
		maxSubsteps = (deltaTime / (1.f / 60.0f));

		if (maxSubsteps == 1)
			maxSubsteps++;
	}

	dynamicsWorld->stepSimulation(deltaTime, maxSubsteps, 1.0f / 60.0f);
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: Jittering and shaking when moving a rigidBody

Post by benelot »

I am not sure if you should move your object with set linear velocity constantly. Have you considered using a kinematic object instead?

Have a look at my determinism example in this thread, maybe it helps you fix your issue.

http://bulletphysics.org/Bullet/phpBB3/ ... =9&t=11187

It should be in the example browser since long ago, but is not accepted yet.
Zaladen
Posts: 25
Joined: Thu May 14, 2015 10:21 am

Re: Jittering and shaking when moving a rigidBody

Post by Zaladen »

Ok thank you, I have considered using a kinematic object but if I do that do I need to resolve collisions manually? How does kinematic objects work?
hyyou
Posts: 96
Joined: Wed Mar 16, 2016 10:11 am

Re: Jittering and shaking when moving a rigidBody

Post by hyyou »

I have never set velocity directly every frame.
I always change velocity by apply impulse, and never encounter any jittering.

If you are hurry, a simple code to reproduce the issue can induce faster and better response from people.
Zaladen
Posts: 25
Joined: Thu May 14, 2015 10:21 am

Re: Jittering and shaking when moving a rigidBody

Post by Zaladen »

I use a variable to manipulate speed that's a vec3 of 3 floats and then I set the speed every frame with that variable, it's a bit hard to make that work with apply impulse. I guess a kinematic object is better, but how do I get that to work with collision response?
hyyou
Posts: 96
Joined: Wed Mar 16, 2016 10:11 am

Re: Jittering and shaking when moving a rigidBody

Post by hyyou »

I am only a beginner, but as far as I know, kinematic is not designed to receive force from automated collision response.

I doubt, setting velocity in every frame shouldn't cause jitter, otherwise Bullet would have rather-severe fault/bug.

Where did you call set velocity? - hopefully not inside some bullet's callback
Which function did you call to set velocity?
Did you try to print what velocity you set every frame? - just to make sure
Did you try with only single body in the scene. - if so, can you place the code here?
Zaladen
Posts: 25
Joined: Thu May 14, 2015 10:21 am

Re: Jittering and shaking when moving a rigidBody

Post by Zaladen »

I call set velocity inside the player class every frame. Even if I set it to a specific value and don't use the variable the jitter still happens. The thing is though, that it only happens between 30 and 60 fps, and this is what makes me wonder what the problem might be. If it's lower than 30 there's no jitter, if it's exactly 60 there's no jitter either (I use Vsync).
paokakis
Posts: 29
Joined: Mon Jan 04, 2021 10:31 am

Re: Jittering and shaking when moving a rigidBody

Post by paokakis »

Interested, have the same problem. I know it is an old post but I was wondering if someone found any solutions for this
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Jittering and shaking when moving a rigidBody

Post by drleviathan »

For the record: I'm in favor of poking the velocity instead of using applyForce(). A properly implemented velocity algorithm can be agnostic about the object's mass.

As to the jitter:

Are you using MotionState's to get a body's transform or do you harvest directly from the body?

The MotionState API is designed to supply a transform that is extrapolated forward from the body's current transform as known by the physics engine, using the body's current velocity and any "step remainder" (e.g. after the physics engine advances some integer number of substeps, when using fixed substeps). This can reduce visible jitter which is otherwise more evident when the render framerate aliases with the simulation substep frame rate.

If you are NOT using MotionStates but are harvesting the transform straight from the body then you will tend to see jitter... unless your render frame period and the simulation step duration always match and are always integer multiples of the fixed substep (assuming you are using fixed substeps which is recommended) -- this is tricky to accomplish so many people opt for the MotionState method.

Finally, I will mention that I recall seeing jitter even with the MotionState method for fast moving objects (and fast camera following them) suffering from heavy damping. Damping at high velocities isn't linear and so the linear extrapolation used by the MotionState system fails... at least this is what I remember but it was a long time ago.
paokakis
Posts: 29
Joined: Mon Jan 04, 2021 10:31 am

Re: Jittering and shaking when moving a rigidBody

Post by paokakis »

Thanks for the quick reply @drleviathan. I am indeed using applyForce() but when I use stepSimulation with substeps = 0

Code: Select all

mpDynamicsWorld->stepSimulation(delta, 0);
the movement is fine I only get jitter on retrieving the rotation. So when I move my mouse to rotate the Spaceship in my game I get a stutter (only on rotation) every several frames. If I use substeps , 1, 2, 3 or something I get jitter on movement (with applyForce) too. I harvest the position and rotation directly from the rigid body as you say although I tried retrieving a world transform from the motionstate of the rigid body to get position and rotation with the same results. I don't know if that's what you meant... Can you also point me to the way I should calculate the substeps for my application on the stepSimulation?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Jittering and shaking when moving a rigidBody

Post by drleviathan »

The delcaration for btDiscreteDynamicsWorld::stepSimulation() looks like this:

Code: Select all

   ·///if maxSubSteps > 0, it will interpolate motion between fixedTimeStep's
   ·virtual int stepSimulation(btScalar timeStep, int maxSubSteps = 1, btScalar fixedTimeStep = btScalar(1.) / btScalar(60.));
It returns the number of substeps taken. Harvesting the return value can be useful in some cases where you need to do post-simulation work but only when the actual substeps taken is non-zero. Note: if you submit a very tiny timeStep that is smaller than a fixedTimeStep then the stepSimulation() might not actually make a substep: instead it will accumulate your small timeStep and return 0. It will take a substep later when there is enough time piled up to fit a full fixedTimeStep.

If you supply maxSubsteps=0 then it goes into "variable step" mode where the simulation moves forward the full step duration, no matter its value. Such a configuration is not recommended. Only use it if you: (a) can guarantee reasonable limits to the range of step durations and (b) have a good understanding of the content in the simulation and how physics simulations work. IBasically: it can lead to a variety of subtle and not so subtle bugs such as: variable success at solving constraints and instabilities that blow velocities to NaN.

If you really want to eliminate transform glitch then consider this recommended checklist:

(1) Use fixed substeps and set the maxSubSteps argument of btDiscreteDynamicsWorld::stepSimulation() to be the max number of substeps that fit into the largest step you expect to ever submit... plus one. So, if you expect your main game loop to run at 30fps and your substeps are 1/60th of a second, then set maxSubSteps = 2 + 1.

(2) You should only ever poke velocity or use applyForce() inside SomeCustomAction::updateAction() where SomeCustomAction is a class that derives from btActionInterface. This guarantees that your poking logic happens every substep instead of every step... because Bullet will invoke that updateAction() method for every Action in the World every substep.

(3) Create SomeCustomMotionState which derives from btMotionState and has custom code inside SomeMotionSubstep::setWorldTransform() which does the Right Thing with the worldTransform argument (e.g. copies that transform to the GameObject and computes the new camera location). That method will be called every substep on every dynamic Body that is "active" and that has a non-null SomeMotionState instance.

If you still experience transform glitches with that system... then something odd is happening: submit relevant code snippets for review and be ready to: describe your render pipeline and justify your math.
paokakis
Posts: 29
Joined: Mon Jan 04, 2021 10:31 am

Re: Jittering and shaking when moving a rigidBody

Post by paokakis »

Hi again,

I implemented your first two suggestions with minor improvements. First of all now irrelevant of frame rate I get the same response from the physics engine, for example the maximum velocity of the spaceship is the same with 10 fps and with 60 fps but the jitter is still there. only on low framerates below 30 or at 60 fps I don't get jitter. Can you elaborate a bit on how to create a custom motion state? If you need to see code I can post here. Thanks in advance!

Edit:
after getting the position this way the jitter for the applyForce is gone

Code: Select all

btTransform trans;
(*body)->getMotionState()->getWorldTransform(trans);
pos = trans.getOrigin();
retPos = glm::vec3(pos.getX(), pos.getY(), pos.getZ());
The only jitter that remains is that of the rotation. Inside the Action I am updating the rotation this way:

Code: Select all

// use mouse position to compute a delta-rotation in spaceship local-frame
btQuaternion deltaLocalRotation(yaw, pitch, roll);

// get old ship rotation
btTransform transform = body->getWorldTransform();
btQuaternion oldBodyRotation = transform.getRotation();

btQuaternion newBodyRotation = oldBodyRotation * deltaLocalRotation;

transform.setRotation(newBodyRotation);
body->setWorldTransform(transform);
I can not use the motion state here, correct? I think here is the jitter of the rotation. This happens every substep.
I am calculating the substeps this way:

Code: Select all

maxSubsteps = static_cast<int>(fabs(delta / (1.f / 60.0f) + 0.5f)) + 1;
and that's all. Any ideas on the rotation? Thanks!
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Jittering and shaking when moving a rigidBody

Post by drleviathan »

Here is an example minimal custom MotionState. This implementation assumes you have a GameObject class outside of Bullet which knows how to get/set its transform. If your Application is using some other math library (e.g. GLM) outside of the Bullet simulation then you will have to embed transcription logic either in the GameObject itself, or here in MyMotionState (this work is left as an exercise for the reader).

You can think of the MotionState as a "bridge object" which communicates between the Body in the simulation and the GameObject in your game logic and render pipeline. It is a good place to add other Body-vs-GameObject intelligence (e.g. activation, deactivation, changing other Body parameters (gravity, damping, restitution, etc) to help prevent you from leaking Bullet dependencies into other sections of your Application logic which should be physics-engine agnostic.

Code: Select all

class MyMotionState : public btMotionState
{
    btRigidBody* m_body;
    GameObject* m_gameObject;

public:
    MyMotionState(btRigidBody* body, GameObject* gameObject)
        :   m_body(body), m_gameObject(gameObject)
    {}

    // getWorldTransform() relays transform from external logic to Bullet.
    // It is called by Bullet in two places:
    // (1) Once when the Body is created and added to the world.
    // (2) Once per substep iff the Body is Kinematic and active.
    //
    void getWorldTransform(btTransform& worldTrans) const {
        // Note: worldTrans is passed by reference
        // It is our duty here to copy into it the correct transform as known
        // to external logic and Bullet will harvest·
        //·
        m_gameObject->getWorldTransform(worldTrans);
    }

    // setWorldTransform() relays Body transform from the Bullet simulation to the GameObject
    // and is called once per substep iff the Body is Dynamic and active.
    //
    void setWorldTransform(const btTransform& worldTrans) {
        // It is our duty here to apply worldTrans to GameObject.
        // Note: worldTrans is NOT necessarily the Body's transform as known to Bullet.
        // It is an extrapolated transform based on the Body's state at the
        // end of the last substep... extrapolated forward into the future (e.g. the remainder
        // not yet stepped) according the current Body velocities.
        //
        m_gameObject->setWorldTransform(worldTrans);
    }
};
Post Reply