This project has moved. For the latest updates, please go here.

Body.Geoms ?

Feb 6, 2009 at 9:37 AM
Edited Feb 6, 2009 at 9:39 AM
Hi, something what surprised me is the lack of a reference to a list of geoms that are attached to a body. There is a geom.body property, but not the other way around. Or is there?
Feb 6, 2009 at 2:16 PM
The Body and Geom classes does only have a oneway reference. Geometries know about the body attached but not the other way around. This is because geometries depend on a body, but a body can exist without a geometry.

You can easily create this functionality, but as we have a lot of new users (new to farseer and new to programming) I think we just let this go unless a lot of users request this functionality. The reason we let it go is that it can create unpredictable behavior. Users might get the idea that a default geometry is attached to a body and try to access it. They will get a nullreference exception and post about it.

As an alternative we always have the body.Tag property that can include a reference to the geometry.
Feb 6, 2009 at 5:04 PM
Ok. I understand you're not looking for more questions of new users hehe. Apart from that I think it would still be useful. It's cleaner for a game-object to store only a reference to a body, instead of both reference to body and to that one's geometries.
Feb 6, 2009 at 7:14 PM
Questions are fine.  They're how we learn.  In addition to the Tag option, you could create a struct to couple together a body and it's multiple (or not) geometries.  Rather than storing references to the body elsewhere in your code, you can just store the structure.  It could then have methods for cloning or disposing.

If the body were to reference the geoms directly, it would create a circular reference, which means the garbage collector wouldn't remove it from memory once it is discarded, resulting in memory leaks, and run-on sentences like this one.  I suppose you could use a WeakReference object... hmmz....
Feb 7, 2009 at 8:19 AM
Oh yeah, I see. You're right about the circular reference.
As I want to use the tag property to store a reference to my game-object, I'm going for that struct. Thanks for the tip!