Farseer 3.0 Decoupled Physics Demo

Developer
Jul 4, 2010 at 11:05 PM
Edited Jul 12, 2010 at 12:02 AM

Hi everyone,

I have created a demo that runs Farseer 3.0 on a separate thread. This allows the game to run in a variable time-step mode while the physics runs at a fixed time-step mode.

Here is the link to the demo - Sorry no more source code.

Please try it out and let me know what you think. I have an iCore7 2.8GHz so I need around 1000 bodies to get a good load on the CPU (23% lol)

Press space to switch between normal single threaded design and the separate thread design. Use the mouse to drag the bodies around.

I haven't tested this on the Xbox yet, but if anyone has time to try it out on the Xbox please let me know how it runs. And please post back with as much detail as you can.

I'm gonna try to add interpolation between frames and then the difference should be hugely noticeable.

Jul 5, 2010 at 8:38 AM

Thanks Matt, I've been meaning to do this in my game some time too, but haven't started looking into it yet.

Just wondering, are there any threading tips you've come across, but haven't implemented yet? I've seen some difficult discussions of threading before.

Jul 5, 2010 at 3:21 PM
Very interesting, here the framerate counter shows that it drops from around 400fps to 17fps (Core2 Quad CPU Q6600, 2.4GHz).
Developer
Jul 6, 2010 at 3:15 AM
Edited Jul 9, 2010 at 1:53 AM

Thanks for the input guys.

Robert: I don't know anything about threading really. In fact I bet it could be done much better then my implementation. but it works. As for tips about threading all I figured out was if your gonna modify something that is being modified on a separate thread then make sure to lock that object from both threads. You can see where I had to to this in the code. I run World.Step in a separate thread and lock it. But then when the user clicks to drag a body I also lock World. Comment out the lock code and you'll see what I mean about locking. If both ends are not locked it only works sometimes. lol Also I originally tried using ParallelTasks library, but it had severe problems with stability. I recommend using custom threading code for something like this and ParallelTasks for something simpler or maybe I just don't know how to use it.

Pnikosis: The physics are not actually running faster. They run at 50-60 Hz. You'll see in my code where if the update time was to long I take a slightly larger time-step. This is why the last pyramid crumbles, at least on my machine. The purpose for running the physics on a separate thread is so all the other animations and other stuff can run at a variable time-step. So even if your physics is only running at 60 Hz your animation code can run at any Hz. You'll also note that it just looks smoother, at least to me. When I add the position/rotation buffering with interpolation there should be a huge difference. We might be able to run our physics at 30 Hz and let the interpolation smooth out the rest for us. I'm going to try running this demo on my Xbox before I start work on the position/rotation buffer though. On a side note I think I might add some animation and maybe some simple particle effects so it's easy to tell the difference of why running the physics on a separate thread can be so beneficial.

Developer
Jul 9, 2010 at 1:51 AM
Edited Jul 12, 2010 at 12:03 AM

Sorry no more source code.

Jul 9, 2010 at 11:32 AM
Edited Jul 9, 2010 at 11:39 AM

The DebugViewXNA project is missing. When downloading from latest source code, the DebugViewXNA is looking for a method called GetAABB on Fixture, which dosen't exists.

Please provide a full sample, and please ALWAYS provide full samples. People hate when they have to digg for projects to complete a sample in order to make it run. 

Developer
Jul 12, 2010 at 12:01 AM

Here is the latest demo. No source code because it's to hard to update a reference.

http://www.mediafire.com/?oznm2uagndw

Jul 12, 2010 at 6:56 AM
The interpolation now really smooths the animation. Just a small detail: when some squares move quick, the animation "shakes" a little (the square jumps back to a previous position and then repositions itself).
Developer
Jul 12, 2010 at 9:41 AM

Pnikosis - I am working on that bug. Also whenever you grab something there is a lock contention between threads. I'm working on both these bugs and working on a C++ version too. I was able to get 3000+ bodies, all active, all while still looking smooth. So far in my tests moving the physics to a separate thread costs about 1ms on Windows, but I still need to clean up everything, integrate this tech into my own version of Farseer, and eliminate all garbage collection.