# Collision

## Recommended Posts

Jazonxyz    138
how can I make an object stop when it collides with another object like on the legend of zelda when he hit a tree? What do I need if I already know the X axis and the Y axis and the objects' width and height?

##### Share on other sites
Kest    547
You need to represent all of your objects with rectangles during collision.

If A's left side is more than B's right side, then the objects DID NOT collide.
OR
If A's bottom side is higher than B's top side, then the objects DID NOT collide.
OR
If A's right side is less than B's left side, then the objects DID NOT collide.
OR
If A's top side is lower than B's bottom side, then the objects DID NOT collide.

If all of these were false, then the objects DID collide.
If any single one of them were true, the objects DID NOT collide.

Here's one general way to stop movement into collisions:
Backup = CurrentPositionMoveObject()CheckCollision( some_object )if collision{   Goal = CurrentPosition   CurrentPosition.x = Backup.x   CheckCollision( some_object )   if collision   {      CurrentPosition.x = Goal.x      CurrentPosition.y = Backup.y      CheckCollision( some_object )      if collision         CurrentPosition.x = Backup.x;   }}

- Severely edited

[Edited by - Kest on May 10, 2007 9:14:09 PM]