[SOLVED] Interpolation and Vehicle Issues

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
BrightBit
Posts: 21
Joined: Fri Jul 08, 2016 12:11 pm

[SOLVED] Interpolation and Vehicle Issues

Post by BrightBit »

I didn't want to pollute my first thread with several topics, so I started a new one. Now, where I got the "Interpolation Mode" running and tried to add a vehicle instance, I noticed some related issues:

1. Calling Space.Update() instead of Space.Update(dt) while BufferedStates.Enabled and InterpolatedStates.Enabled are both true makes the wheels not follow the car due to the fact that the line:

Code: Select all

worldAttachmentPoint += wheel.vehicle.Body.BufferedStates.InterpolatedStates.Position;
in the public method UpdateWorldTransform() of the CylinderCastWheelShape class will nevertheless use the interpolated vehicle position which is invalid since updates to InterpolatedStates only happen when calling the Space.Update(dt) method.

This issue almost made my head explode. Is there a reason for allowing Space.Update() calls although BufferedStates.Enabled and InterpolatedStates.Enabled are both true? Can I consider this a bug? :)

2. Even calling Space.Update(dt) (while BufferedStates.Enabled and InterpolatedStates.Enabled are both true) results in some weird behaviour as you can see in the following animation:

Image

The vehicle body looks as if it snaps to some invisible grid. I am updating the visual representation for the vehicle in Unity's LateUpdate() method which means that it definitely happens after BEPU Physics' update calls are all done. The code looks like this:

Code: Select all

transform.position = vehicle.Body.Position.ToUnity();
Setting BufferedStates.Enabled and InterpolatedStates.Enabled both to false resolves this problem as you can see here:

Image

So I thought it might be related to interpolation as well. What do you think?
Last edited by BrightBit on Tue Jul 19, 2016 3:08 pm, edited 1 time in total.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Interpolation and Vehicle Issues

Post by Norbo »

Is there a reason for allowing Space.Update() calls although BufferedStates.Enabled and InterpolatedStates.Enabled are both true? Can I consider this a bug?
There are times where a user might want to use Space.Update while interpolated states are on, and trying to get in the way of that would tend to be more annoying than anything else.

That said, I'm not real happy with the whole internal time stepping and interpolation system across the board, which is why it is not part of the engine in v2. It really makes more sense to handle it at the application level, considering that it's totally unrelated to the actual simulation, and because there is no one True Way to perform buffered state management. In fact, it wouldn't be a bad idea to build your own time accumulator and state management (if needed) and ignore BEPUphysics's implementation. I don't use it in my own game.
The vehicle body looks as if it snaps to some invisible grid. I am updating the visual representation for the vehicle in Unity's LateUpdate() method which means that it definitely happens after BEPU Physics' update calls are all done. The code looks like this:
The direct Entity.Position property is unbuffered, so you're getting the latest raw uninterpolated state. Since the wheels are interpolated, they don't match up. Either make both uninterpolated, or both interpolated.
BrightBit
Posts: 21
Joined: Fri Jul 08, 2016 12:11 pm

Re: Interpolation and Vehicle Issues

Post by BrightBit »

Oh man, I should have been able to see that. I made the stupid assumption that because WheelShape.WorldTransform will return the interpolated or the uninterpolated state depending on the settings of the Space instance in use, Vehicle.Body.Position would do so too.

I fixed it and it works like a charm, now. Thanks again for your help and patience. :)

BTW: You're not using interpolated states in your own game? How do you deal with variable frame times then?


For readers who might be interested in what I've done. Instead of writing:

Code: Select all

transform.position = vehicle.Body.Position.ToUnity();
I now wrote:

Code: Select all

transform.position = vehicle.Body.BufferedStates.InterpolatedStates.Position.ToUnity();
Note: The same needs to be done for the orientation.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Interpolation and Vehicle Issues

Post by Norbo »

You're not using interpolated states in your own game? How do you deal with variable frame times then?
I just built a game-specific system. I already had a semidynamic smoothing system for graphics built to handle network updates without visual snapping. Every object that could undergo correction had a current position/velocity state and a target position/velocity state, and they update like damped springy motors. I run a little implicit integrator on those states. It's kind of like a very simple physics simulation, except with only one thing involved at any time. So, when a network update update comes in with new position/velocity information, I immediately update the 'real' gameplay state and then just set the target state and let the integrator smooth it out visually.

From there, it's pretty easy to make the jump to handling variable frame times. Conceptually, the smoothing simulation can run independently from the game simulation and with a variable timestep thanks to the simulation simplicity and the implicit integrator. From its perspective, it's just receiving occasional new target states- it doesn't matter how they came about. So, if a graphics frame happens between physics updates, all it has to do is perform the graphical simulation starting from the last physical state, up to the current graphical time.

The main motivation here is a reduction in latency. When interpolating between the last two completed frames like the BEPUphysics implementation, you're always seeing results from the past. Extrapolation gives you a guess at what things should look like at any time with zero latency. That guess can be wrong, but when the simulation is running at 60 to 120hz, there isn't a lot of time for severe errors to accumulate. Network correction induced divergence tends to be much larger, and it doesn't cause any significant problems.

The devil is in the details, of course- things don't stay simple once you think about every single bone in every single character's animation being extrapolated, and needing to also extrapolate IK targets for feet on the ground, and every object could potentially require custom simulation logic for optimal extrapolation, and so on. But it works pretty well!
BrightBit
Posts: 21
Joined: Fri Jul 08, 2016 12:11 pm

Re: Interpolation and Vehicle Issues

Post by BrightBit »

As always: Thank you for your detailed answer! :)
Post Reply