# How do I determine which side of the player has collided with an object?

I have some static bodies (platforms) and a dynamic body (the player) in my world. The collision between them works great, but I would like to know which side of the player (rectangle) collides with the platform. How do I get this information?

I know this question was already answered but I needed this in one of my games as well and I found the answer in the Farseer forums and modified it for my engine. You will find the original link in the comments (Shameless link to engine http://code.google.com/p/axiosengine/ - perhaps you will find more interesting things there) :

namespace Axios.Engine.Extensions
{
public enum CollisionDirection
{
Right,
Left,
Top,
Bottom
}
public static class AxiosExtensions_Contact
{
/// http://farseerphysics.codeplex.com/discussions/281783
/// <summary>
/// Returns the direction that the collision happened.
/// Should be used in the event OnAfterCollision
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public static CollisionDirection Direction(this Contact c)
{
CollisionDirection direction;
// Work out collision direction
Vector2 colNorm = c.Manifold.LocalNormal;
if (Math.Abs(colNorm.X) > Math.Abs(colNorm.Y))
{
// X direction is dominant
if (colNorm.X > 0)
direction = CollisionDirection.Right;
else
direction = CollisionDirection.Left;
}
else
{
// Y direction is dominant
if (colNorm.Y > 0)
direction = CollisionDirection.Bottom;
else
direction = CollisionDirection.Top;

}

return direction;
}
}
}


To use it:

 switch (contact.Direction())
{
case CollisionDirection.Left:
//ect

• I've had issues (in Farseer) with using the Contact's normal. As I said in my answer, you should probably use the WorldManifold. – Richard Marskell - Drackir Jun 8 '12 at 20:56
• I have not had a chance to do extensive testing on it, but it seems to work great on a rectangle shaped body. You may be right though - it might give wrong answers on more complex shapes - or even one that was created by a decomposer algorithm. – Natalie Adams Jun 9 '12 at 15:26
• Are you talking about in Farseer or in your physics library? Because I doubt it's going to be the same. – Richard Marskell - Drackir Jun 9 '12 at 20:55
• I don't think I understand that question. In my engine the only physics engine I use is Farseer and that is what I was referencing in my comment. Would you like for me to include a more indepth example in my answer? – Natalie Adams Jun 16 '12 at 22:44
• You said the engine you use is called "axios engine" and you modified an example from Farseer for that. – Richard Marskell - Drackir Jun 17 '12 at 13:49

There's a few ways I can think of to do this:

1. You could make the player into several fixtures representing each side. Then you could either give each one an OnCollision event, or give each one an ID in the UserData to figure out which one is the "collider" in a shared OnCollision method. This was good for me because I could set the side and top fixtures to have zero friction so the player doesn't stick to platforms.
2. If you're stuff is all axis aligned rectangles, you could calculate it based on their positions and dimensions. This is probably not a great idea for more complex situations since it can get difficult to follow and Farseer already has to calculate this anyways.
3. I think you can check the Contact's world manifold's normal. In your OnContact event, there are three parameters: two Fixtures and a Contact. Contact has a method called GetWorldManifold which has an out parameter called normal. This is a Vector2 that, I think (someone please correct me if I'm wrong), represents the angle of contact.
• Is it possible to get the information how much farseer rotates the object etc? this soloutions look very complicated :D – Michael May 18 '12 at 14:16
• @Michael - I'm not sure about that. I think you can only get the points of contact and the normal, but I could be wrong. – Richard Marskell - Drackir May 18 '12 at 16:52
• Knows anyone else if there is a option in farseer? – Michael May 18 '12 at 18:09
• @Michael - I don't think that the object will have rotated at all on contact. The contact happens when the first point of one Fixture overlaps another. After that happens, the bodies, depending on the collision, may rotate, but it will be a different value each iteration of the Update. So, what you could do is store the body's rotation from the initial collision event OnCollision and then on other iterations of the loop, you can subtract the current rotation of the body from that value to figure out how much it has been rotated. This wouldn't really work if you have multiple collisions. – Richard Marskell - Drackir May 19 '12 at 12:06
• Yeah so thanks for your answer I am going to try the way with the fixtures on every side. I am a beginner so I haven't worked with fixtures yet. If you have a good tutorial I would be glad! – Michael May 19 '12 at 12:20