Advertisement Jump to content
Sign in to follow this  

D3DXIntersect and Barycentric Coordinates

This topic is 4991 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've been browsing around the older posts on this forum about this very topic, but I am yet to find a solution to my problem. I'm drawing a plane that represents the range of the user for his melee weapon. When the user right clicks in this plane, I want a projectile to be fired at the point where he clicked. I'm using a mesh to draw the plane and the D3DXIntersect function to determine if and where the user clicked in the plane. I am currently able to successfully tell if the user is clicking in the plane, but the "where" part does not seem to be working correctly. It seems to me as if my plane is divided diagonally in half - when I click on one half, the projectile goes to roughly the correct location. When I click on the other half, the projectile launches as if I clicked on the opposite corner (if I click in the lower left, the projectile fires to the upper right and vice versa, yet if I click in the lower right, it goes to the lower right, same for the upper left). I'm calculating the "where" using the barycentric coordinates returned by the D3DXIntersect call. Once I convert these to cartesian coordinates, I translate them using the world matrix for the object. My code follows: D3DXIntersect(pLocalMesh,&vNear,&vDir,&bHit,&dwIndex,&u,&v,&dist,NULL,NULL);
//Convert the barycentric coordinates to world coordinates
BYTE* pVertices = NULL;
BYTE* pIndicies = NULL;

HRESULT hr=pLocalMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
if (!FAILED(hr))
	HRESULT hrIndex = pLocalMesh->LockIndexBuffer(D3DLOCK_READONLY, (LPVOID*)&pIndicies);
		D3DXVECTOR3 vertices[3];
		short faces[3];

		faces[0] = ((short*)pIndicies)[(dwIndex * 3) + 0];
		faces[1] = ((short*)pIndicies)[(dwIndex * 3) + 1];
		faces[2] = ((short*)pIndicies)[(dwIndex * 3) + 2];

		vertices[0] = D3DXVECTOR3(((D3DXVECTOR3*)pVertices)[faces[0]]);
		vertices[1] = D3DXVECTOR3(((D3DXVECTOR3*)pVertices)[faces[1]]);
		vertices[2] = D3DXVECTOR3(((D3DXVECTOR3*)pVertices)[faces[2]]);

		//V1 + U(V2 - V1) + V(V3 - V1).
		D3DXVECTOR3 temp = vertices[0] + u * (vertices[1]  - vertices[0]) + v * (vertices[2] - vertices[0]);


		pvPosition->x = temp.x;
		pvPosition->y = temp.y;
		pvPosition->z = temp.z;


If anyone has any ideas why this might not be working, please let me know. I was initially unclear how the index buffer worked, and it's possible that I still am. But outside of that, I can't seem to figure out where my error might lie. Thanks!

Share this post

Link to post
Share on other sites
What does your plane look like? Is it a flat square? A flat circle? You can probably skip a lot of the work you're doing by not using the mesh to intersect. But it depends on what shape it is.

Share this post

Link to post
Share on other sites
The plane is a trapezoid that extends from the user. It has a shorter edge at the user and a longer edge in the distance.

Share this post

Link to post
Share on other sites
I got it working.

Turns out it was the Vertex buffer that I didn't quite understand!

I had forgetten about the whole FVF idea. So I was treating the vertex buffer as if it were full of D3DXVERTEX3's when in fact, each vertex had much more data.

Are there any helper funtions to extract different portions of FVF data from a vertex buffer? As it stands, I'm extracting the vertices with:

memcpy(&vertices[0], &((BYTE*)pVertices)[faces[0] * bytesPerVert], sizeof(D3DXVECTOR3));

That seems a little messy, though it works. So if there's a cleaner way, I'd love to know about it.


Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!