Two rectangles colliding

Started by
13 comments, last by Stroppy Katamari 11 years, 4 months ago
The simplest method is to 1st move the object in the X-axis, check if it's intersects anything; if so, you know if u hit it on left or right (based on velocity), and you should move it back along the x-axies.

Then, move it on the y-axis, and check if it collided, and, if so, you know if it hit top or bottom based on Y velocity.

The obvious downside is you do 2 checks, but it's probably not a problem unless you have tons of objects you are checking against. If so, you should do some spatial hashing, but don't worry about that unless you have to.

Good luck.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
Sounds like a working solution. But what would happen if this test is performed when they are intersecting already?

But what would happen if this test is performed when they are intersecting already?


How can that happen, and why is it any different from any other collision test check? If you've written your code right, you should never enter a new movement loop when your character is already intersecting with something else, unless it's meant to be.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

It can happen if you have a moving object that's driving collision detection and a moving object that's not. I ran into that problem in my breakout clone - the ball drives collision, but the paddle can also move (and thus come to intersect the ball).

I solved that by adding a property to the paddle that caused it to cease being collidable for a few frames after collision. There was no other reasonable response in that case since the paddle does not react to the ball physically. In terms of objects that follow the normal rules of physics, though, the answer is simply to run testing for all objects as they move.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Well my so called "solution" was limited to work with one single situation and the code is not reusable.
I want a global method to check this, so I have looked a bit closer at Stroppy Katamaris post.
With all due respect, your text is cryptic. Why is velocity needed? Old R2? timex/timey?
Could you please clarify your post?
If you just have the locations of two overlapping rectangles, there is no way to tell accurately whether they collided vertically or horizontally. You have to know where they came from, in other words, where they were at last frame. Current position minus velocity gives you that last position, so you don't have to store the last position separately.

I have revised the post. It was indeed cryptic but should be a lot easier to follow now. I also made an error with a formula that is now fixed. Please follow the logic of the numbered steps, trying to understand each one before moving to the next, and ask for help if you get stuck at some specific part.

This topic is closed to new replies.

Advertisement