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 :)
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 :)