This project has moved and is read-only. For the latest updates, please go here.

2 Collision Q's on: Collision categories IsCollide? Collision direction?

Jan 30, 2009 at 6:20 AM
Edited Jan 30, 2009 at 2:08 PM
I have two questions i would be very grateful if they could get answered.

1.  If I have two collision objects that are in the same category(assuming that this would result in them not bouncing of each other), could I use the OnCollide(IsSensor) function to detect a collision(penetration rather) between the two objects.   

2. If I have a circle colliding with another object(square for instance), is it possible to retrieve which Geom.Vertices(for lack of a better word) are currently colliding? I want to do this to get the direction of the coliding square relative to the circle. If it's to the right, left, top or bottom. The circle object 'might' never be rotated so that tracking which vertex of the circle is colliding with the square will also result in current relative direction of the square.

If both above are possible(hopefully relatively easy) I plan on using them together. So I'm trying to get a circle that doesn't bounce of other objects, but can track if there is a collision and which direction the collision is heading from.
Jan 30, 2009 at 10:38 AM
Well you might want to use a geom with the IsSensor property on for a geom that only detects collisions. As far as detecting which direction a collision is coming from, i'm not sure about that. You'll have to see what genbox or mattbetcher or one of the other people that have been hanging around this site lately has to say.
Jan 30, 2009 at 2:22 PM
The Geom class also have a Intersect method. It returns a Feature that contains the amount of penetration, position and the normal of the collision.

We have two Geom.Collide() methods. One of them takes in a point and check it in the Grid (by using Grid.Intersect() - same as Geom.Intersect()) and if the distance returned by the Feature is below 0, that means a collision has occurred.
The other Geom.Collide works exactly the same way, but it checks collisions between all the vertices of both the geometries (itself and the one you provided into the method).
Jan 31, 2009 at 6:08 PM
As for the collision direction, yes, we do have a way of detecting where it comes from.
In your OnCollision method, you have the 2 geometries and a contactlist. The one you need to focus on is the contactlist.
This list gives you all the contacts where a collision occurs. A contact is just a vector on a geometry where a collision occurred.


private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
    //Gives you the position in the world where the collision occurs.
    Vector2 position = contactList[0].Position;
    return true;

You might want to transform the world vector to a local vector (one that is relative to 0,0 on the geometry).

private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
    Vector2 worldContact = contactList[0].Position;
    Vector2 localVector;

    _rectangleGeom.TransformToLocalCoordinates(ref worldContact, out localVector);

    return true;

now the localVector variable contains the position of the collision (contact) relative to your geometry. Try it out.
Jan 31, 2009 at 11:36 PM
Thanks genbox - that really helped.  I basically had gotten as far as attempting to use a dot product with unit vector (1, 0), but was attempting the calculation with the contact normal instead of position. That calculation did not always produce what I thought it would. I had noticed that the contact normal was being copied from the Feature when I was looking at the source code - never occured to me that position was what I wanted.  I'll have to take another look at that.=).

 - 3Z0
Feb 1, 2009 at 4:57 AM
everything works really nice.. awsome tip.. is there anything you can do in the case of two collisions? would that be contactList[1] as apposed to contactList[0]?
How would i approach a multiple object collision event?

Feb 1, 2009 at 1:37 PM
A single collision can easily contain 2 contactpoints. Imagine a chair falling (legs down) to the ground. Each leg of the chair will have a contact point and thus 2 contacts will be created. (actually more depending on what you have MaxContactsToDetect set to in the physicssimulator)