Jump to content
  • Advertisement
Sign in to follow this  
CodeBoy504

2d collision detection

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having problems setting up a decent collision detection system for my 2d side scroller game. It is not the actual collision detection I am having problems with but more the collision response. I am using only AABB but I think I need to find which side of the box I am colliding with to handle it appropriately. The way I am doing it now involves setting the position back to the old one like this... update(entity): entity.old_position := entity.current_position modify entity.current_position based on entity.velocity and other factors if Colliding(entity) then entity.current_position := entity.old_position the problem with this is that the object (player to be more specific) may be constantly colliding with the ground tile due to gravity and resetting the position does not allow the character to walk and move around horizontally. I thought if I could set just the Y position or just the X position that would work better but I need to know which side it is colliding with to do this correctly. This seems like a lot more work for 2d collision detection then it should be, am I making this too complicated or is there an easier way to handle this?

Share this post


Link to post
Share on other sites
Advertisement
Don't you have a deltaX and deltaY of the moving object? From this you can tell which side of the rectangle the moving object is colliding on.

If deltaX is positive (moving to the right), then that means the object is colliding with the left side of the tile. If deltaX is negative (moving to the left), then the object is colliding with the right side of the tile. The same goes for deltaY.

Share this post


Link to post
Share on other sites
Resetting the position is the common way to do things, but the more effective way is to set up a simple action-reaction system where the objects add the negative difference between the two boxes to the object. With this system, objects can freely slide along the thing they're hitting. Not sure how understandable that was :P

EDIT:

Here's a picture:


In this system, you need to find X, then add -X to the player object (or just subtract X)

Note that the name X has nothing to do with the XY coordinate system; its just a variable letter I used randomly.

You should probably use the Velocity of the object to determine which coordinate to test for.

Once this system is in place, the objects can slide around on each other freely with no problems.


A simpler solution would be to test for the ground directly underneath the player's feet, and if its there, allow the player to move without resetting the position (and don't fall either)

Share this post


Link to post
Share on other sites
Quote:
Original post by alnite
Don't you have a deltaX and deltaY of the moving object? From this you can tell which side of the rectangle the moving object is colliding on.

If deltaX is positive (moving to the right), then that means the object is colliding with the left side of the tile. If deltaX is negative (moving to the left), then the object is colliding with the right side of the tile. The same goes for deltaY.

this would work if the characters could only move in 4 directions but deltaX and deltaY (assuming they are velocity components) can both be positive or negative or both positive and negative so I can't use those alone to determine the side it hits.

Quote:
In this system, you need to find X, then add -X to the player object (or just subtract X)

Note that the name X has nothing to do with the XY coordinate system; its just a variable letter I used randomly.

You should probably use the Velocity of the object to determine which coordinate to test for.

Once this system is in place, the objects can slide around on each other freely with no problems.
i used something like this and it seems to be working pretty well. I think my troubles were more in finding the side the object is colliding with but i fixed it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!