PDA

View Full Version : Need help with ray tracing collision detection


Luna
12-10-2008, 03:25 AM
Hello, I am trying to create ray tracing collision between mesh but I am having a problem.

It seems to work perfectly to all directions but right.

In game I have those two function to check left and right, they are just the opposite unless I am blind :)

if(KeyboardDevice->GetKeyState(KEY_LEFT) == TRUE)
{
if(pMesh->CheckIntersect(pWorldPosition->GetXPos(), pWorldPosition->GetYPos(), pWorldPosition->GetZPos(), pWorldPosition->GetXPos() - 0.01f, pWorldPosition->GetYPos(), pWorldPosition->GetZPos(), NULL, pWorldPosition2) == false)
pWorldPosition->MoveRel(-0.01f,0,0);

}

if(KeyboardDevice->GetKeyState(KEY_RIGHT) == TRUE)
{
if(pMesh->CheckIntersect(pWorldPosition->GetXPos(), pWorldPosition->GetYPos(), pWorldPosition->GetZPos(), pWorldPosition->GetXPos() + 0.01f, pWorldPosition->GetYPos(), pWorldPosition->GetZPos(), NULL, pWorldPosition2) == false)
pWorldPosition->MoveRel(0.01f,0,0);
}


The CheckIntersect is this:

Prototype:

BOOL CheckIntersect(float XStart, float YStart, float ZStart,
float XEnd, float YEnd, float ZEnd,
float *Length, cSSWorldPosition* WorldTransformationMat);


Function:

BOOL cSSMesh::CheckIntersect(float XStart, float YStart, float ZStart,
float XEnd, float YEnd, float ZEnd,
float *Length, cSSWorldPosition* WorldTransformationMat)
{
LPD3DXMESH tMesh = NULL;

m_pFirstMesh->MeshData.pMesh->CloneMeshFVF(D3DXMESH_MANAGED,
D3DFVF_XYZ, GraphicsDevice->GetDeviceCOM(), &tMesh);

BYTE* pVertices=NULL;

if(FAILED(tMesh->LockVertexBuffer(0, (LPVOID*)&pVertices)))
return FALSE;

D3DXVECTOR3 *vPtr=(D3DXVECTOR3 *) pVertices;


for (unsigned long i = 0; i < tMesh->GetNumVertices(); i++)
{
D3DXVECTOR4 transformVec;

D3DXVec3Transform(&transformVec, &vPtr[i], WorldTransformationMat->GetMatrix());



vPtr[i] = (D3DXVECTOR3)transformVec;

}

tMesh->UnlockVertexBuffer();



BOOL Hit;
float u, v, Dist;
float XDiff, YDiff, ZDiff, Size;
DWORD FaceIndex;
D3DXVECTOR3 vecDir;

XDiff = XEnd - XStart;
YDiff = YEnd - YStart;
ZDiff = ZEnd - ZStart;

D3DXVec3Normalize(&vecDir, &D3DXVECTOR3(XDiff, YDiff, ZDiff));
D3DXIntersect(tMesh,
&D3DXVECTOR3(XStart,YStart,ZStart), &vecDir,
&Hit, &FaceIndex, &u, &v, &Dist, NULL, NULL);

tMesh->Release();

if(Hit == TRUE) {
Size = (float)sqrt(XDiff*XDiff+YDiff*YDiff+ZDiff*ZDiff);
if(Dist > Size)
Hit = FALSE;
else {
if(Length != NULL)
*Length = Dist;
}
}

return Hit;
}


Thanks :)

Reedbeta
12-10-2008, 09:45 AM
Please use ... tags to post code.

Luna
12-12-2008, 08:41 AM
After checking several more .x files, I believe the problem is with the .x file and not the code (that and I can't see any problem with the code!)

I am using Pandasoft exporter, can anyone recommend me of another exporter from 3Ds Studio to .x which also handles animations exporting and bones?