# Detect which Entity caused the collision

I have a 2D plane with two or more moving entities (polygons with an AABB). On collision both of them will go through a "collidesWith" Method, where I have access to both entities but only want to modify the other one to avoid that something is applied twice. For collision detection I am using a custom implementation of the SAT Algorithm.

The entities will only move in x or y direction not diagonal. (Though I would like to add diagonal, if there is a solution that works for diagonal as well)

What I want to achieve is:

1. a moves vertically and b moves into it, b should invert its movement
2. d moves horizontally and c moves into it, c should invert its movement

How do I approach this without a huge amount of if statements?

• To clarify, when the objects collide, only one of them is effected by the collision? (changes direction etc)
– benh
Commented Dec 13, 2016 at 15:32
• Yes, only the object which caused the collision. Commented Dec 13, 2016 at 15:38
• Okay i think i've worked it out, the one who caused the collision is the one with the movement vector in the direction of the other?
– benh
Commented Dec 13, 2016 at 15:46
• I'm surprised I did not notice that until now. Yes that should be correct. Commented Dec 13, 2016 at 15:48

All you need to do is check the x and y velocity of each entity when they collide, if the velocities are in the direction of the other block (say for instance if you're an entity where Vx is -1 and Vy is 0, and the entity you have collided with has a location that is to the left of you) you can determine that you caused the collision as you moved towards the other block. Once you have determined this you can simply multiple the appropriate velocity(x or y) by -1.

• I think this should solve it, not sure if I forgot any edge cases. I did not notice this for some reason, thanks! Commented Dec 13, 2016 at 15:55

If both objects are moving, neither one of them "caused" the collision any more than the other, so we should seek a better definition of how we would define which object should bounce.

In real-life physics, the bouncing would have a lot to do with momentum (mass * velocity), but in the two examples you have given, if we distill it down, you view the object that got "in the way" of the other as NOT being the "cause". More specifically, if you think of an object's "front" face as being the side facing the direction the object is moving, when a collision occurs you have stated a desired that the object whose front-face collides should be the one that bounces.

From a pseudo-code perspective, you could then think of it like this:

• Object A and Object B have been found to collide.
• Find the "front" face of Object A
• If the front face of Object A has done the collision, Object A bounces. (In this case, if you want a "head-on" collision to bounce both objects, you would also have to check the front-face of Object B.)
• If the front face of Object A did not collide, you can assume Object B bounces.

edit: was typing this at the same time as benh. His answer is more concise & doesn't wax philosophical about defining your collision, so I think it's better. As a side-note: his recommendation will cover everything I've mentioned, including head-on collisions.

• This would actually work well for polygons, but for now I'll try to do this only with the movement vectors. I think computing the front face can be quite complex, because the left front face could be completely inside the other object, which causes the bottom and top to be "causing" the collision as well. Commented Dec 13, 2016 at 16:09