• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Enerjak

bounding box collision detection not working right....

4 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.....

0

Share this post


Link to post
Share on other sites

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;
	}
}
0

Share this post


Link to post
Share on other sites

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 
                        }

0

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  
Followers 0