Check if an enemy collided with a bullet

Topics: User Forum
Oct 6, 2012 at 11:44 AM

Okay so as you can guess i am trying to make a little shooter game to try and learn to use farseer. I have an array of bullets that are fired by the player. I want to check when any of the bullet bodied hit the enemy body and then i will set its alive flag to false. I am very new to farseer and its a really fun engine all round.



Oct 6, 2012 at 8:31 PM



  So what you can do is attach an event to your main bodies (player shootable enemis) like this 

 body.OnCollision += new OnCollisionEventHandler(awew_OnCollision);=
and just check if the collided body is a bullet .There might be a better was of doing it ,but this is the first thing ,that comes to mind.
Oct 8, 2012 at 1:20 PM
Edited Oct 8, 2012 at 1:23 PM

This may sound like a stupid question but i understand the first part:

body.OnCollision += new OnCollisionEventHandler(awew_OnCollision);

However i am not sure how to check if the collided body is a bullet.

Do i need to create fixtures or do i just use the two bodies.

Oct 10, 2012 at 5:32 AM
Edited Oct 10, 2012 at 5:33 AM

Two ideas come to mind (though i am also fairly new to farseer).

First, why dont you add the OnCollision to the body of the bullet:

bullet.OnCollision += new OnCollisionEventHandler(awew_OnCollision);


Second, if you want to add the logic to the main body's OnCollision, do something like this:


body.OnCollision += new OnCollisionEventHandler(awew_OnCollision);

private bool awew_OnCollision(Fixture f1, Fixture f2, Contact c)
     if ((f1.Body.IsBullet) || (f2.Body.IsBullet))
        //Add logic here
    return false;