InterpolatedStates not smooth?, v1.1.0

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
dexyfx
Posts: 7
Joined: Thu Feb 23, 2012 10:33 am

InterpolatedStates not smooth?, v1.1.0

Post by dexyfx »

Hi there,
Firstly I must say thanks to Norbo for BEPU! It's great..
I have been trying to get InterpolatedStates to work smoothly in my project with v1.1.0 of BEPU, but whatever I try, the result seems to not be smooth at all. In contrast, if I don't use InterpolatedStates and call Space.Update(), everything runs very smoothly but the physics will slow down when the framerate drops.. I'm guessing I'm missing something important, maybe someone can help me out..?

My Space initialisation code is as follows:

Code: Select all

Space = new Space();
Space.BufferedStates.Enabled = true;
Space.ForceUpdater.Gravity = new Vector3(0.0f, -9.8f, 0.0f);
Space.ThreadManager.AddThread();
Space.ThreadManager.AddThread();
Space is updated as such:

Code: Select all

Space.Update(elapsed);
After Space is updated, I set my rendering values on my entities from the InterpolatedStates: (Collidable is the BEPU Entity object)

Code: Select all

Position = Collidable.BufferedStates.InterpolatedStates.Position;
Orientation = Collidable.BufferedStates.InterpolatedStates.Orientation;
I make a matrix out of those for rendering with. I separate Position and Orientation out since I render everything in camera position space (ie Position - Camera.Position), but I don't think that matters here.

End result is for example, a ball rolling down a hill, or ragdoll ragdolling down the hill is jumping around a bit, as if the InterpolatedStates aren't correct.

If, however, I don't enable BufferedStates on the Space, call Space.Update() (with no time parameter), then use Collidable.Position and Collidable.Orientation, the result is all very smooth as expected, but won't work too well with a varying elapsed frame time, as expected.

Is there anything obvious here that I've missed?

Thanks!
Fe_Yoshi
Posts: 397
Joined: Tue Jul 04, 2006 5:05 pm
Location: New Tower!

Re: InterpolatedStates not smooth?, v1.1.0

Post by Fe_Yoshi »

Try adding more hamsters, and make sure they stay well-hydrated, as well as fed.

If that doesn't work, try adding more hamsters.
It's simple, just take the hydraulic phase ship emulator and attach it to the photon particle emitter, BAM, new tower!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: InterpolatedStates not smooth?, v1.1.0

Post by Norbo »

Here's a few possibilities:

-Is the simulation running asynchronously? If it is, updating the positions of the graphics by pulling the transforms one-by-one out of entities will not provide a consistent snapshot of simulation state. To address this, a complete set of the interpolated motion states can be grabbed atomically by using the Space.BufferedStates.InterpolatedStates.GetStates method. Those states are linked back to the entities by the entity.BufferedStates.MotionStateIndex. If it's not running asynchronously, there's no issue at all with going through one-by-one.

-Is the simulation taking more time than per update than the time step duration? If the simulation is consistently behind, it will perpetually try to 'keep up,' running multiple time steps every frame at significant cost. The result would be very low performance and things will appear to skip large distances between frames.

-Is the elapsed time being computed properly? A common issue is accidentally using milliseconds instead of seconds, resulting in the engine trying to simulate 1000x too fast. For any nontrivial simulation, the engine can't keep up and performance will drop.

-A few more hamsters would probably be good. I wasn't aware of the hamster-state buffering link, but Fe_Yoshi is a senior expert in these matters so I'll have to defer to him in this regard.
dexyfx
Posts: 7
Joined: Thu Feb 23, 2012 10:33 am

Re: InterpolatedStates not smooth?, v1.1.0

Post by dexyfx »

-I already have too many hamsters already.. You have a point though, maybe they are hungry or thirsty or something..

-Simulation isn't running asynchronously, World.Update(elapsed) is just being called from the XNA Game.Update() method.

-I don't think the simulation is lagging, there isn't much in the world. The thing runs at solid 60fps, with plenty of CPU time to spare.

-Time is measured in seconds.

It is a bit strange because it's sort of an intermittent problem. One moment it will appear to be running smoothly, and the next moment the moving objects will be stuttering.

Oh, and I removed the AddThread() lines from the Space initialisation part and there was no difference (so it's not the BEPU multithreading causing the issue..)

What is really strange is that if I use the Entity.Position directly when using Space.Update(dt), the result is smoother (but not 100%) than if I use the InterpolatedStates.

I don't really want to have to resort to doing fixed frame updates..
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: InterpolatedStates not smooth?, v1.1.0

Post by Norbo »

That's interesting- I'm not sure what is causing it then. A debuggable reproduction case would be helpful in diagnosing the issue- could you replicate the issue by modifying a demo, perhaps the MultithreadingDemo (probably the simplest self-contained demo) or a demo in the BEPUphysicsDemos itself (a bit trickier since the framework is more complicated)?
dexyfx
Posts: 7
Joined: Thu Feb 23, 2012 10:33 am

Re: InterpolatedStates not smooth?, v1.1.0

Post by dexyfx »

Thanks,
In trying to reproduce this issue I have come to the conclusion that it's not BEPU that's at fault. It's XNA....
In the simple scenario where I was attempting to reproduce the issue, I didn't have a problem. It only seems to happen when lots of things are being rendered. Turning off vsync and running in windowed mode seems to fix the problem, unfortunately it has the expected tearing and the odd frame where everything is just messed up. Without vsync on, and in fullscreen mode, the stuttering is however quite noticeable.
The main reason I came to the conclusion that something bizarre is going on with the rendering is that I have my camera's position attached to this "falling" entity. But the rendered object jumps around all over the place. It's almost as if the frame isn't getting completely rendered (even with vsync on!), as often I can see a nasty "tearing" thing happening where the top half or so of the screen seems to be a few frames behind the bottom half...
Really frustrating! Not sure what I can do about it... Guess I'll run it on a different PC and see if that helps...
I am sorry to have wasted your time on this.
Keep up the great work with BEPU!
Fe_Yoshi
Posts: 397
Joined: Tue Jul 04, 2006 5:05 pm
Location: New Tower!

Re: InterpolatedStates not smooth?, v1.1.0

Post by Fe_Yoshi »

Try using more hamsters.
It's simple, just take the hydraulic phase ship emulator and attach it to the photon particle emitter, BAM, new tower!
dexyfx
Posts: 7
Joined: Thu Feb 23, 2012 10:33 am

Re: InterpolatedStates not smooth?, v1.1.0

Post by dexyfx »

Well, I added 10x more hamsters, it didn't seem to help... Maybe some of them are running in the wrong direction?
dexyfx
Posts: 7
Joined: Thu Feb 23, 2012 10:33 am

Re: InterpolatedStates not smooth?, v1.1.0

Post by dexyfx »

So, after trying to solve this issue for too long, the only way I could seem to make it run smoothly is to use Game.IsFixedTimeStep=true. I really didn't want to do that though, I suppose I'll just have to live with the consequences of it (not sure what they are, but I initially set it to false because my camera motion seemed a lot smoother with it off). Also I am totally confused why I was unable to reproduce the issue outside of the current project I'm working on.

It seems that lots of people have been having trouble with XNA's sense of timing for a long time, I guess it's something they have never really fixed!

Oh, and I did run it on some different hardware over the weekend. The issue was still there, but in general it was smoother than on this system. I am still having VSync issues on this system which is not there on the nVidia hardware, but I'm in the process of trying to talk to AMD about that.
Post Reply