Detecting collision between two objects

Sep 14, 2010 at 1:59 AM

Hey guys, I'm having some problems regarding the collision. 

I would like to ask is it possible to get a bool which indicating that the object collision status?

For example:

I need to know whether the object is colliding with something or not, if yes return TRUE else return FALSE.

How can I do this in Farseer?

Sep 15, 2010 at 3:47 PM

//Create two Fixtures, here is an example for a beverage object :) :

Fixture bodyBeverage;    

 bodyBeverage = FixtureFactory.CreateRectangle(Rc.GameHelpfulGlobals.Instance.physicsSimulator,Rc.ConvertUnits.ToSimUnits(beverageTexture.Width * scale.X), Rc.ConvertUnits.ToSimUnits(beverageTexture.Height * scale.Y), 10f);
 bodyBeverage.Body.IsStatic = false;
 bodyBeverage.Body.IgnoreGravity = true;
 bodyBeverage.Body.Position = Rc.ConvertUnits.ToSimUnits(new Vector2(position.X, position.Y));


bodyBeverage.CollisionGroup = (int)CollisionCategory.Cat9;
bodyBeverage.CollisionCategories = CollisionCategory.Cat9;
bodyBeverage.Body.UserData = "Beverage";


bodyBeverage.OnCollision += onCollision;
// add a 2nd fixture...
 ..... }
//  Now when the collision happens between the two fixtures the event onCollision gets called:
bool collision = false;

  public bool onCollision(Fixture geometry1, Fixture geometry2, FarseerPhysics.Dynamics.Contacts.Contact manifold)
        {
            if (geometry2.CollisionCategories == CollisionCategory.Cat2)
            {
                  // Collides with CollisionCategory 2
                  collision = true; //:)
	     return true;

}
return false;

}

 

Sep 15, 2010 at 4:11 PM
So I see your way is setting a boolean type of variable to true when both objects are colliding but where should I set the collision back to false if they are not colliding?
I dont think I can set the collision to false under the ELSE statement because the problem with the OnCollision is it only fired when both objects are colliding. Which means when both of them are not colliding it wont fired and then how am I suppose to set the collision to false?


Regards,
Cadrick


On Sep 15, 2010, at 11:47 PM, "Northwind877" <notifications@codeplex.com> wrote:

From: Northwind877

//Create two Fixtures, here is an example for a beverage object :) :

Fixture bodyBeverage;    

 bodyBeverage = FixtureFactory.CreateRectangle(Rc.GameHelpfulGlobals.Instance.physicsSimulator,Rc.ConvertUnits.ToSimUnits(beverageTexture.Width * scale.X), Rc.ConvertUnits.ToSimUnits(beverageTexture.Height * scale.Y), 10f);
 bodyBeverage.Body.IsStatic = false;
 bodyBeverage.Body.IgnoreGravity = true;
 bodyBeverage.Body.Position = Rc.ConvertUnits.ToSimUnits(new Vector2(position.X, position.Y));


bodyBeverage.CollisionGroup = (int)CollisionCategory.Cat9;
bodyBeverage.CollisionCategories = CollisionCategory.Cat9;
bodyBeverage.Body.UserData = "Beverage";


bodyBeverage.OnCollision += onCollision;
// add a 2nd fixture...
 ..... }
//  Now when the collision happens between the two fixtures the event onCollision gets called:
bool collision = false;

  public bool onCollision(Fixture geometry1, Fixture geometry2, FarseerPhysics.Dynamics.Contacts.Contact manifold)
        {
            if (geometry2.CollisionCategories == CollisionCategory.Cat2)
            {
                  // Collides with CollisionCategory 2
                  collision = true; //:)
	     return true;

}
return false;

}

Sep 15, 2010 at 4:31 PM

I am not sure if it works in 3.0 too, but you could try to set the boolean (collision) to false in your update method! (maybe at the end or before you update farseer)

Sep 15, 2010 at 7:53 PM

There's also an "OnSeparation" delegate that fires when two fixtures are no longer touching.

Even if there is a way to check "IsTouchingOtherObject" or something as part of fixture, it may be more expensive to ask that question

every frame than to do Northwind's suggestion of just checking a boolean you set when the delegate fires.