Jump to content
  • Advertisement
Sign in to follow this  
Hawkblood

Collision detection problem

This topic is 2080 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

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

Share this post


Link to post
Share on other sites
Advertisement

Wow! Now I feel stupid. It was the mesh the whole time. I ruled it out as a possibility early on, but somewhere along the way, I must have optimized some of the faces out of existence. rolleyes.gif

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!