• Advertisement
Sign in to follow this  

Collision Detection with D3DXIntersect()

This topic is 2506 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 just tried out collision detection with D3DXIntersect(), and although it basically seems to be working as I get some results, I wondered how to work with the values I got from D3DXInteresect()? Currently, I'm doing it that way:

BOOL Collision;
DWORD FaceIndex;
LPD3DXPMESH Mesh = ObjectManager.GetObject(0)->GetModel()->GetMesh();
float u;
float v;
float d;
D3DXIntersect(Mesh, &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, -1, 0), &Collision, &FaceIndex, &u, &v, &d, NULL, NULL);

DWORD* Indices = NULL;

Mesh->LockIndexBuffer(D3DLOCK_NOOVERWRITE | D3DLOCK_READONLY, (void**)&Indices);

FaceIndex -= FaceIndex%3;

int v1, v2, v3;

v1 = Indices[FaceIndex];
v2 = Indices[FaceIndex+1];
v3 = Indices[FaceIndex+2];


MeshVertices* Vertices = NULL;

D3DXVECTOR3 p1, p2, p3, p;

Mesh->LockVertexBuffer(D3DLOCK_NOOVERWRITE | D3DLOCK_READONLY, (void**)&Vertices);

p1 = Vertices[v1].Position;
p2 = Vertices[v2].Position;
p3 = Vertices[v3].Position;


p = p1 + u*(p2 - p1) + v*(p3 - p1);

So, just to make sure everything works OK I'm casing a ray from the origin down the y-axis. I should get some 0/y/0-point, however what I get is some random point. So I quess I'm trying to access the wrong vertices. What I figured was that: The index of the nearest vertex I get from D3DXIntersect minus its modulo with 3 should give me the 3 vertices of the face the ray intersected with. Then I'd access that vertices, and access it's positions. Using the formula given on the msdn site I can calculate the actual intersection point.

So, does anyone see what I did wrong? I was using a non-transformed mesh, and my ray can't really be wrong, so I guess that my understanding of how to access index/vertex data from the returned values of D3DXIntersect() is wrong. Can somebody tell me how this is done properly? Thanks in advance!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement