Collision Won't Work...Please HELP!

Started by
3 comments, last by VladWorks 19 years, 8 months ago
I am using a Collision detection such as:

BOOL cGame_ver1::IntersectEnable(                                                     float XStart, float YStart, float ZStart,                     float XEnd,   float YEnd,   float ZEnd)
{
  sMesh *MeshPtr;
  BOOL  Hit;
  float u, v, Dist;
  float XDiff, YDiff, ZDiff, Size;
  DWORD FaceIndex;
  D3DXVECTOR3 vecDir;

  if((MeshPtr = m_TerrainMesh.GetParentMesh()) == NULL)
    return FALSE;  // No polygons hit

  XDiff = XEnd - XStart;
  YDiff = YEnd - YStart;
  ZDiff = ZEnd - ZStart;
  Size  = (float)sqrt(XDiff*XDiff+YDiff*YDiff+ZDiff*ZDiff);
  D3DXVec3Normalize(&vecDir, &D3DXVECTOR3(XDiff, YDiff, ZDiff));

  while(MeshPtr != NULL) {
    D3DXIntersect(MeshPtr->m_Mesh, &D3DXVECTOR3   / (XStart,YStart,ZStart), &vecDir, &Hit,            /
&FaceIndex, &u, &v, &Dist, NULL, NULL);

    if(Hit == TRUE) {
      if(Dist <= Size)
        return TRUE;  // Hit a polygon
    }

    MeshPtr = MeshPtr->m_Next;
  }

  return FALSE;  // No polygons hit
}


Well And when I finaly made a model in MilkShape and loaded up I coulden't hit it :(:(:(:( Please help me...
"C" is like "C++" after you fail it 2 times!
Advertisement
Also what modeling tools do you use (for your levels) to check for collision with D3DXIntersect(...);

*EDIT* Could anyone with the knowlege of this please help?
"C" is like "C++" after you fail it 2 times!
I have only skim read your code but is your ray in model space? For each ray mesh collision you need to transform the ray into the local model space for the mesh - perhaps you are doing this already?
------------------------See my games programming site at: www.toymaker.info
As stated, you need to transform your ray in to the object space of the model you are testing. To do this you will need the inverse of your model's world transformation matrix. Your function may look something like this:

BOOL cGame_ver1::IntersectEnable( float XStart, float YStart, float ZStart, float XEnd, float YEnd, float ZEnd ){	sMesh *MeshPtr;	BOOL  Hit;	float u, v, Dist;	float XDiff, YDiff, ZDiff, Size;	DWORD FaceIndex;		if((MeshPtr = m_TerrainMesh.GetParentMesh()) == NULL)		return FALSE;	while( MeshPtr != NULL ) {		D3DXVECTOR3 vecRayOrigin;		D3DXVECTOR3 vecRayDirection;		D3DXMATRIX*	ObjectMatrix;		D3DXMATRIX	matInverse;		vecRayOrigin.x = XStart;		vecRayOrigin.y = YStart;		vecRayOrigin.z = ZStart;		vecRayDirection.x = XEnd - XStart;		vecRayDirection.y = YEnd - YStart;		vecRayDirection.z = ZEnd - ZStart;			// Get the mesh's world transformation matrix		// If the mesh is centred around the origin then use an identity matrix		// For example :		// ObjectMatrix = MeshPtr->GetWorldTransformation()		// Transform ray in to mesh object space		D3DXMatrixInverse( &matInverse, NULL, ObjectMatrix );		D3DXVec3TransformCoord( &vecRayOrigin, &vecRayOrigin, &matInverse );		D3DXVec3TransformNormal( &vecRayDirection, &vecRayDirection, &matInverse );    		D3DXVec3Normalize( &vecRayDirection, &vecRayDirection );		D3DXIntersect( MeshPtr->m_Mesh, &vecRayOrigin, &vecRayDirection, &Hit, &FaceIndex, &u, &v, &Dist, NULL, NULL );		if(Hit == TRUE) 		{			Size  = (float)sqrt(XDiff*XDiff+YDiff*YDiff+ZDiff*ZDiff);						if( Dist < Size )				return TRUE;  // Hit a polygon		}		MeshPtr = MeshPtr->m_Next;	}	return FALSE;  // No polygons hit}


Great Worked like a dream :)
"C" is like "C++" after you fail it 2 times!

This topic is closed to new replies.

Advertisement