Inactivity / Resting / Sleep in 3.0?

Topics: Developer Forum, User Forum
Jun 12, 2010 at 6:59 AM

Well, after just over a year of not touching anything, I'm back again working on my game. I've spent the last couple weeks updating my code to use the latest version (2.1.3) of Farseer, MPE, and XNA. I'm now trying to pick up where I left off before and am remembering the problems I ran into then regarding sleep/resting/Inactivity, etc, and stackable objects.

genbox wrote at http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=60096:

In 3.0 we have a concept of bounds or world. It is simply a AABB that you define in size and when things get out of bounds, they will get disabled. No need to update stuff that falls out of the world. We also have a very good inactivity controller in 3.0 - In our early tests we can have several 1000 geomtries in the world that is sleeping while we have 1000 active geometries at 40 FPS. Just letting you know that good things will come in Farseer Physics 3.0 that helps you to get more of out the physics engine without actually doing anything.

Looking at the 3.0 source, I see that the InactivityController is gone.  In an old thread I was involved in (http://farseerphysics.codeplex.com/Thread/View.aspx?ThreadId=29842), I was pointed toward either the InactivityController or looking into Box2DX.  Well, I have been looking at Box2DX and they way they do sleeping (what they call it), was far superior to the InactivityController.  Before I spend time digging through possibly broken code in 3.0, or spend time trying to implement sleep the way Box2DX does it, I wanted to ask for an overview of this new method of improving resting bodies, or whatever we are calling it now.  Any help would be appreciated.

Developer
Jun 12, 2010 at 12:32 PM

The code in 3.0 is not broken. It's a direct port from Box2D.Xna, but now has most of Farseer 2.1's feature's like texture to polygon and factories.

In 3.0 resting is done per island. An island is a group of bodies all connected by contact points. If every body has come to rest and no new contacts are being formed then the entire body is put to sleep. For that group of bodies 95 to 99% of the load is removed from the engine. Resting is fully automatic, but adjustable in that you can set the minimum linear and angular velocities before a body is even considered for rest. Also you can set bodies to never rest.

It is possible to implement this style of resting in 2.1, but it would require a fairly major overhaul of almost the entire engine.

Also the latest source for 3.0 uses a dynamic tree for the broad phase and has a limitless world.

Jun 15, 2010 at 7:45 AM

I wish I had known about the Box2D.XNA port, very cool. I already read through all the Box2DX code before posting, and saw the islands thing, which looked very cool. I was going to copy it until you showed me there is no need now. Do you have any major hints as to FP3 changes (other than looking through the testbed, which I'm already doing)?  I did notice that the pyramid testbed example doesn't sleep the blocks, even though the Box2DX and Box2D.XNA examples of a pyramid both did.

mattbettcher wrote:

The code in 3.0 is not broken. It's a direct port from Box2D.Xna, but now has most of Farseer 2.1's feature's like texture to polygon and factories.

I've completed my move to FP3, now I just need to learn how to use it.  Any estimates as to what is still needed to get the DebugView to work?  When I used it in my game (previously FP2.01 and recently updated to FP2.1.3), the screen went white.  I haven't looked in the code yet, but I assume I will need to make some changes.  What functionality does it have and/or lack?  

Also, JointFactories and a few other things are still missing :P.  I guess I didn't mean "broken," I meant incomplete :).  I'm not complaining, you guys are doing some great stuff here.  I'm excited to be working on my game again and be able to attack a few of the problems I ran into a year ago which influenced my decline in motivation.

Coordinator
Jun 17, 2010 at 8:32 PM
The Box2DX project is dead. Together with Matthew and a couple of other guys, we had the task of updating Box2DX to the newest revision before using it in FPE 3.0. Fortunately Brandon and Nathan together with their brothers created the Box2D.XNA project that was a complete port of Box2D to C#/XNA. We helped them out with their port and then started implementing FPE 2.1 code in FPE 3.0. Right now we actually have a newer revision of Box2D inside FPE 3.0 than the Box2D.XNA team. I've talked to Brandon and Nathan and they should have the latest revision by next week. FPE 3.0 should be completely identical with Box2D.XNA in almost all parts. I've done some optimizations and worked the API to make it easier, but everything else should be the same. On a side note: I've noticed that FPE 3.0 and Box2D.XNA does not behave exactly the same in some cases. I'm waiting on them to update to the latest revision of Box2D to see if we are talking about a bug or what. It is in no way a serious bug (if it even exist), so I would go ahead and use FPE 3.0 if I were you. Let me know if you are missing something from FPE 2.x or find a bug.
Jun 18, 2010 at 2:11 AM

Thanks genbox.  I already put FP3.0 into my project and am taking the opportunity to rewrite a lot of it and do it right (my project, not FP3), which I would have had to do at some point anyways.  In one night worth of work, I was already able to get my blocks to stack properly, which I couldn't do in months worth of work a year ago.  Nice job!  I love the latest features of FP3.0.  It's so much better, for my case anyways.

I used to be using PinJoints,, but will probably use a revolute joint as I transition the rest of my code over and clean it all up.  Other than the JointFactory being non-existent, the only other thing I'm having a hard time with is the new Manifold class.  I'm trying to write a custom OnCollision method like I had in the past, where I can use the two points of two bodies when they collide to create a joint, but I'm still trying to figure out how using Manifold, instead of ContactList which was from FP2.x.  Other than that, all seems to be going swimmingly.

Coordinator
Jun 18, 2010 at 4:28 PM

Great feedback Jerky. FPE 3.0 should be better than FPE 2.x in almost any aspect - it is a hybrid between FPE and Box2D where we have taken the best of the two and combined them in a single engine (Reminds me of Frankenstein). I will take a look at the manifold stuff in OnCollision and see if I can simplify it.

Jun 20, 2010 at 6:36 AM
Edited Jun 23, 2010 at 4:21 AM
Thanks genbox. In FP2.x, I was about to create a joint at the point of collision grabbing the point from the contactlist using a custom OnCollision, like this: private bool MyOnCollisionMethod(Geom geom1, Geom geom2, ContactList list) { float distance; distance = Vector2.Distance(collisionIndex, list[0].Position); if (collisionJoint != null) { if (distance > 63f) { collisionJoint.Dispose(); Vector2 index = list[0].Position; if (geom1.Body.Moves == true) { collisionJoint = JointFactory.Instance.CreateRevoluteJoint(PhysicsSimulator, geom1.Body, geom2.Body, index); collisionJoint.TargetDistance = 0.2f; collisionJoint.Enabled = true; collisionJoint.Breakpoint = 2f; index = collisionIndex; } } } else if (collisionJoint == null) { Vector2 index = list[0].Position; collisionJoint = JointFactory.Instance.CreateRevoluteJoint(PhysicsSimulator, geom1.Body, geom2.Body, index); collisionJoint.Enabled = true; collisionJoint.Breakpoint = 2f; index = collisionIndex; } return true; //Setting this to false creates a cool tarzan swing where the revolute //joint creates only after it falls through another body. } So far, I haven't figured out how to do the same thing in FP3, even after reading the Box2D manual.
Jun 20, 2010 at 5:28 PM
Edited Jun 20, 2010 at 5:29 PM

Uhh what? I can't read that without it not being formatted. Or at least I'm not willing to try. Could you format it because I love seeing what ideas people have come up with for different things :)