Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dev578

problem with D3DXIntersect()

This topic is 5273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using D3DXIntersect() to update the camera position on my terrain. I have been messing around with it for quite a bit, and it seems to be working just fine. It it correctly setting the "BOOL pHit" parameter. To update the camera position, I defined the ray at 300 units above the camera, so I could then do 300 - "FLOAT pDist". pDist is supposed to be the distance on the ray until it intersects the triangle. However, it is returning a very large negative number for some reason. Anyone have any idea why it is doing this? -Dev578

Share this post


Link to post
Share on other sites
Advertisement
I had a similar problem and realized my ray and object were not in the same coordinate space. I basically needed to transform my ray into the mesh''s model space.

Share this post


Link to post
Share on other sites
is pDist a pointer to a float, or a float?

It sounds like you''re subtracting the pointer''s value (a 32-bit unsigned integer) from the float. Assuming that the integer isn''t being initialized, it''s probably something like 0xFFFFFFFF (4,294,967,295). That could explain your negative number.

Share this post


Link to post
Share on other sites
"I had a similar problem and realized my ray and object were not in the same coordinate space. I basically needed to transform my ray into the mesh's model space"

You are most likely right, I have been trying to figure out how to do that for awile and nothing seems to work. I am going to post my entire main loop, and hope someone can help me:

while (e_CanRun)
{
while ( PeekMessage(&e_msg, NULL, 0, 0, PM_REMOVE) )
{
TranslateMessage(&e_msg);
DispatchMessage(&e_msg);
}
g_dCurTime = timeGetTime();
g_fElpasedTime = (float)((g_dCurTime - g_dLastTime) * 0.001);
g_dLastTime = g_dCurTime;
e_DeviceD3D->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE( 0.0f, 0.0f, 0.0f, 1.0f), 1.0f, 0 );
D3DXMATRIX matRotation;
//
// Get keyboard input...
//
unsigned char keys[256];
GetKeyboardState( keys );
D3DXVECTOR3 tmpLook = g_vLook;
D3DXVECTOR3 tmpRight = g_vRight;
// Up Arrow Key - View moves forward
if( keys[VK_UP] & 0x80 )
{
g_vEye -= tmpLook*-g_fMoveSpeed*g_fElpasedTime;
}
// Down Arrow Key - View moves backward
if( keys[VK_DOWN] & 0x80 )
g_vEye += (tmpLook*-g_fMoveSpeed)*g_fElpasedTime;
// Left Arrow Key - View side-steps or strafes to the left
if( keys[VK_LEFT] & 0x80 )
//g_vEye -= (tmpRight*g_fMoveSpeed)*g_fElpasedTime;
{
D3DXMatrixRotationAxis( &matRotation, &D3DXVECTOR3(0,-1,0), D3DXToRadian((float)/*nYDiff */ 2.0f));
D3DXVec3TransformCoord( &g_vLook, &g_vLook, &matRotation );
D3DXVec3TransformCoord( &g_vUp, &g_vUp, &matRotation );
}
// Right Arrow Key - View side-steps or strafes to the right
if( keys[VK_RIGHT] & 0x80 )
{
//g_vEye += (tmpRight*g_fMoveSpeed)*g_fElpasedTime;
D3DXMatrixRotationAxis( &matRotation, &D3DXVECTOR3(0,1,0), D3DXToRadian((float)/*nYDiff */ 2.0f));
D3DXVec3TransformCoord( &g_vLook, &g_vLook, &matRotation );
D3DXVec3TransformCoord( &g_vUp, &g_vUp, &matRotation );
}
// Home Key - View elevates up
if( keys[VK_HOME] & 0x80 )
g_vEye.y += g_fMoveSpeed*g_fElpasedTime;
// End Key - View elevates down
if( keys[VK_END] & 0x80 )
g_vEye.y -= g_fMoveSpeed*g_fElpasedTime;
D3DXMATRIXA16 view;
D3DXMatrixIdentity( &view );
D3DXVec3Normalize( &g_vLook, &g_vLook );
D3DXVec3Cross( &g_vRight, &g_vUp, &g_vLook );
D3DXVec3Normalize( &g_vRight, &g_vRight );
D3DXVec3Cross( &g_vUp, &g_vLook, &g_vRight );
D3DXVec3Normalize( &g_vUp, &g_vUp );
view._11 = g_vRight.x;
view._12 = g_vUp.x;
view._13 = g_vLook.x;
view._14 = 0.0f;

view._21 = g_vRight.y;
view._22 = g_vUp.y;
view._23 = g_vLook.y;
view._24 = 0.0f;

