#### Archived

This topic is now archived and is closed to further replies.

# 2D collision detection - beyond testing

This topic is 5982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m working on a top-view 2D action game. Thanks to plenty of articles online, I know how to test two rectangles to see if they intersect, and I''ve implemented that. However, I stuck 50 entities in a room and had them run around, and found that occasionally entities will (for example) collide on the left side of another entity, and magically pop over to the that entity''s right side. Here''s how my movement function (which moves one entity) works: 1. Save the entity''s position, for determining collision direction. 2. Move the entity. 3. Clip the entity against the walls of the map. 4. Test collisions with tiles. If a collision is found, place the entity on the edge of the tile it collided with. (e.g., if I hit a solid tile from the right, place me right up against the right side of the tile.) Use the previous position (stored in step 1) to determine whether the entity collided horizontally or vertically. (note that entities can move diagonally...) 5. Test collisions with other entities. Go through a list of entities that are reasonably close to this one, and test collisions. If any are found, as above, determine whether they collided horizontally or vertically and move the entity appropriately. When there are 1 or 2 entities in the room, this works fine. But when I flood the room, I get occasional problems with bizarre movement, sometimes 30-200 pixels in the wrong direction. This happens the most when entities collide with more than one other tile or entity in steps 4 and 5, or when an entity moves diagonally. Can anyone help? Is this enough information?

##### Share on other sites
Without seeing your code, I would think that the problem arises because you''re not specifying which object you''re colliding with.

For example:

If an object(A) collides with two other objects(B and C) at exactly the same time, colliding with B from above, and with the C from below, the program might get confused. It might, being a stupid computer, place the object(A) on the right hand side of the object(C) which it collided with from below.

That was very confusing, sorry.

The way I do it:

I store the previous coordinates in a temporary variable, and if I detect a collision, I simply move the object back to where it was before it collided. You could also change the velocity of the object, if you wanted a bounce effect.

I hope that helps. Sorry about the confusion though...

Jonoxon

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633271
• Total Posts
3011160
• ### Who's Online (See full list)

There are no registered users currently online

×