• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Getting Intersected Triangle

11 posts in this topic

I'm triangle to use raycasting to get the intersected triangle, here is the code that I'm using:

 

LPD3DXMESH pMesh = GetMesh();
pMesh->CloneMeshFVF( D3DXMESH_MANAGED, pMesh->GetFVF(), d3ddev, &pMesh );

LPDIRECT3DVERTEXBUFFER9 pVB;
LPDIRECT3DINDEXBUFFER9 pIB;

pMesh->GetVertexBuffer( &pVB );
pMesh->GetIndexBuffer( &pIB );

WORD* pIndices;
D3DVERTEX* pVertices;

// Lock
pIB->Lock( 0, 0, ( void** )&pIndices, 0 );
pVB->Lock( 0, 0, ( void** )&pVertices, 0 );

BOOL pHit;
DWORD pFaceIndex;
FLOAT pU, pV, pDist;
D3DXIntersect(pMesh, &rayFrom, &rayTo, &pHit, &pFaceIndex, &pU, &pV, &pDist, NULL, NULL);
if (pHit)
{
    DWORD dwNumFaces = pMesh->GetNumFaces();
    for(DWORD i = 0; i < dwNumFaces; i++)
    {
        D3DXVECTOR3 v0 = pVertices[pIndices[3 * i + 0]].p;
        D3DXVECTOR3 v1 = pVertices[pIndices[3 * i + 1]].p;
        D3DXVECTOR3 v2 = pVertices[pIndices[3 * i + 2]].p;


        if ( D3DXIntersectTri(&v0, &v1, &v2, &rayFrom, &rayTo, &pU, &pV, &pDist) )
        {
             // Intersected triangle found
        }
    }
}

// Unlock
pMesh->UnlockVertexBuffer();
pMesh->UnlockIndexBuffer();
 
Why the above code doesn't work as expected?
I'm not getting a valid intersected triangle in (v0, v1, v2).
0

Share this post


Link to post
Share on other sites

Is 'pHit' ever true? What is the HRESULT of D3DXIntersect()?

Have you checked the validity of the data behind two Lock()-pointers? If you assign the indices to a WORD, the buffer should be created as a 16 bit buffer.

 

Also, I don't see why you would test all faces individually once D3DXIntersect() has determined an intersection.

0

Share this post


Link to post
Share on other sites

Is 'pHit' ever true? What is the HRESULT of D3DXIntersect()?

 

I'm getting intersected triangle in (v0, v1, v2) but it's invalid, D3DXIntersect() HRESULT is S_OK.

 

Have you checked the validity of the data behind two Lock()-pointers? If you assign the indices to a WORD, the buffer should be created as a 16 bit buffer.

 

I tried using byte* instead of WORD*, same problem.

 

Also, I don't see why you would test all faces individually once D3DXIntersect() has determined an intersection.

 

I use break; after working on the intersection:

 

if ( D3DXIntersectTri(&v0, &v1, &v2, &rayFrom, &rayTo, &pU, &pV, &pDist) )
{
     // Process intersection...
     break;
}
0

Share this post


Link to post
Share on other sites

You've called the 3rd parameter passed to D3DXIntersect, "rayTo", which worries me slightly. The 3rd parameter is actually the direction of the ray, which is a vector, not a point in space. If rayTo is actually a point, you'll need to pass in (rayTo - rayFrom).

 

Also, you shouldn't need to scan through the faces after the initial test, the output value in pFaceIndex should contain the index of the face hit, if D3DXIntersect returns a hit.

1

Share this post


Link to post
Share on other sites

@Postie:

 

First, I use bullet physics raycasting to check for intersection, then I use D3DXIntersect() to get the intersected triangle, rayFrom and rayTo are D3DXVECTOR3 that I use with bullet physics raycasting.

 

I changed the code to something like this:

 

LPD3DXMESH pMesh = GetMesh();
pMesh->CloneMeshFVF( D3DXMESH_MANAGED, pMesh->GetFVF(), d3ddev, &pMesh );

WORD* pIndices;
D3DVERTEX* pVertices;
pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&pVertices);
pMesh->LockIndexBuffer(D3DLOCK_READONLY, (void**)&pIndices);

