Skip to main content
gamedev.net
🔒 Locked

Collision Algorithm Problem

Started by Oni Sephiroth 3 replies 1.4k views
Original Post
Oni Sephiroth
Oni Sephiroth
I've been playing around with collision detection and I'm having nothing but problems. This is the code I'm using..
bool HVSTEVENT::collision(float xA1, float yA1, float xA2, float yA2, float xB1, float yB1, float xB2, float yB2)
{
	if (  ((xA2 < xB1) || (xB1 > xA2) || (yA1 < yB2) || (yA2 > yB1))   )
		return false; //no possible collisions
	else 
	{
		//all of the above are FALSE, there may be a collision.
		
		//upper left corner collision
		if ((xA2 > xB1) && (xA2 < xB2))
			if ((yA2 > yB2) && (yA2 < yB1))
				return true;

		//bottom left corner collision
		if ((xA2 > xB1) && (xA2 < xB2))
			if (yA1 > yB2 && yA1 < yB1)
				return true;

		//upper right corner collision
		if ((xA1 > xB1) && (xA1 < xB2))
			if ((yA2 > yB2) && (yA2 < yB1))
				return true;

		//bottom right corner collision
		if ((xA1 > xB1) && (xA1 < xB2))
			if ((yA1 > yB2) && (yA1 < yB1))
				return true;

		//overlapping
		if ((xA1 == xB1) && (xA2 == xB2) && (yA1 == yB1) && (yA2 == yB2))
			return true;

		//no collision
		return false;

	}
}

where x,y is the upper left corner and x2,y2 is the bottom right corner of the objects. A is object one, B is object 2. What I've noticed from the debugger is that the only time the main if statement evaluates to false is when there's a collision between the top right of objectA and the bottom left of objectB, which for some reason makes the first if statement in the else block go to the first return true. Can someone go through this quickly or point me to a working algorithm?
hunpro
hunpro
There is a lil mixup in the beginning, the fist 2 condition is the same.
if (  ((xA2 < xB1) || (xB1 > xA2) || (yA1 < yB2) || (yA2 > yB1))   )


The whole thing is:
if( xA1>xB2 || xA2<xB1 || yA1>yB2 || yA2<yB1) return false;return true; // everything else is true.
alnite
alnite
Quote:
Original post by Oni Sephiroth
I've been playing around with collision detection and I'm having nothing but problems. This is the code I'm using..


*** Source Snippet Removed ***

where x,y is the upper left corner and x2,y2 is the bottom right corner of the objects. A is object one, B is object 2. What I've noticed from the debugger is that the only time the main if statement evaluates to false is when there's a collision between the top right of objectA and the bottom left of objectB, which for some reason makes the first if statement in the else block go to the first return true. Can someone go through this quickly or point me to a working algorithm?


You got it confused.

(xA2 < xB1) || (xB1 > xA2)
This expression is redundant. If xA2 < xB1, then xB1 > xA2. You are only switching the operands.

Here's the correct one:
return !(xA2 < xB1 || xA1 > xB2 || yA1 > yB2 || yA2 < yB1);
Derakon
Derakon
The first thing I do when I run into problems with this kind of code is draw a little diagram. Two rectangles, label their points, and then go comparison by comparison to make things as plain as possible. Heck, I get a lot more use out of my tablet as a scratchpad than I do for actual art. :) But a piece of scratch paper and a pencil work just as well.

Trying to visualize all this will just give you a headache. Draw it out.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
Oni Sephiroth
Oni Sephiroth
Much better, I appreciate the help! I also noticed I had a mistake in one of my y2 coordinates. I added the objects height to the y instead of subtracing it, causing it to be above the sprite. OpenGL coordinates can confuse me sometimes, heheh.

Quote:

The first thing I do when I run into problems with this kind of code is draw a little diagram. Two rectangles, label their points, and then go comparison by comparison to make things as plain as possible. Heck, I get a lot more use out of my tablet as a scratchpad than I do for actual art. :) But a piece of scratch paper and a pencil work just as well.

Trying to visualize all this will just give you a headache. Draw it out.


Haha, that's really funny, because I have a notepad of sticky notes on my desk for visualizing things when I program. I was using two of the notes as objects and moving them around, trying to get it working. It's really useful, I found it helpful as well when I had to write a Linked List class.

Topic Locked

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

Sign in to reply to this topic.