Is Farseer simply not designed to support my use case of late-positioning?

Topics: User Forum
Dec 9, 2012 at 4:49 AM

With my system, I'm working with heavy object pooling based on copying template objects.

So, I create a template object which may potentially have some Bodies/Shapes/Fixtures and Joints included.

When this object is requested again, the GameObjectFactory copies these Bodies and Joints into a new object. When returned, this object is not yet in use, and does not have a position.

The caller that requested the object will set its position at some indeterminate future point in time.

I seem to have two issues thus far:

(1) The Bodies/Joints from the template object are registered with the world (is there a way to avoid this while still using the provided Factories?)

(2) Changing the position of the requested Game Object manually (which, in turn, filters down to the Bodies), majorly screws up the Joints as there's no way to change Joint positions as far as I can tell. A Fixed Prismatic joint, for example, remains at 0,0 and this screws up the positioning of the Body.

If there are any misunderstanding on my part, please let me know.


Dec 9, 2012 at 1:26 PM
Edited Dec 9, 2012 at 1:27 PM


You would have to modify the code in the constructor of Body and Fixture not add/register themselves to the world until the time has come. Joint already needs manual World.AddJoint() anyway. You don't have to use the Factories - they are just  convenience methods.

Also you could create the body normally and set Enabled=false so it won't participate in the simulation.You could also modify your template method to teleport the newly created body somewhere safe (e.g. in a offscreen grid). Otherwise disabled bodies will clump around 0,0 and will burden the broadphase a bit when it queries that quadrant. 

I suppose you create and destroy many objects per frame. Otherwise in general I would be careful about physics pools. The biggest hit in creating a body is inserting it into the broadphase and finding new contacts (narrow phase). By teleporting a precreated body you would be essentially performing the same steps so no speed gains. You would gain less garbage collection which I suppose is a noble goal especially in heavy per frame changes... I think jbox2d has a body pool implemented for that purpose that you can have a look at.

As for question No2 I don't have experience with fixed joints because I don't use them. I use the "normal" ones anchored to static helper bodies which gives more flexibility and consistency in my opinion.  In general there is no problem teleporting articulated structures if the local reference frame remains unchanged (e.g. in the local space of the articulated object nothing is changed). Regular joints work only with local space but of course fixed joints present a problem because their local space changes as by definition the articulated body=world+fixed joint+body so I suppose for the fixed joints you would need to carefully update the anchors to the new positions which you can do as they are public members.