I am at the point where I need collision detection. This is something I have done before with success, but for some reason I can't seem to get it to work correctly now.... I have my "display" meshes as a single mesh and I display them by subset. The collision meshes are done the same way, but are low polygon. I load the collision mesh and create a buffer of normals (D3DXVECTOR3) for each face:
//load the collision mesh
FileName="media/meshes/Spaces/collisions/";
FileName+=SpaceFileNames[fl].CollisionMeshName;
collisionMesh=ELoadStaticMesh(FileName,GE->d3ddev);
MeshObject[collisionMesh].ppMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT,(DWORD*)0xffffffff,NULL,NULL,NULL);
//*************** create and fill the normals buffer
LPVOID IB;
MeshObject[collisionMesh].ppMesh->LockIndexBuffer(D3DLOCK_READONLY,&IB);
short *ib=(short*)IB;
DWORD numFaces=MeshObject[collisionMesh].ppMesh->GetNumFaces();
LPVOID VB;
MeshObject[collisionMesh].ppMesh->LockVertexBuffer(D3DLOCK_READONLY,&VB);
E_D3DVERTEX *vb=(E_D3DVERTEX*)VB;
faceNormals=new D3DXVECTOR3[numFaces];
for (DWORD i=0;i<numFaces;i++){
faceNormals[i]=vb[ib[i*3]].n;
faceNormals[i]+=vb[ib[i*3+1]].n;
faceNormals[i]+=vb[ib[i*3+2]].n;
D3DXVec3Normalize(&faceNormals[i],&faceNormals[i]);
}
MeshObject[collisionMesh].ppMesh->UnlockVertexBuffer();
MeshObject[collisionMesh].ppMesh->UnlockIndexBuffer();
When I want to detect collision, I determine where the player is and loop all the objects within the area. The player's location is made relative to each of the objects and transformed inversely by the object's rotation matrix.
void ACTORCONTROLLER::CheckCollisions(GAMEENGINE *GE){
if (Actors[0].InMesh>-1){//make sure
if ((Actors[0].InSection==2)||(Actors[0].InSection==3)){//make sure it's the one I want to test
//loop all the objects in the section
for (UINT i=0;i<PlayerShipInterior.Section[Actors[0].InSection].Objects.size();i++){
int objIndex=PlayerShipInterior.Section[Actors[0].InSection].Objects[i].ObjIndex;
D3DXVECTOR3 rayPos=Actors[0].relativeCollisionLoc;//this is non-rotated location relative to the section
D3DXVECTOR3 rayDir=Actors[0].LookDir;
D3DXMATRIX rot=PlayerShipInterior.Section[Actors[0].InSection].Objects[i].matrix;
D3DXVECTOR3 objLoc;
objLoc.x=PlayerShipInterior.Section[Actors[0].InSection].Objects[i].matrix(3,0);
objLoc.y=PlayerShipInterior.Section[Actors[0].InSection].Objects[i].matrix(3,1);
objLoc.z=PlayerShipInterior.Section[Actors[0].InSection].Objects[i].matrix(3,2);
rot(3,0)=rot(3,1)=rot(3,2)=0;
D3DXMatrixInverse(&rot,0,&rot);
rayPos-=objLoc;
D3DXVec3TransformCoord(&rayPos,&rayPos,&rot);
D3DXVec3TransformCoord(&rayDir,&rayDir,&rot);
D3DXMatrixInverse(&rot,0,&rot);
BOOL hit;
DWORD faceIndex;
float hitDist;
//**************** check the direction of travel
D3DXIntersectSubset(MeshObject[Actors[0].InMesh].ppMesh,(DWORD)objIndex,&rayPos,&rayDir,&hit,&faceIndex,NULL,NULL,&hitDist,NULL,NULL);
if (hit){
if (hitDist<0.5f){
//rotate the face normal
D3DXVECTOR3 fn=PlayerShipInterior.faceNormals[faceIndex];
rayPos+=fn*(0.5f-hitDist);
}
}
D3DXVec3TransformCoord(&Actors[0].relativeCollisionLoc,&rayPos,&rot);
Actors[0].relativeCollisionLoc+=objLoc;
}
GE->MyCamera.fpPosition=Actors[0].relativeCollisionLoc+PlayerShipInterior.Section[Actors[0].InSection].pos;
}
}
}
Here is a video link of what I am seeing:
What's wrong with my code?????