Share with us how are you using the powerrrr of the force
Moderator: Alain
by agi_shi » Sun Jun 29, 2008 9:36 am
Slow-motion can be simulated by either stepping the physics at a lower frame rate (like my little video), or just stepping them less often. Either way, you'll get choppy results. Implementing what "
fix your timestep" says... Result:
http://www.youtube.com/watch?v=htjS8NKRaHg
Both boxes are running at only 5 FPS (dt = 1.0 / 5.0), except one box is running in interpolated time (extremely smooth regardless of physics FPS), and the other in exact time (updates at same rate as physics).
The implementation is extremely easy, I'll walk you through it:
- Code: Select all
// pseudo-code
double physicsDeltatime = 1.0 / 5.0; // physics frequency, for example, 5 FPS
class physicsBody {
// previous and current position/orientation
vector p0, p1;
quaternion q0, q1;
// accumulation time
double time;
PhysicsSetTransform(matrix)
{
// store the "old" position/orientation
p0 = p1;
q0 = q1;
// store the "current" position/orientation
p1 = matrix.getPosition()
q1 = matrix.getOrientation()
// start interpolating at 0 again
time = 0;
}
// per-frame stepping
step(deltaTime)
{
// figure out how much we need to interpolate
double interp = deltaTime / physicsDeltaTime;
// add this to our interpolation
time += interp;
// lerp the position and slerp the orientation
vector position = p0 + (p1 - p0) * time;
quaternion orientation = quaterion::slerp(time, q0, q1);
// set this as the graphical body
setGraphicalBody(position, orientation);
}
}
// game loop
while (...)
{
// implement accumulator stepping
physicsTime += deltaTime;
while (physicsTime >= physicsDeltaTime)
{
NewtonUpdate(world, physicsDeltaTime;
physicsTime -= physicsDeltatime;
}
// for all physics objects
object->step(deltaTime); // per-frame graphical interpolation
}
That's really all there is to it. Very pseudo-code, but give a good math library it shouldn't be hard to implement.
Note: this lags 1 frame behind. The problem is that we're interpolating from the previous position to the current position, when the physics tell use we're already at the current position. This really isn't a problem unless you're running some 100% exact scientific simulation, in which case I'm sure you won't be running the physics at 5 FPS, or even 60 FPS for that matter
.
-
agi_shi
-
- Posts: 263
- Joined: Fri Aug 17, 2007 6:54 pm
by Julio Jerez » Sun Jun 29, 2008 10:57 am
Thank Shy, thei is very use full for many peopel.
I will put a copy of this post to the FAQ
-
Julio Jerez
- Moderator
-
- Posts: 12249
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by Leadwerks » Sun Jun 29, 2008 12:37 pm
You can just interpolate the matrices, which might be less expensive than quaternion maths.
-
Leadwerks
-
- Posts: 569
- Joined: Fri Oct 27, 2006 2:54 pm
by JernejL » Sun Jun 29, 2008 1:20 pm
I admit that looks incredibly impressive, that way you can run physics simulation at constant 25 / 30 fps while graphics subsystem can run with interpolated data at 60 fps, but running anything on as low as 5 is only good for testing, so it is not exactly a real world usage scenario (but it sure does look incredible).
-
JernejL
-
- Posts: 1578
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by agi_shi » Sun Jun 29, 2008 2:10 pm
Leadwerks wrote:You can just interpolate the matrices, which might be less expensive than quaternion maths.
How do you interpolate between two 4x4 matrices? Either way, my final calculations require the transformation in the form of vector & quaternion, so using a spherical interpolation comes practically for free since I already have the target quaternion.
Delfi wrote:admit that looks incredibly impressive, that way you can run physics simulation at constant 25 / 30 fps while graphics subsystem can run with interpolated data at 60 fps, but running anything on as low as 5 is only good for testing, so it is not exactly a real world usage scenario (but it sure does look incredible).
This usually comes in really handy with slow-motion. You can still run the physics at 60 FPS, but for slow motion you'll need to run the physics less frequently, causing the same choppiness (which is resolved using the interpolation). I just didn't really do the "real" slow motion in the video, instead I just took the easy route and ran the whole simulation at 5 FPS
Technically, the results should be the same, other than obvious collision inaccuracies that are resultant from the 12x larger step in time.
-
agi_shi
-
- Posts: 263
- Joined: Fri Aug 17, 2007 6:54 pm
by Dave Gravel » Sun Jun 29, 2008 10:11 pm
Nice stuff Thanks.
I have a question.
I'm not sure if i'm wrong maybe you can make the test with your simulation to see if it happening for you too.
I think you can have a 13 milliseconds of delay when the vsync is enabled.
The video driver need around 13 milliseconds to apply the vsync.
Exemple you have a screen that running at 60hz with vsync enabled.
Imagine that you create a simulation and in this simulation you have a object that can accumulate 100 of speed.
Now imagine you fix the step to 60 internally.
When the vsync is not enabled you get high fps but the simulation running at 60.
Your object can get the 100 of speed normally,
imagine if you enable the vsync the simulation stay at 60 fps anyway but you lost around 13 milliseconds in the speed value.
Normally the deltatime is suposed to fix the delay but on my test it seen to lost the 13 milliseconds anyway.
Exemple without vsync the speed come to 100 normally but with the vsync the speed value seen to lose around 13 in value and come to around 87.
Maybe i'm totally wrong, thanks if you have time to test it in your simulation.
Edited:
Oh I think I have find from where the problem that I get coming.
It seen to come from the way that I use to update my 3d scene.
I have test a new method and now the problem seen to have go away.
Last edited by
Dave Gravel on Sun Jun 29, 2008 11:40 pm, edited 1 time in total.
-
Dave Gravel
-
- Posts: 800
- Joined: Sat Apr 01, 2006 9:31 pm
- Location: Quebec in Canada.
-
by Rob » Sun Jun 29, 2008 10:58 pm
Leadwerks wrote:You can just interpolate the matrices, which might be less expensive than quaternion maths.
This would cause shapes to scale at a greater degree as angle deltas increase, same reason it's generally not used in character animation interpolation. Could be LoD based, but in the grand scheme of things, doubt it'd make enough of a difference.
-
Rob
-
- Posts: 7
- Joined: Tue May 23, 2006 6:28 pm
by agi_shi » Mon Jun 30, 2008 8:04 am
k00m wrote:Maybe i'm totally wrong, thanks if you have time to test it in your simulation.
Edited:
Oh I think I have find from where the problem that I get coming.
It seen to come from the way that I use to update my 3d scene.
I have test a new method and now the problem seen to have go away.
I don't do the interpolation based on any velocity, so it's pretty much immune to the above issue. Instead, I simply interpolate between the current and previous physics frames. The problem here is that you get 1 frame lag (up to <physics frequency> milliseconds), but the visual smoothness is much worth it.
-
agi_shi
-
- Posts: 263
- Joined: Fri Aug 17, 2007 6:54 pm
by Leadwerks » Fri Sep 05, 2008 12:12 am
you are right. i tried a linear interpolation of 4x4 mats. it was awful.
-
Leadwerks
-
- Posts: 569
- Joined: Fri Oct 27, 2006 2:54 pm
Return to User Gallery
Who is online
Users browsing this forum: No registered users and 45 guests