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
}
Collision Won't Work...Please HELP!
I am using a Collision detection such as:
Well And when I finaly made a model in MilkShape and loaded up I coulden't hit it :(:(:(:(
Please help me...
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?
*EDIT* Could anyone with the knowlege of this please help?
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?
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}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement