Hi guys i am doing a 3D Pacman game in DX9 and c++, i have got all my models in a AABB 'Axis Aligned Bounding Box', but now i want to get the test collision function working properly.
my Bounding Box code is as follows: -
boundingVolumes.h
#ifndef _BOUNDING_VOLUMES_
#define _BOUNDING_VOLUMES_
#include <d3dx9.h>
#include <string>
#include <limits>
// Colors
const D3DXCOLOR BLACK( D3DCOLOR_XRGB( 0, 0, 0) );
const D3DXCOLOR BLUE ( D3DCOLOR_XRGB( 0, 0, 255) );
class CBoundingVolumes
{
public:
CBoundingVolumes(void);
virtual ~CBoundingVolumes(void);
//////////////////
// Struct
/////////////////
struct sBoundingBox
{
sBoundingBox();
D3DXVECTOR3 _min;
D3DXVECTOR3 _max;
};
sBoundingBox s_BoundingBox;
D3DMATERIAL9 WHITE_MTRL;
D3DMATERIAL9 RED_MTRL;
D3DMATERIAL9 GREEN_MTRL;
D3DMATERIAL9 BLUE_MTRL;
D3DMATERIAL9 YELLOW_MTRL;
LPD3DXMESH BoxMesh;
/////////////////////
// Functions
////////////////////
D3DMATERIAL9 BoundingVolumes_InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p);
void BoundingVolumes_CreateBox(LPDIRECT3DDEVICE9 device, ID3DXMesh* mesh);
void BoundingVolumes_RenderBox(LPDIRECT3DDEVICE9 device);
bool BoundingVolumes_Box_Compute(ID3DXMesh* mesh, sBoundingBox* box);
private:
};
#endif // _BOUNDING_VOLUMES_
boundingVolumes.cpp
#include "boundingvolumes.h"
#include <vector>
CBoundingVolumes::CBoundingVolumes(void)
{
BLUE_MTRL = BoundingVolumes_InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
BoxMesh = 0;
}
CBoundingVolumes::~CBoundingVolumes(void)
{
BoxMesh->Release();
}
D3DMATERIAL9 CBoundingVolumes::BoundingVolumes_InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
{
D3DMATERIAL9 mtrl;
mtrl.Ambient = a;
mtrl.Diffuse = d;
mtrl.Specular = s;
mtrl.Emissive = e;
mtrl.Power = p;
return mtrl;
}
CBoundingVolumes::sBoundingBox::sBoundingBox()
{
// infinite small
_min.x = INFINITY;
_min.y = INFINITY;
_min.z = INFINITY;
_max.x = -INFINITY;
_max.y = -INFINITY;
_max.z = -INFINITY;
}
void CBoundingVolumes::BoundingVolumes_RenderBox(LPDIRECT3DDEVICE9 device)
{
// Draw bounding volume in Blue
D3DMATERIAL9 color = BLUE_MTRL;
color.Diffuse.a = 0.10f; // 10% opacity
device->SetMaterial(&color);
device->SetTexture(0, 0); // disable texture
device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
BoxMesh->DrawSubset(0);
}
bool CBoundingVolumes::BoundingVolumes_Box_Compute(ID3DXMesh* mesh, sBoundingBox* box)
{
HRESULT hr = 0;
BYTE* pVertices = NULL;
hr=mesh->LockVertexBuffer(0, (void**)&pVertices);
if (FAILED(hr))
return FALSE;
hr = D3DXComputeBoundingBox( //D3D Function that calc's the bounding box for a mesh
(D3DXVECTOR3*)pVertices, //Vertex position component is stored 1st in the vertex structure we are using
mesh->GetNumVertices(), //Get the number of vertices's in the mesh
D3DXGetFVFVertexSize(mesh->GetFVF()), //function how many bytes there are between each vertex
&box->_min, //Return the min and max points of the bounding box
&box->_max);
mesh->UnlockVertexBuffer();
if( FAILED(hr) )
return false;
return true;
}
void CBoundingVolumes::BoundingVolumes_CreateBox(LPDIRECT3DDEVICE9 device, ID3DXMesh* mesh)
{
//******************** Compute Bounding Box
BoundingVolumes_Box_Compute(mesh, &s_BoundingBox);
D3DXCreateBox(
device,
s_BoundingBox._max.x - s_BoundingBox._min.x,
s_BoundingBox._max.y - s_BoundingBox._min.y,
s_BoundingBox._max.z - s_BoundingBox._min.z,
&BoxMesh,
0);
}
right and now to create the bounding boxes for my models i do
//in pacman.h
#include "../BoundingVolume/boundingvolumes.h"
CBoundingVolumes *pBoundingBox;
// in pacman.cpp
pBoundingBox = NULL;
pBoundingBox = new CBoundingVolumes();
pBoundingBox->BoundingVolumes_CreateBox(device,pModel->mesh);
pBoundingBox->BoundingVolumes_RenderBox(device);
Up till now all the above is working brill, its this bit i need help with :)
so now in my Game.cpp update function i want to check for collisions.
here is where i create my game objects and update everything i.e.
CPacMan *pPacman; // Pointer to a PacMan object
So we come to the bit that is bugging the hell out off me :(
i know that the function to check for 2 coliding BoundingBoxes is:
bool Game::Check_For_Collisions(pBox1,pBox2)
{
if ( pBox1._min.x > pBox2._max.x || pBox2._min.x > pBox1._max.x
||pBox1._min.y > pBox2._max.y || pBox2._min.y > pBox1._max.y
||pBox1._min.z > pBox2._max.z || pBox2._min.z > pBox1._max.z )
{
return false;
}
return true;
}
but how do i declare
'bool Check_For_Collisions(? pBox1,? pBox2);' properly what do i add for ?.
and when i want to call it
Check_For_Collisions(pPacman->?????,pPill->?????);
[Edited by - Prog101 on December 16, 2006 11:21:29 AM]