Sign in to follow this  
Jazonxyz

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 this post


Link to post
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 = CurrentPosition
MoveObject()
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this