BOOL pHit;
DWORD pFaceIndex;
FLOAT pU, pV, pDist;
HRESULT hr = D3DXIntersect(pMesh, &rayFrom, &rayTo, &pHit, &pFaceIndex, &pU, &pV, &pDist, NULL, NULL);
assert(hr == S_OK);
if (pHit)
{
    D3DXVECTOR3 v0 = pVertices[pIndices[3 * pFaceIndex + 0]].p;
    D3DXVECTOR3 v1 = pVertices[pIndices[3 * pFaceIndex + 1]].p;
    D3DXVECTOR3 v2 = pVertices[pIndices[3 * pFaceIndex + 2]].p;
}

pMesh->UnlockVertexBuffer();
pMesh->UnlockIndexBuffer();

Anything wrong with the above code?

0

Share this post


Link to post
Share on other sites

@Postie:
 
First, I use bullet physics raycasting to check for intersection, then I use D3DXIntersect() to get the intersected triangle, rayFrom and rayTo are D3DXVECTOR3 that I use with bullet physics raycasting.

You didn’t address the issue he mentioned at all, and doing so would have likely solved your problem by now.
You are passing a starting point for the ray and an ending point for the ray when you are supposed to be passing a starting point and a direction.
D3DXIntersect() doesn’t care if the same ray points work in Bullet. Justifying passing incorrect parameters by saying, “But it works in Bullet,” is not going to convince DirectX to start working.

Bullet takes a ray start point and a ray end point (which means it is not actually a ray but a line segment).
DirectX D3DXIntersect() takes a ray start point and a ray direction.  As was already pointed out to you.

 

 

L. Spiro

2

Share this post


Link to post
Share on other sites

@L. Spiro:

So that means I have to pass (rayTo - rayFrom) as a direction.

 

I tried:

 

HRESULT hr = D3DXIntersect(pMesh, &rayFrom, &(rayTo - rayFrom), &pHit, &pFaceIndex, &pU, &pV, &pDist, NULL, NULL);
assert(hr == S_OK);
if (pHit)
{
    D3DXVECTOR3 v0 = pVertices[pIndices[3 * pFaceIndex + 0]].p;
    D3DXVECTOR3 v1 = pVertices[pIndices[3 * pFaceIndex + 1]].p;
    D3DXVECTOR3 v2 = pVertices[pIndices[3 * pFaceIndex + 2]].p;
}

 

Still not getting a valid intersected triangle.

0

Share this post


Link to post
Share on other sites

I expected as much but I did not want to overload you.

Also you need to normalize the direction vector, which means you should store it in a temporary.  This helps with debugging anyway, which is a skill you need to be a good developer in the first place.

 

I expect you have yet another problem which is much more complicated to explain.

Are you drawing the target object with a world matrix other than identity?

If so you need to translate the ray into the frame-of-reference of that object (and each object in your scene when you have more than one) before doing the ray cast.

 

That means translating the ray’s position by the inverse world matrix of each object, and translating the ray’s direction vector by the inverse transpose of the inverse world matrix.

 

 

L. Spiro

Edited by L. Spiro
0

Share this post


Link to post
Share on other sites

When I switch to Wirefire mode, I notice that I'm getting a valid intersected triangle for the terrain ONLY, but none of the other meshes are getting a valid intersected triangle.

 

Here is the code that I tried:

 

// Get ray direction
D3DXVECTOR3 direction = rayTo - rayFrom;
D3DXVec3Normalize(&direction, &direction);

// Inverse matrix
D3DXMATRIX matWorld = mesh->worldMatrix();
D3DXMATRIX matInverse;
D3DXMatrixInverse(&matInverse, NULL, &matWorld);

D3DXVECTOR3 modelRayOrigin, modelRayDir;
D3DXVec3TransformCoord(&modelRayOrigin, &hits.origin, &matInverse);
D3DXVec3TransformNormal(&modelRayDir, &direction, &matInverse);

hr = D3DXIntersect(pMesh, &modelRayOrigin, &modelRayDir, &pHit, &pFaceIndex, &pU, &pV, &pDist, NULL, NULL);
assert(hr == S_OK);
0

Share this post


Link to post
Share on other sites
I have never used D3DXIntersect(); it is typically better to do this manually.

Print debug information related to the world location of the object, the world location of the ray (and its direction), and the final ray position/direction after transforming it by the inverse of the world.

Really, from here out it is just a matter of debugging, which means you are on your own. You will have to learn how to debug.


L. Spiro
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0