- Home /
Physics. What is interpolate, extrapolate, discrete, continuous?
I've been playing around with physics over the past few days, 2D physics. I understand mostly everything but when it comes to the interpolation and collision modes, I'm confused a bit.
Ill start with interpolate / extrapolate. I understand how each one affects rigidbodies, I turned the fixed timestep up to 0.5 seconds and simply let a body fall. With no interpolation, it was very juttery, with interpolate, it was really smooth as if the fixed timestep was not at 0.5, but slows down as it approaches another collider. Extrapolate also produces juttery results. However when fixed timestep is at the default 0.02 or less, there is no noticeable difference between this modes. But then it's documented as saying to use interpolate on characters driven by physics to smooth them out. Yet I don't see this being needed if your fixed timestep is low enough. So am I right in thinking it should be used in games where fixed timestep may be a little higher?
As for collision detection mode, I really really really cannot see any difference at all. I have done a lot of testing to try and see a difference but cannot. People say that discrete collision can cause colliders to pass through each other but I cannot get that to happen at all. No matter how fast I make a rigidbody move into another, it never passes through, ever. Whats the deal here?
Interpolate and extrapolate have a smoothing effect as you have seen. It just means that if you have frames being rendered in between physics steps, the position of the object will be guessed based on where it was previously (or where it will be if you use extrapolate).
But surely that would mean that there would be frames where bodies would clip (on the non fixed update frames), but that doesnt seem to happen at all
Answer by gfoot · Aug 09, 2014 at 12:11 AM
Regarding interpolation and extrapolation, you're right, if the physics frame rate is high then you won't notice much difference. One time when you will particularly notice a difference is if you arrange for your camera to move at a fixed velocity in Update(), and move a rigidbody at a fixed velocity via the physics. In this scenario, especially with a high render frame rate, you're likely to see some artefacts which would go away if you enabled interpolation.
My understanding is as follows:
The physics moves in discrete steps, e.g. at t=0.0s, t=0.5s, t=1.0s, t=1.5s. Suppose Unity is rendering a frame at time t=1.2s. It needs to decide what object positions to use.
The first option is to just use the positions calculated for t=1.0s - I believe this is the "None" option. However high the render frame rate is, the object will only visually move when the physics ticks.
The next option is extrapolation. Bear in mind that Unity hasn't calculated the t=1.5s position yet, because right now t=1.2s. But it can see the object's state at t=1.0s, including its linear and angular velocities, and extrapolate by assuming the object will continue with those velocities. So the time sinse t=1.0s is 0.2s, and it adds 0.2 times these velocities on to the linear and angular positions, and renders that location.
The last option is interpolation. Instead of guessing where the object might go, instead I believe Unity delays everything by one physics timestep. So at time t=1.0s, it would actually render the objects at their positions from t=0.5s. But it still calculates the physics for t=1.0s, it just doesn't render those positions yet. Later, at t=1.2s, it can now interpolate 0.2/0.5 of the way between the object positions at t=0.5s and t=1.0s.
Interpolation generally gives the best result, but at the expense of adding latency to the view. Extrapolation kind of works but runs the risk of objects being rendered at odd positions, e.g. penetrating each other, as it can extrapolate through a collision that it hasn't detected yet.
Again, as you said, it's often unnecessary to turn on interpolation at all, unless you've turned your physics timestep up really high. Generally the things on which you'll notice problems the most are physics objects which are moving vaguely in sync with non-physics objects that are moved in Update() rather than FixedUpdate(). The most important case is a camera tracking the player character, which is why Unity's recommendation is to turn interpolation on for the player character, but not for anything else.
Hey thanks for the answer. That all makes sense and I'm pretty sure you're spot on with interpolate being behind. I think it's always one frame behind. I ran a test... fixed timestep to 0.5, two rigidbodies, one with interpolate and with extrapolate. I pause before I play so it enters play mode paused. Then I hit next frame and the extrapolate body moves whilst the interpolate body stays still for 1 frame.
This answer is brilliant!! So explanatory... i have searched this info for long and now i found it! Thanks!
Answer by terresquall · Aug 03, 2020 at 03:56 PM
I teach Unity certification classes, and I receive a lot of questions about Interpolation and the Collsion Detection properties, so I've written articles on them. Interpolation is mainly used to remove jitter in Rigidbody movements, and Collision Detection is used to prevent fast-moving objects from tunnelling through thin objects.
https://blog.terresquall.com/2020/08/unity-rigidbodys-interpolate-property/ https://blog.terresquall.com/2019/12/collision-detection-modes-in-unitys-rigidbody-component/
Answer by haikezhijian · Jun 02, 2016 at 09:54 AM
I agree gfoot's explanation, but i think the point about interpolation is not right. unity doesn't delay a physics timestep to render, i test interpolation, don't find delay render, so i think it is just based on previous frames to calculate Velocity and Acceleration. So interpolation is more accurate than extrapolation. Because extrapolation is based on current Velocity, while interpolation is based on current Velocity and Acceleration. In other words, extrapolation is fit for constant Velocity, interpolation is fit for constant Velocity and non-constant Velocity. Certainly, interpolation mode will cost more.
By delaying by a physics timestep, think about it as the render is running as if it is a full physics timestep late. Therefore, the physics is not the one being delayed, its the render cycle running late. Clue's in the name. Extrapolation, by definition, is the process of estimating, beyond the original observation range, the value of a variable on the basis of its relationship with another variable.. Contrast this with interpolation, which is a method of constructing new data points within the range of a discrete set of known data points. This means that interpolation inherently requires 2 data points (in this case physics updates) to estimate between them, while extrapolation requires a point, but estimates after it and does not require a point after it. They are very different techniques. If they were similar, as you are saying, there would be absolutely no need for extrapolation, as most non-colliding objects are cheap to compute anyways.