Search for a body in a certain radius

Feb 13, 2011 at 11:50 AM

How would I go for searching if there is anything in a certain radius (or box, or rectangle, etc...)? Is there already something built in?

Coordinator
Feb 13, 2011 at 11:51 AM

World.QueryAABB() is a very fast way to check if anything is overlapping the given AABB.

Feb 13, 2011 at 4:58 PM

Alrght I will look at it. Thanks!

Developer
Feb 13, 2011 at 8:14 PM

You might also want to have a look at sensors. If you set Fixture.IsSensor to true, you will get notified when objects collide with it, without getting any collision response. Thus sensors can be used for all kinds of "invisible" triggers.

Feb 20, 2011 at 8:09 PM

Well I tried the QUeryAABB method, only it seems that I can't get it to work my code:

 

AABB temp = new AABB(size.X, 0.1f, _fixture.Body.Position + new Vector2(size.Y, 0));

            _world.QueryAABB(CheckUnderyourself, ref temp);

My Method is this:
        public bool CheckUnderyourself(Fixture fix)
        {

            SaniorGame.WriteToConsole(fix.GetType().ToString());
            if (fix.UserData is StaticEntity) //Can only jump off staticentity
            {

                _isOnGround = true;
                return false;
            }
            _isOnGround = false;
            
            
            return true;

        }

I am sure it is on a Farseer Simulated Entity. Only nothing gets written to the console. Could you show me how to use it?

Feb 21, 2011 at 9:21 AM
Edited Feb 21, 2011 at 9:21 AM

Try this:

 

_world.QueryAABB(
	fixture =>
	{
		SaniorGame.WriteToConsole(fix.GetType().ToString());
		if (fix.UserData is StaticEntity) //Can only jump off staticentity
		{

			_isOnGround = true;
			return false;
		}
		_isOnGround = false;

		// Continue the query.
		return true;
	}, ref aabb);
Feb 21, 2011 at 10:44 AM
Edited Feb 21, 2011 at 10:46 AM
Pnikosis wrote:

Try this:

_world.QueryAABB(
	fixture =>
	{
		SaniorGame.WriteToConsole(fix.GetType().ToString());
		if (fix.UserData is StaticEntity) //Can only jump off staticentity
		{
			_isOnGround = true;
			return false;
		}
		_isOnGround = false;

		// Continue the query.
		return true;
	}, ref aabb);

Isn't this the same as what @Commander2040 has but with a lambda expression rather than an external method pointer?  I don't think it will make any difference.

Also, in your case, fix.UserData and fix.GetType() should be fixture.UserData and fixture.GetType() since that's the name you gave to the argument above.

Feb 21, 2011 at 12:53 PM
Edited Feb 21, 2011 at 1:32 PM

It seems I must have made a mistake somewhere. It fixed it self, it works now. Thank you for the help!