Okay. Im using DirectX. I got the bounding box workong fine for one model.
Basically i have a class called meshLoader that loads in a model. Then i have two classes, enemyShip and spaceShip, both inherited from meshModel. meshModel has all the code to do with drawing and the bounding box that is the same for both.
So i load up a 2 meshes and make an instance of both spaceShip and enemyShip.
The problem im having is that the bounding box that is for my spaceShip, is being drawn around my enemy ship and i cant see why.
Im using a fx file t odo the drawing of the box. so in a setup function at the start, after ive created both instances i have
theSpaceShip->CreateFX();
theSpaceShip->initBoundingBox();
In the main loop i call a mehtod to draw the ships, same for both ships, just using different matrices.
void spaceShip::updateMatrices()
{
D3DXMatrixTranslation(&spaceShipPosMat, spaceShipPos.x, spaceShipPos.y, spaceShipPos.z);//Translates spaceShip to coordinates defined.
D3DXMatrixScaling(&spaceShipScale, 0.10f,0.10f,0.10f);//Scales the spaceShip to defined size.
worldMat = spaceShipScale*rotationMat*initialRotationMat*spaceShipPosMat;
setWorldMat(worldMat);
}
void spaceShip::showShip()
{
updateMatrices();
show();
}
Then i draw the bounding box
void drawBBox(D3DXMATRIX View, D3DXMATRIX projection)
{
theSpaceShip->setupBBox();
theSpaceShip->drawBBox(View, projection);
enemyShip1->setupBBox();
enemyShip1->drawBBox(View, projection);
}
void meshModel::setupBBox()
{
D3DVERTEXELEMENT9 decl[2]= {
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
D3DDECL_END()
};
Device->CreateVertexDeclaration(decl, &vertexDeclaration);
//bounds[0] = bBox.getBounds();
float halfWidth = 0.0f;
D3DXVECTOR3 maxBound = bBox.getMaxBound();
D3DXVECTOR3 minBound = bBox.getMinBound();
D3DXVECTOR3 points[18];
//Is the first line not just minbound.x, minbound.y, minbound.z
points[0] = D3DXVECTOR3(minBound.x, minBound.y, minBound.z);
points[1] = D3DXVECTOR3(minBound.x, minBound.y, maxBound.z);
points[2] = D3DXVECTOR3(maxBound.x, minBound.y, maxBound.z);
points[3] = D3DXVECTOR3(maxBound.x, minBound.y, minBound.z);
points[4] = D3DXVECTOR3(minBound.x, minBound.y, minBound.z);
points[5] = D3DXVECTOR3(minBound.x, maxBound.y, minBound.z);
points[6] = D3DXVECTOR3(minBound.x, maxBound.y, maxBound.z);
points[7] = D3DXVECTOR3(maxBound.x, maxBound.y, maxBound.z);
points[8] = D3DXVECTOR3(maxBound.x, maxBound.y, minBound.z);
points[9] = D3DXVECTOR3(minBound.x, maxBound.y, minBound.z);
points[10] = D3DXVECTOR3(minBound.x, minBound.y, minBound.z);
points[11] = D3DXVECTOR3(minBound.x, maxBound.y, minBound.z);
points[12] = D3DXVECTOR3(maxBound.x, minBound.y, minBound.z);
points[13] = D3DXVECTOR3(maxBound.x, maxBound.y, minBound.z);
points[14] = D3DXVECTOR3(maxBound.x, minBound.y, maxBound.z);
points[15] = D3DXVECTOR3(maxBound.x, maxBound.y, maxBound.z);
points[16] = D3DXVECTOR3(minBound.x, minBound.y, maxBound.z);
points[17] = D3DXVECTOR3(minBound.x, maxBound.y, maxBound.z);
Device->CreateVertexBuffer( 18 * sizeof(D3DXVECTOR3), 0, 0,D3DPOOL_MANAGED,&pVB, NULL );
VOID* pVertices;
pVB->Lock( 0, 18*sizeof(D3DXVECTOR3), (void**)&pVertices, 0 );
memcpy( pVertices, points, 18*sizeof(D3DXVECTOR3));
pVB->Unlock();
}
void meshModel::drawBBox(D3DXMATRIX View, D3DXMATRIX proj, D3DXMATRIX spaceShipScale, D3DXMATRIX spaceShipPosMat)
{
Device->SetVertexDeclaration(vertexDeclaration);
Device->SetStreamSource( 0, pVB, 0, sizeof(D3DXVECTOR3) );
mFX->SetTechnique("Simple");
mFX->SetMatrix(mhWorld,&(spaceShipScale*spaceShipPosMat));
mFX->SetMatrix(mhView,&(View));
mFX->SetMatrix(mhProjection,&(proj));
D3DXVECTOR4 col = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f);
mFX->SetValue(mhColour,&(col) ,sizeof(D3DXVECTOR4));
UINT passes = 1;
mFX->Begin(&passes,0);
Device->DrawPrimitive(D3DPT_LINESTRIP, 0, 4);
Device->DrawPrimitive(D3DPT_LINESTRIP, 5, 4);
Device->DrawPrimitive(D3DPT_LINESTRIP, 10, 4);
mFX->EndPass();
mFX->End();
}
But for some reason, the bounding box of the spaceShip is being drawn around the enemy. Its strange aswell, cos when i rotate the player spaceship the bounding box is rotating. So its just the position that is wrong.