This project has moved and is read-only. For the latest updates, please go here.

Identifying Selected Objects

Topics: Developer Forum, Project Management Forum, User Forum
Apr 17, 2011 at 8:57 AM

Hey guys,


Need a little help. I am using one of the supplied gamedemo files and writing my code by making changes in the original. I have multiple objects on my game screen that I can play around with by using the mouse.

I need to identify, firstly, when I click the mouse button is there an object at the pointers location or not. And secondly if there is which object is it, by index number or something.

All the objects are vertices in a list that have been converted to polygons by using the compoundpolygon tool.

Please help !!



Apr 17, 2011 at 3:22 PM

There is a World.Collide() method that checks if the specified point collides with any of the bodies in the world. To identify a body, use the UserData property.

Apr 18, 2011 at 11:23 AM

Thanks for the reply Genbox but I can't find World.Collide(), could you be a little more specific please. There is ContactList etc but no Collide method.

I am using Farseer 3.3



Apr 18, 2011 at 12:43 PM

It's called World.TestPoint()

Take at look at our samples framework and how the picking for the mousejoint is done in PhysicsGameScreen.cs.

Apr 18, 2011 at 4:08 PM

My mistake, it used to be called World.Collide(). :)

Apr 18, 2011 at 5:02 PM

Lol ! Thanks guys ! I still need a little more help though

I have gone through the code in the PhysicsGameScreen. What I want to do is that by clicking on a body I can change its rotation and properties like friction etc, using the keyboard. For that I need to identify that particular body.

How do you think I should go about doing it ? Could you explain a little too, I am relatively new. I did understand that UserData was to be used in someway.


Best Regards


Apr 18, 2011 at 8:06 PM

You could do it like this:

1. Call World.TestPoint() on the mouse position (look in the samples on how you do this)
2. When a fixture is returned, save it's body as a reference (myGlobalVisibleBody = testedFixture.Body)
3. When you press buttons to change properties, you just set the properties on the body (myGlobalVisibleBody.Friction = myFriction)

Note: Setting Friction property on the body is only supported in newer versions of FPE. Setting it on the body will set the friction on all the attached fixtures.

Apr 19, 2011 at 11:55 AM

Spot on GenBox !

Seems to work perfectly!

Thanks !

Jan 19, 2012 at 3:44 AM

i'm sorry for bumping an old thread. but my question is similar to this.

how to set tolerance for world.testpointall?.

world.testpointall will return all fixtures that has the point exactly contained in them. but what i'm looking for is that there's some tolerance, so that a point near fixture is also considered to be in the fixture as long as it's still below the tolerance. 

i see that farseer already implemented some tolerance using Settings.Epsilon. but when i copied the content of the function and changed the d to some other values bigger (like 0.6f), it didn't work

anyone knows something about this? 

Jan 22, 2012 at 12:38 AM

If you look at the code first World.TestPointAll  constructs and AABB at the point and then queries the world with the AABB which will get you all the fixtures overlapping the AABB. Then for each a Shape.TestPoint is performed with more accurately tests if the point is really inside the shape. 

Perhaps just stopping at the AABB stage is enough for you? For mouse picking it should be more than enough.

Or even more complicated - rewrite the code of Shape.TestPoint so that the vertices are scaled by some epsilon (the whole shape is scaled up) and only then the point is tested.