Unexpected Jitter

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
toaster
Posts: 14
Joined: Sat Mar 21, 2015 3:44 pm

Unexpected Jitter

Post by toaster »

https://www.youtube.com/watch?v=ileEyne2k4M

I took a video to demonstrate the jitter I'm seeing. My gravity is set to around 60. I tried lowering it but I see similar results. The static mesh is generated using:

PhysicsBody = new StaticMesh(staticTriangleVertices, staticTriangleIndices);
PhysicsBody.Sidedness = BEPUutilities.TriangleSidedness.Counterclockwise;

and really doesn't have any unique properties. The same thing can be said for the character controller. I used the following settings:

Character.StandingSpeed = 8.0f;
Character.BodyRadius = .35f;
Character.Body.Height = 1.0f;
Character.JumpSpeed = 20.0f;
Character.DoubleJumpSpeed = 20.0f/ 1.75f;
Character.AirSpeed = 2.5f;
Character.SlidingJumpSpeed = 0.5f;

I slightly changed the existing character controller to support a double jump feature. I also tried playing around with the timestep. The TimeStepDuration in the video is set to 1 /120. I also pass the gametime to the space.update method, but nothing really seems to fix the issue. I notice if I turn the TimeStepDuration to say 1/30 there is much more jitter, or if I turn it up it alleviates it slightly, but the jitter is still there. I also tried turning on the interpolation, but it did not have much of a change.

The game engine(Paradox3D) I am using uses async tasks for most update logic, perhaps this might cause an issue?

Thanks for any help!

-Toaster
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Unexpected Jitter

Post by Norbo »

This is usually caused by the physics being updated at an inconsistent rate. That is, in one frame the physics might simulate two time steps and in the next it might simulate one or three time steps. Most often this shows up when using internal timestepping (Space.Update(dt), as opposed to Space.Update) with a variable dt and without using the interpolated states.

It could also show up when the physics are updated in a different thread, and another thread reads the states directly without using a locked interpolated states view.

A few solutions if it's not a threading issue:
1) Turn on the interpolation buffers and use the interpolated states. Note that the interpolated states cannot be accessed through the entity properties like entity.Position directly, they must be accessed through the interpolation buffer or entity.BufferedStates.InterpolatedStates.Position. Further, in the video, it looks like the position of the model and the position of the camera are driven by different values; make sure they all use the same source to avoid a discontinuity.

2) If the game already has an update loop with a fixed time step, it may be best to just call Space.Update() without a parameter. That way, you don't need to worry about the engine accumulator differing from the game logic accumulator and everything stays synchronized without resorting to interpolation buffers.

3) There exist other forms of time step management which may work, depending on the game. As a simple example, the BEPUphysicsDemos will still only try to execute one time step per frame even when the application slows down. Different time accumulator/update algorithms could also be used to achieve a fixed time step. Some games can even use a variable time step (changing the TimeStepDuration every frame) if they're careful about making sure it doesn't change too much at once, where 'too much' is application specific.
toaster
Posts: 14
Joined: Sat Mar 21, 2015 3:44 pm

Re: Unexpected Jitter

Post by toaster »

I made some changes I did not realize that I would have to use the entity.BufferedStates.InterpolatedStates.Position. This seems to have slightly helped when the TimeStepDuration is set to 1/120 however at 1/60 there is still very noticeable jittering. Here is the code I am using now:

Code: Select all

            Space.TimeStepSettings.TimeStepDuration = 1 / 120.0f;
            Space.BufferedStates.Enabled = true;

            Space.Update((float)Game.UpdateTime.Elapsed.TotalSeconds);

            Position = Character.Body.BufferedStates.InterpolatedStates.Position.ToVector3();

            Camera.ChasePosition = Position;
The camera position was being interpolated, but I had turned that off prior to making the video the model and camera should have the exact same position on each update.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Unexpected Jitter

Post by Norbo »

If the physics aren't running asynchronously and interpolation states are properly used, I'm not sure what the source is. Disabling internal time stepping completely (using Space.Update without a parameter) to verify that inconsistent time accumulation is or is not related would be a good first diagnostic.

If the physics are running in a different thread, then using entity.BufferedStates.InterpolationStates.Position values is not sufficient in general. It doesn't take a lock on the interpolation buffers, so they can flip at inopportune times. To address this issue, acquire a lock on the Space.BufferedStates.InterpolatedStates.FlipLocker before accessing any interpolation states (or call Space.BufferedStates.InterpolatedStates.GetStates, which handles the locking for you and copies states into an array). However, this problem should only really be visible when you have more than one object in the scene to compare against, so it's still not a good explanation for that video.

If the physics thread is running with a long thread sleep between calls to Space.Update(dt), the engine won't be able to update the interpolation buffers. That would make jitter very visible.

If possible, running the physics sequentially would be a good sanity check.
toaster
Posts: 14
Joined: Sat Mar 21, 2015 3:44 pm

Re: Unexpected Jitter

Post by toaster »

I've removed the space.Update(dt) and changed it to space.Update() and then I turned off the interpolation. Even though this is how I original started I can't seem to see anymore jitter anymore except when going up and down steps, but I know this is because the steps warp you up and down. I think I'll have to make my own step manager to smooth this out a bit more. For a 3rd person view warping looks a bit unnatural. I suppose instead of changing the step manager I could create a custom collision mesh for my steps to make them slopes instead, but this is a bit more involved because the stairs are actually built into the voxel chunk's mesh, and the static mesh is calculated from the resulting vertices generated which is quite efficient because of my face merging algorithm.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Unexpected Jitter

Post by Norbo »

I think I'll have to make my own step manager to smooth this out a bit more. For a 3rd person view warping looks a bit unnatural. I suppose instead of changing the step manager I could create a custom collision mesh for my steps to make them slopes instead, but this is a bit more involved because the stairs are actually built into the voxel chunk's mesh, and the static mesh is calculated from the resulting vertices generated which is quite efficient because of my face merging algorithm.
The step manager is one of the most complicated parts of the character, so if teleportation-based stepping is required but the teleportation doesn't look good, I would recommend just interpolating the graphical representation to smooth out the step. The BEPUphysicsDemos do this for the camera to make walking up steps less jarring (CharacterCameraControlScheme.Update, in the if (UseCameraSmoothing) block).
toaster
Posts: 14
Joined: Sat Mar 21, 2015 3:44 pm

Re: Unexpected Jitter

Post by toaster »

Taking a look at that now.
Post Reply