Detect collision - run code ?

Apr 7, 2010 at 6:55 PM

I'm confused how I'm supposed to detect a collision, and subsequently run my code.

for example an enemy colliding with a projectile loses 1 health point.


I just don't understand how to check for a collision between two objects. I tried using .OnCollision(projectile, enemy, ??)

but i dont know how to use contact lists or if this is even the right way to do it.


help would be great.

Apr 7, 2010 at 7:38 PM

You should probably check the samples but collision is an event and you hook the event like this:

yourGeom.OnCollision += OnCollision;

And then the collision function would look like this

private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)

  //Your code here!
Good luck!
Apr 7, 2010 at 7:59 PM

sweet! few questions though


Do I need to create a new OnCollision for each of my objects or can I just have it in my main game class and then just add that to each geometry?


Second, how do I detect a specific collision?

Would I write, for example

if(geom1 == playergeom && geom2 == firegeom)



(sry, im not at home so i can't trial and error)

Apr 7, 2010 at 8:31 PM

I think its up to you how you wish to structure your code.

In your example you could add this to your player.

In each Body or Geom you have a Tag that you can set. Ex playerGeom.Tag="Player"; enemyGeom.Tag="Enemy";

Then you can check the collision in your event

 //Do something

Apr 7, 2010 at 8:45 PM
Edited Apr 7, 2010 at 10:35 PM

oh ok, but does it matter which geom (geom1/2) i ask?

what if the event is fired with geom1 being a projectile and geom2 being the player, but i ask for geom1 == player and geom2 == projectile? will it still work?



alright i just ended up doing


if(geom1 == player && geom2 == projectile

|| geom1 == projectile && geom2 == player)


thanks for the help!