Targeting System

Topics: User Forum
Nov 28, 2008 at 4:32 PM
Hello all.

I'm rather new to the Engine as well as programming (c#/XNA)  so I apologies ahead of time if everything sounds rather noobish :)

What I'm trying to do is figure out sort of a targeting system.  I am currently just working on a game editor to setup the world, joints, ragdolls or whatever else I decide to put in a game.  I am currently stuck on targeting specific geoms, well I can used pickedGeom = Farseer.Physics.Collide(point); from the samples with a little modification to not use the spring,  but my problem is I want to be able draw a box around it or change the shade of the texture when it is selected and I couldn't find anyway of telling if each geom has an ID that I can check to make sure I have the right geom.  So I guess my question would be is there a way to find specific geoms by an ID of some sort.  I eventually would like to be able to "tab" through certain objects in the actual game itself much like an MMO but for now I would like to select an object in the editor and change the apperance or add a box around it so I know that object or multiple objects are selected.

any ideas or sample code are greatly appreciated.

I would like to say I love the engine,  really quite easy to get started with as well as a lot of fun.  Thank you very much.

Nov 28, 2008 at 6:16 PM
Yes there is. C# actually has an ID on all objects. Just run .GetHashCode() on the geometry and you will have an ID. You should note that while GetHashCode() returns identical id's for identical objects, it's not considered to be unique results. You can read more about this on

Another thing you should note, is that both the Geom and Body class have a "Tag" property that you can insert references to your objects into.
Example: If you have a wrapper object that contains a geometry and a body (from Farseer Physics) you can always reach the geom and body if you have the wrapper object reference. It's a little harder the other way around, but the Tag property makes this easy. Just do like this:

Wrapper wrapper = new Wrapper();
wrapper.Geom.Tag = wrapper;

You can now use Collide() and get the specific geometry that it collides with, and then get the reference to the wrapper (which should contain the drawing properties of your physics object) and for example change the color (to a "selected" color).

Thanks by the way for the kind words about the engine. It's great to hear that people like the engine as much as ourselves.
Dec 1, 2008 at 4:37 PM
Thank you for your reply genbox.  I pretty sure I get where your going.  Thanks for the nudge
Dec 3, 2008 at 9:48 PM
Edited Dec 3, 2008 at 9:53 PM
As previously mentioned, .GetHashCode and Geom.Tag will work great, but another thing to note, is that since the code is completely open source, you can always edit it free of charge :)

So, if you are getting some problems with getting non-unique results, you can try the following:

Adding a private int _geomID, with a public int GeomID { get geomID } to the geom class.
Adding the same thing to your wrapper.
Assigning a unique ID to the geom (via constructor) during the creation of the object by means of your own custom code.
Assigning the same thing to your wrapper.

Then use the GeomID to find the wrapper which is wrapping the geom, primarily for use in your implementation of OnCollision event.
This should work as the GeomID will be the same as WrapperID, and if you're using arrays or indexers, it will be a super quick find.
By means of _wrapper = Wrapper[geom1.GeomID].

This has worked alright for me thus far...
Dec 12, 2008 at 3:49 PM
Excellent!  Thank you very much.