view._31 = g_vRight.z;
view._32 = g_vUp.z;
view._33 = g_vLook.z;
view._34 = 0.0f;

view._41 = -D3DXVec3Dot( &g_vEye, &g_vRight );
view._42 = -D3DXVec3Dot( &g_vEye, &g_vUp );
view._43 = -D3DXVec3Dot( &g_vEye, &g_vLook );
view._44 = 1.0f;

////////////////////
//Problem///////////
////////////////////
if ((g_vEye.x > 0)&&(g_vEye.x < terrain.GetSize())&&(g_vEye.z > 0)&&(g_vEye.z{ //if we are in the terrain
D3DXVECTOR3 RayPos(g_vEye.x,-300.0f,g_vEye.z);
D3DXVECTOR3 RayDir(0.0f,-1.0f,0.0f);
BOOL Hit; DWORD FaceIndex; FLOAT pU,pV,pDist;
D3DXIntersect(NULL,&RayPos,&RayDir,&Hit,&FaceIndex,&pU,&pV,&pDist,NULL,NULL);
if(Hit)
{
//it is irrelevant what we do here, because pDist= -107374176 ???
}
}
/////////////////////
//Problem////////////
/////////////////////

e_DeviceD3D->SetTransform( D3DTS_VIEW, &view );
D3DXMATRIXA16 matWorld;
D3DXMatrixScaling( &matWorld, 3.0f, 3.0f, 3.0f );
e_DeviceD3D->SetTransform( D3DTS_WORLD, &matWorld );
e_DeviceD3D->BeginScene();

D3DXMatrixScaling( &matWorld, 1.0f, 1.0f, 1.0f );
e_DeviceD3D->SetTransform( D3DTS_WORLD, &matWorld );

D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 150.0f );
e_DeviceD3D->SetTransform( D3DTS_PROJECTION, &matProj );

e_DeviceD3D->SetStreamSource( 0, e_VB, 0, sizeof(TexVertex) );
e_DeviceD3D->SetIndices( g_pIndexBuffer );
e_DeviceD3D->SetFVF( TexVertex::FVF_Flags );
e_DeviceD3D->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 1600, 0, 3000 );

e_DeviceD3D->SetStreamSource( 0, e_Axis, 0, sizeof(Vertex) );
e_DeviceD3D->SetFVF( Vertex::FVF_Flags );
e_DeviceD3D->DrawPrimitive( D3DPT_LINELIST, 0, 3 );

e_DeviceD3D->EndScene();
// show on screen
e_DeviceD3D->Present( 0, 0, 0, 0 );



If anyone could tell me what I am screwing up / how to fix it, that would be great. Any help is appreciated.

-Dev578

No one knows? Guess I am not the only one.

[edited by - Dev578 on June 6, 2004 6:53:34 PM]

Share this post


Link to post
Share on other sites
use D3DXVec3TransformCoord() to multiply a vector by a given matrix.


Kam1kaz3

Share this post


Link to post
Share on other sites
Hi - Sorry, I didn't read your code (the text gives me a headache) but I am posting you my function I created that tests an arbitrary ray against a model located in my 3D world. Because the model is not at the origin I need to convert the ray in to the objects model space. I hope it is of some use:

BOOL CWorldEnvironment::CheckIntersectObject( ID3DXMesh* Mesh, D3DXMATRIX* ObjMat, float XStart, float YStart, float ZStart, float XEnd, float YEnd, float ZEnd, float* Length )
{
BOOL Hit;
float u, v, Dist;
float XDiff, YDiff, ZDiff, Size;
DWORD FaceIndex;
D3DXVECTOR3 vecDir;
D3DXVECTOR3 vecOrig;
D3DXMATRIX matInv;

// Start and End variables are the start and end of the

// arbitrary ray

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

vecOrig.x = XStart;
vecOrig.y = YStart;
vecOrig.z = ZStart;

vecDir.x = XDiff;
vecDir.y = YDiff;
vecDir.z = ZDiff;

// Invert the objects world position matrix

// and use it to transform vectors

D3DXMatrixInverse( &matInv, NULL, ObjMat );
D3DXVec3TransformCoord( &vecOrig, &vecOrig, &matInv );
D3DXVec3TransformNormal( &vecDir, &vecDir, &matInv );

D3DXVec3Normalize(&vecDir, &vecDir);

D3DXIntersect( Mesh, &vecOrig, &vecDir, &Hit, &FaceIndex, &u, &v, &Dist, NULL, NULL);

return Hit;
}


Edit: Removed unnecessary code

[edited by - __Daedalus__ on June 8, 2004 3:11:36 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!