Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rectangle Collision

Started by walsh06 May 23, 2012 at 5:52 PM 1 replies 1k views
Original Post
walsh06
walsh06
I am working on a simple game at the moment and Im currentyly trying to do the collision. The collision is done by the usual, creat two rectangles and check if they intersect. My question is, is there a way to tell which sides of the rectangles are intersecting?? As in to know which direction one sprite is moving into the other.
dAND3h
dAND3h
Well, surely doing checks such as:

if(rect1.y + rect1.height <= rect2.y && rect1.Intersects(rect2))

Then in this instance rect1 is colliding to the top of rect2. I could be wrong though, these things can sometimes be deceptively simple smile.png

Another approach would be to have 1 large bounding rectangle, and 4 smaller rectangles representing his head/rhs/lhs/feet. The large rectangle would be a quick check to see if anything is likely to collide, then depending on which smaller rectangle it collides with, you make the appropriate reaction to it.

These smaller rectangle idea could be helpful for things like walking on a platform, you only want "Gravity" to stop if his feet are collding with something etc.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.