Farseer Updating

Topics: Developer Forum, User Forum
Jun 19, 2009 at 6:16 PM

Hey there everyone! I'm trying to make a standard platforming game and have been putting a lot of thought into how Farseer updates. I absolutely love the program and has been working for me very well but I have a few possible problems that I need to learn how to deal with them before moving unto the big picture. From what I know about Farseer, it updates EVERYTHING that you have loaded into a map as long as it has been added to the physics simulator. Well I plan to have big levels for the game like what you do in standard platform games (such as Sonic!). What I'm trying to figure out is if there is anyway to have Farseer not update objects that are not on the screen. Only about 1/25 of the map will be showing at any point in time and I don't need the other 24/25 updating as well their physics. Is there anyway of just not updating those objects? I've thought of a couple routes where if their not in the screen, I just remove them from the Physics Simulator and then when they are in the screen, I just add them back in but I'm trying to find out if there is any other way to do this. Any help to this would be great!

Jun 19, 2009 at 9:20 PM
Edited Jun 19, 2009 at 9:21 PM

There is an "Inactivity Controller" class that you can use to prevent the engine from updating inactive objects. Take a look at Demo 3 from the advanced samples to see it in action, but the code to set it up is extremely simple.

This is right out of the sample:

// This distance has to be big enough to make sure all objects are reactivated
// before they could collide with an currently active object

// our biggest object is 25*25 -> 120 would be big enough
InactivityController controller = new InactivityController(PhysicsSimulator);
controller.ActivationDistance = 120;

// Deactivate the object after 2 seconds of idle time
controller.MaxIdleTime = 2000;
controller.Enabled = true;
PhysicsSimulator.Add(controller);

I've even linked this to my AI and other update modules to save some processing. Hope this helps :)

Jun 22, 2009 at 12:54 PM

> From what I know about Farseer, it updates EVERYTHING that you have loaded into a map as long as it has been added to the physics simulator

static Geoms are not updated anyways and for collision checking this is the job of the broad phase collider. i would try the naive approach first and only optimize the broad phase collider if you are really getting performance problems. if you have active objects (such as enemies) which you want to move only if they are close to the visible screen, you could use the inactivity controller as sterling mentioned.

Coordinator
Jun 22, 2009 at 3:50 PM

Both sterlingware and yobiv have answered this, but I'll just add a bit.

There are tons of ways to get more performance out of Farseer Physics. You just have to get a little creative sometimes.

The easiest is to only load what is needed. If you have 1000 geometries on a level, you can't have them all in the physics simulator, even if they are disabled. The current design of Farseer Physics traverse the list of geometries. It only calculate on those who are enabled, but just the overhead of iterating 1000 disabled geometries can take some time. This is by far the least of your worries. Having 1000 active geometries is a lot worse.

As yobiv said, you can create your own optimized broad phase. We have a decoupled broad phase collider design in Farseer Physics that lets you create your own. If you can logically separate your geometries, you can create a broad phase collider that only create pairs of colliding geometries (Arbiters) when when two geometries can collide (by checking if their AABB intersect). This is not recommended for new users but can be a powerful tool if used correctly.

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.

Jun 24, 2009 at 5:07 AM

Holy crap! I'm liking the sound of 3.0! Oh and I think I have a little bit to work on before I can do what you said in your secdond paragraph but still thank you very much for helping me out and also for making Farseer! Amazing physics engine!