How to test if there is a collision (should be easy?)

Topics: Developer Forum
Feb 7, 2009 at 7:24 PM
This should be rather simple, but I don't seem to be able to get my head around it.

I have a "ship" with "health" and I'm trying to make the health go down based on impact speed. I have a few asteroids:

asteroid[i].geom
asteroid[i].body

the ship is

_shipGeom
_shipBody
float ship.hp  //ship health

what do I put in the if statement to detect the asteroid - ship collision? And is there any way to get the collision impact strength?


for (int i = 0; i < asteroidList.Length; i++)
                {
                        if (?????????)
                        ship.hp -= someSortOfValue;
                }

Coordinator
Feb 7, 2009 at 8:25 PM
Well, there are several ways actually. One way is looking at the LinearVelocity of the asteroid. Another would be to look at the amount of  penetration the collision causes. When a collision occurs, the 2 geometries are actually overlapping and the amount of overlapping is called penetration.
If I remember correctly, the amount of penetration is contained in the Contact class and is called Separation.

What you need to do is to use the OnCollision event in the Geom class. There is no need to check each asteroid against the ship of they are far away form the ship. If you use the OnCollision event, this will be done automatically for you.
Inside the event, you can access the ContactList with all the contacts (a contact is the connection/contact between 2 geometries) and there you have the amount of penetration. If you decide to use that.
Feb 7, 2009 at 9:29 PM
Thank you so much for your quick response, but I don't seem to be able to find the ContactList?
I see inside the Geom class where OnCollision created:

public CollisionEventHandler OnCollision;

but how exactly to I get the Geom the ship collided with?


Coordinator
Feb 7, 2009 at 9:41 PM
Inside your game you register/subscribe to the OnCollision event like this:

ship.Geom.OnCollision += MyOnCollisionMethod;

then you create a method like this:

private bool MyOnCollisionMethod(Geom geom1, Geom geom2, ContactList list)
{
return true;
}

Inside that method you are able to reach the 2 geometries that collide. On of them is going to be the ship and the other is whatever it's colliding with. (Could be an asteroid..)
Feb 7, 2009 at 10:18 PM
Ah, that's what I was looking for.
Thank you, I got everything working now.