Sign in to follow this  
VladWorks

Collision Won't Work...Please HELP!

Recommended Posts

VladWorks    122
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...

Share this post


Link to post
Share on other sites
VladWorks    122
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?

Share this post


Link to post
Share on other sites
Trip99    577
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?

Share this post


Link to post
Share on other sites
__Daedalus__    480
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
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this