Jump to content


Need help with ray tracing collision detection


  • You cannot reply to this topic
2 replies to this topic

#1 Luna

    New Member

  • Members
  • Pip
  • 2 posts

Posted 10 December 2008 - 11: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 :)

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4968 posts
  • LocationBellevue, WA

Posted 10 December 2008 - 05:45 PM

Please use [code]...[/code] tags to post code.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Luna

    New Member

  • Members
  • Pip
  • 2 posts

Posted 12 December 2008 - 04:41 PM

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users