Archived

This topic is now archived and is closed to further replies.

flashlaser

Me again

Recommended Posts

Hi, My recent attempt at collision was successful but not sucessful enough. I kept getting invisible collisions. I was using the D3DXIntersect function so I had no control over how the collision detection worked. I decided to write my own and but the code doesn't work for some reason. Could someone have a looks over it in case I missed something. CarCorner is the original location of my four point bounding box around the car and it never changes.
void Car::checkCollision(LPD3DXMESH mesh) {
	iscollision = 0;
	dist = 0;
	D3DXMATRIX 	matRots;
	D3DXMATRIX 	matTrans;
	D3DXMATRIX	matWorlds;
	D3DXVECTOR3 p1,p2,p3;
	D3DXMatrixRotationY(&matRots,olddir);
    my_vertex* vertex;
	VERTEX *pVertices;
	WORD* pIndices;
	DWORD dwNumFaces=mesh->GetNumFaces();
	D3DXMatrixTranslation(&matTrans,dest.x,dest.y,dest.z);

	D3DXMatrixMultiply(&matWorlds,&matRots,&matTrans);

	DWORD i = 0;
	mesh->LockIndexBuffer(D3DLOCK_READONLY,(BYTE **) &pIndices);
	mesh->LockVertexBuffer(D3DLOCK_READONLY,(BYTE **) &pVertices);
	for (DWORD l = 0; l<4; l++)  // Run through the 4 corners
    {
		for (DWORD m=0;m 0) {
						D3DXVECTOR3 IP = pos + CarCorners[l]*t;
						if(CheckPointInTriangle(IP,p0,p1,p2)) {
							iscollision = 1;

							dist = 1;

					}
				}
			}
		}
	}
	mesh->UnlockIndexBuffer();
	mesh->UnlockVertexBuffer();
	if ( iscollision == 1) {
							// do something later if collision occurs

	}
	else {

		pos = dest;
		dir = olddir;

	}

}

// ----------------------------------------------------------------------
// Name  : intersectRayPlane()
// Input : rOrigin - origin of ray in world space
//         rVector - vector describing direction of ray in world space
//         pOrigin - Origin of plane 
//         pNormal - Normal to plane
// Notes : Normalized directional vectors expected
// Return: distance to plane in world units, -1 if no intersection.
// -----------------------------------------------------------------------  
float intersectRayPlane(D3DXVECTOR3 rOrigin, D3DXVECTOR3 rVector, D3DXVECTOR3 pNormal, float planeD) {
  
float cosAlpha,deltaD;

cosAlpha = D3DXVec3Dot(&rVector,&pNormal);

if (cosAlpha==0) return -1.0f;

deltaD = planeD - D3DXVec3Dot(&rOrigin,&pNormal);

return (deltaD/cosAlpha);

}

BOOL CheckPointInTriangle(D3DXVECTOR3 point, D3DXVECTOR3 a, D3DXVECTOR3 b, D3DXVECTOR3 c) {
  
  double total_angles = 0.0f;
       
  // make the 3 vectors
  D3DXVECTOR3 v1 = point-a;
  D3DXVECTOR3 v2 = point-b;
  D3DXVECTOR3 v3 = point-c;
  
  D3DXVec3Normalize(&v1,&v1);
  D3DXVec3Normalize(&v2,&v2);
  D3DXVec3Normalize(&v3,&v3);

  total_angles += acos(D3DXVec3Dot(&v1,&v2));   
  total_angles += acos(D3DXVec3Dot(&v2,&v3));
  total_angles += acos(D3DXVec3Dot(&v3,&v1)); 
     
  if (fabs(total_angles-2*PI) <= 0.005)
   return (TRUE);
     
  return(FALSE);
}

inline BOOL isZeroVector(D3DXVECTOR3 &v) {
 if ((v.x == 0.0f) && (v.y == 0.0f) && (v.z == 0.0f))
   return TRUE;
   
  return FALSE;	
}
   
Edited by - flashlaser on October 31, 2001 6:10:56 AM Edited by - flashlaser on October 31, 2001 6:11:58 AM

Share this post


Link to post
Share on other sites
Try to make your question a bit more specific. That''s an awful lot of code to review to search for a general, unspecified problem. Try to provide a small subset of your code that will require less time to analyze.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites