Sign in to follow this  
Enerjak

bounding box collision detection not working right....

Recommended Posts

Enerjak    277

As you can tell from the title, bounding box collision detection is not working out for me....*sigh* Here's my problem:

 

bool EntityManager::checkCollision(Entity* ent1, Entity* ent2)
{
	bool cholided = false;
	AABB* box1 = ent1->getBoundingBox();
	AABB* box2 = ent2->getBoundingBox();
	
	if(box1->getMin().getX() > box2->getMax().getX()) return cholided;
	if(box1->getMax().getX() < box2->getMin().getX()) return cholided;
	if(box1->getMin().getY() > box2->getMax().getY()) return cholided;
	if(box1->getMax().getY() < box2->getMin().getY()) return cholided;
	if(box1->getMin().getZ() > box2->getMax().getZ()) return cholided;
	if(box1->getMax().getZ() < box2->getMin().getZ()) return cholided;

	
	return (cholided == true);
}

 

this code seems to be OK since I got this from a programming math book. link: http://gamemath.com/ now I'm not sure if there's a problem with my code in the bounding box, but i don't think so since it renders fine. Maybe I'm doing something weird? anyways, the book also talked about checking a box against a moving box (a player for instance) and gave this code.

 

double EntityManager::checkCollisionAgainstMovingEnt(Entity* movingEnt, Entity* staticEnt, Vector3 & d)
{
	// large number to return if no intersection
	const double noIntesection = 1e30f;
	AABB* otherAABB = staticEnt->getBoundingBox();
	AABB* movingBox = movingEnt->getBoundingBox();
	// interval for time consideration
	double dEnter = 0.0;
	double dLeave = 1.0;

	if(d.getX() == 0.0f)
	{
		if((otherAABB->getMin().getX() >= movingBox->getMax().getX()) || 
			(otherAABB->getMax().getX() <= movingBox->getMax().getX()))
		{
			return noIntesection;
		}
		else
		{
			// divide once

			bool divByZero = (d.getX() != 0);

			double oneOverD = 1.0 / d.getX();

			double xEnter = (otherAABB->getMin().getX() - movingBox->getMin().getX()) * oneOverD;
			double xLeave = (otherAABB->getMax().getX() - movingBox->getMax().getX()) * oneOverD;

			if(xEnter > xLeave)
				swap(xEnter,xLeave);

			if(xEnter > dEnter) dEnter = xEnter;
			if(xLeave < dLeave) dLeave = xLeave;

			if(dEnter > dLeave)
				return noIntesection;
		}


	}
	// check y
	if(d.getY() == 0.0f)
	{
		if((otherAABB->getMin().getY() >= movingBox->getMax().getY()) ||
			otherAABB->getMax().getY() <= movingBox->getMin().getY())
		{
			return noIntesection;
		}
		else 
		{
			// divide by one on the y axis.
			double oneOverD = 1.0 / d.getY();

			double yEnter = (otherAABB->getMin().getY() - movingBox->getMax().getY()) * oneOverD;
			double yLeave = (otherAABB->getMax().getY() - movingBox->getMin().getY()) * oneOverD;

			if(yEnter > yLeave)
				swap(yEnter,yLeave);

			if(yEnter > dEnter) dEnter = yEnter;
			if(yLeave < dLeave) dLeave = yLeave;

			if(dEnter > dLeave)
				return noIntesection;
		}
	}

	// check z-axis
	if(d.getZ() == 0.0f)
	{
		if((otherAABB->getMin().getZ() >= movingBox->getMax().getZ()) ||
			otherAABB->getMax().getZ() <= movingBox->getMin().getZ())
		{
			return noIntesection;
		}
		else 
		{
			// divide by one on the y axis.
			double oneOverD = 1.0 / d.getZ();

			double zEnter = (otherAABB->getMin().getZ() - movingBox->getMax().getZ()) * oneOverD;
			double zLeave = (otherAABB->getMax().getZ() - movingBox->getMin().getZ()) * oneOverD;

			if(zEnter > zLeave)
				swap(zEnter,zLeave);

			if(zEnter > dEnter) dEnter = zEnter;
			if(zLeave < dLeave) dLeave = zLeave;

			if(dEnter > dLeave)
				return noIntesection;
		}


	}
	return dEnter;
}

 

here's a screen shot also: http://puu.sh/2zKM0

 

I'm not really sure why collision detection doesn't work for me....here's how i'm using it:

 

	for(int i = 0; i < 10; i++) {
		
		
	//	if(pack->getPosition().getX() > pallets[i]->getPosition().getX())
	//	{
	//		pallets[i]->setColor(0.5,1,0,1);
	//		pallets[i]->draw();

	//		pack->setColor(1,1,0,1);
	//		pack->setDebugColor(1,0.5,1,0.5);
	//		pack->draw();
	//	}
		
	//	if(pack->getPosition().getY() > pallets[i]->getPosition().getY())
	//	{
	//		pallets[i]->draw();
	//		pack->draw();
	//	}
		bool collide = entMgr->checkCollision(pack,wall);
		char buffer3[256];
		sprintf_s(buffer3,256,"Collision: %d",collide);
		Vector3 d;
		d.setVector(2,1,2);
		
		double interval = entMgr->checkCollisionAgainstMovingEnt(pack,wall,d);
		char buffer4[256];
		interval += 0.04;
		sprintf_s(buffer4,256,"interval: %.2f",interval);
		if(interval <= d.getX() && interval <= d.getY() && interval <= d.getZ())
		{
			pack->setColor(interval,1,0,1);
		}
		output(0,300,buffer4);
		output(0,500,buffer3);
		if(collide)
		{
			pack->setColor(1,0,0,0.5);
			AABB* pbox = pack->getBoundingBox();
			AABB* wbox = wall->getBoundingBox();

			bool contains = pbox->containsPoint(0,0,0);
			bool contains2 = wbox->containsPoint(-100,100,0);
			char buffer[256];
			sprintf_s(buffer,256,"collision: %d",collide);
			output(100,100,buffer);
		}
	}

 

