Jump to content
  • Advertisement
Sign in to follow this  
Dean1988

Collision Detection with Two Box Meshes.

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, Basically, i have my two bounding boxes sorted out, and all is how it should be, however nowi'm not sure exactly how to detect the collision, probably another stupid question :(
	void CalcAABBFromOBB(const D3DXVECTOR3 *obb,D3DXVECTOR3 *minB,D3DXVECTOR3 *maxB)
	{
		minB->x=maxB->x=obb[0].x;
		minB->y=maxB->y=obb[0].y;
		minB->z=maxB->z=obb[0].z;

		for (int i=1;i<8;i++)
		{
			if (obb.x < minB->x) minB->x=obb.x;
			if (obb.x > maxB->x) maxB->x=obb.x;
			if (obb.y < minB->y) minB->y=obb.y;
			if (obb.y > maxB->y) maxB->y=obb.y;
			if (obb.z < minB->z) minB->z=obb.z;
			if (obb.z > maxB->z) maxB->z=obb.z;
		}
	}

	void SetBoundingBox()
	{
		BYTE *pVerts = NULL;
		pMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVerts);
		D3DXComputeBoundingBox((D3DXVECTOR3*)pVerts, pMesh->GetNumVertices(), D3DXGetFVFVertexSize(pMesh->GetFVF()),  &minBounds, &maxBounds);
		pMesh->UnlockVertexBuffer();


		bBoxPoints[0] = D3DXVECTOR3(minBounds.x, minBounds.y, minBounds.z);
		bBoxPoints[1] = D3DXVECTOR3(maxBounds.x, minBounds.y, minBounds.z);
		bBoxPoints[2] = D3DXVECTOR3(minBounds.x, maxBounds.y, minBounds.z);
		bBoxPoints[3] = D3DXVECTOR3(maxBounds.x, maxBounds.y, minBounds.z);
		bBoxPoints[4] = D3DXVECTOR3(minBounds.x, minBounds.y, maxBounds.z);
		bBoxPoints[5] = D3DXVECTOR3(maxBounds.x, minBounds.y, maxBounds.z);
		bBoxPoints[6] = D3DXVECTOR3(minBounds.x, maxBounds.y, maxBounds.z);
		bBoxPoints[7] = D3DXVECTOR3(maxBounds.x, maxBounds.y, maxBounds.z);

		CalcAABBFromOBB((D3DXVECTOR3*)&bBoxPoints, &minBounds, &maxBounds);
	}

That's how i'm doing it, using the code from ToyMaker.info, but i'm not entirely sure it works as i hoped. Any ideas guys? Cheers...

Share this post


Link to post
Share on other sites
Advertisement
Check out the 3D Object Intersection page, here, at RealTimeRendering.com. It is basically a huge matrix of all the types of different algorithms that you could use. For example, it has links to 'ray vs ray', 'AABB vs plane', ect - whatever you need. Some of the code is hastily written, but it's fast and it works well.

Share this post


Link to post
Share on other sites
Cheers mate,

That did manage to baffle me alot more though.

http://www.toymaker.info/Games/html/collisions.html

That's what i've been reading through, and it says

If the max x position of A is less than the min x position of B they do not collide
If the min x position of A is greater than the max x position of B they do not collide
and the same goes for y and z

So as i've got my bounding box stuff already ( code in first post )
surely i can just do the checks?

And i'm guessing i'll be using minBounds.X/Y/Z and maxBounds.X/Y/Z?

Cheers,

Dean

Share this post


Link to post
Share on other sites
Just an observation, but your solution seems just so much more complicated than it has to be.

This is assuming that the boxes are not angled. Think of the two boxes as being in 2D. Select two axises, such as x and z, and test for a collision using min and max values of those rectangles. Then if and only if the boxes collide within that 2D area check for the y value collision. Use the max and min y points on the two boxes and then check for the y values being within each others range. Using this method you wouldn't actually have to specify a box by eight points.

Just a simple suggestion.

Share this post


Link to post
Share on other sites
Hi,

Cheers for your reply mate, i just sat down and thought about it and devised a very simple IF routine to check all the points correctly, and i must say, it works a dream :)

Here's the IF statement if anyones interested :)



if ( pColMesh->minBounds.z + pColMesh->worldPosition.z < this->maxBounds.z + this->worldPosition.z &&//Z
pColMesh->maxBounds.z + pColMesh->worldPosition.z > this->minBounds.z + this->worldPosition.z &&//Z
pColMesh->minBounds.y + pColMesh->worldPosition.y < this->maxBounds.y + this->worldPosition.y &&//Y
pColMesh->maxBounds.y + pColMesh->worldPosition.y > this->minBounds.y + this->worldPosition.y &&//Y
pColMesh->minBounds.x + pColMesh->worldPosition.x < this->maxBounds.x + this->worldPosition.x &&//Z
pColMesh->maxBounds.x + pColMesh->worldPosition.x > this->minBounds.x + this->worldPosition.x ) //Z
return 1;



Cheers for your help and patience people :)

Dean.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!