I don't know maybe I need to think this through. this is for a school project keep in mine I'm not asking for you to do it for me and the collision detection is all me, the teacher just wanted a simple pack man.

Share this post


Link to post
Share on other sites
__SKYe    1784

In the function

bool EntityManager::checkCollision(Entity* ent1, Entity* ent2)

 

 

In the case they intersect, you return



return (cholided == true);

 

bt what you're doing is comparing cholided to true, and return the result of that.

Cholided is always set to false, so that comparison will always return false, since chomparison is not true.

 

Use



return true;

 

instead.

 

Also, you wouldn't need the variable cholided. Just return false in every case except the last.

Edited by __SKYe

Share this post


Link to post
Share on other sites
Enerjak    277

that still didn't do anything. shouldn't the collision detection work whether an AABB is static or moving? this is getting annoying for me.....

Share this post


Link to post
Share on other sites
steg    390

Hi,

 

Don't know if this will help, this is java AABB I use, should be simple to port to C++.

 

package com.voxby.game;

public class CollisionLibrary {
	public static boolean testAABBAABB(final AABB box1, final AABB box2) {
		   if (Math.abs(box1.center.x - box2.center.x) > (box1.r[0] + box2.r[0])) return false;
		   if (Math.abs(box1.center.y - box2.center.y) > (box1.r[1] + box2.r[1])) return false;
		   if (Math.abs(box1.center.z - box2.center.z) > (box1.r[2] + box2.r[2])) return false;
		   return true;
		}
	
	public static float sqDistPointAABB(final Vector p, final AABB aabb) {
		   float sqDist = 0.0f;
		   float v;
		   float minX, minY, minZ, maxX, maxY, maxZ;
		      
		   // get the minX, maxX, minY, maxY and minZ, maxZ points of the AABB
		   minX = aabb.center.x - aabb.r[0];
		   maxX = aabb.center.x + aabb.r[0];
		      
		   minY = aabb.center.y - aabb.r[1];
		   maxY = aabb.center.y + aabb.r[1];
		      
		   minZ = aabb.center.z - aabb.r[2];
		   maxZ = aabb.center.z + aabb.r[2];
		      
		   // test the bounds against the points X axis
		   v = p.x;
		      
		   if (v < minX) sqDist += (minX - v) * (minX - v);
		   if (v > maxX) sqDist += (v - maxX) * (v - maxX);
		      
		   // test the bounds against the points Y axis
		   v = p.y;
		      
		   if (v < minY) sqDist += (minY - v) * (minY - v);
		   if (v > maxY) sqDist += (v - maxY) * (v - maxY);
		      
		   // test the bounds against the points Z axis
		   v = p.z;
		      
		   if (v < minZ) sqDist += (minZ - v) * (minZ - v);
		   if (v > maxZ) sqDist += (v - maxZ) * (v - maxZ);
		      
		   return sqDist;
		}
	
		public static boolean testCircleAABB(final Circle circle, final AABB box) {
		   // get the squared distance between circle center and the AABB
		   float sqDist = sqDistPointAABB(circle.center, box);
		   float r = circle.radius;
		      
		   return sqDist <= r * r;
		}
	
}

package com.voxby.game;

public class AABB {
	public Vector center;
	public float r[];

	public AABB(final float width, final float height, final float depth) {
		center = new Vector();
		r = new float[3];
		r[0] = width * 0.5f;
		r[1] = height * 0.5f;
		r[2] = depth * 0.5f;
	}

	public void update(final Vector position) {
		center.x = position.x;
		center.y = position.y;
		center.z = position.z;
	}
}

package com.voxby.game;

public class Vector {
	public float x;
	public float y;
	public float z;

	public Vector() {
		x = 0.0f;
		y = 0.0f;
		z = 0.0f;
	}

	// returns the (squared) distance between this Vector and another
	public float distSQ(final Vector vec) {
		float distX = x - vec.x;
		float distY = y - vec.y;
		float distZ = z - vec.z;
		return distX * distX + distY * distY + distZ * distZ;
	}
}

Share this post


Link to post
Share on other sites
steg    390

Oh and to use:

 

Vector playerPosition = new Vector();
			playerPosition.x = Math.abs(v.x);  // get player X,Y,Z
			playerPosition.y = Math.abs(v.y);
			playerPosition.z = Math.abs(v.z);
			playerSphere.update(playerPosition, 1.0f);

			AABB voxel = new AABB(1f, 1f, 1f); // width, height, depth
			Vector voxelPosition = new Vector();
			voxelPosition.x = Math.abs(bx);  // get object X,Y,Z
			voxelPosition.y = Math.abs(by);
			voxelPosition.z = Math.abs(bz);
			voxel.update(voxelPosition);

			if (CollisionLibrary.testCircleAABB(playerSphere, voxel)) {
// do whatever 
                        }

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