Picking on transformed vertices

Started by
5 comments, last by exextatic 12 years, 3 months ago
Hi all,

Stumped on this one, Im trying to implement picking (which I have working on vertices), but Im not sure how Id go about picking on vertices after they've been rotated. I use the following code to detect picking on a vertice that has been translated:

D3DXVECTOR3 vert1, vert2, vert3;

CUSTOMVERTEX* tempVerts;
v_buffer->Lock(0, 0, (void**)&tempVerts, 0);
vert1 = D3DXVECTOR3(tempVerts[0].X, tempVerts[0].Y, tempVerts[0].Z);
vert2 = D3DXVECTOR3(tempVerts[1].X, tempVerts[1].Y, tempVerts[1].Z);
vert3 = D3DXVECTOR3(tempVerts[2].X, tempVerts[2].Y, tempVerts[2].Z);
v_buffer->Unlock();

bool hit = D3DXIntersectTri(
&vert1,
&vert2,
&vert3, &vPickRayOrig, &vPickRayDir, NULL, NULL, NULL);

How would I go about picking on rotated vertices?

Thanks,
James Warner
Advertisement
You have to transform your pick ray from world space to the objects model space, OR, you can transform the vertices from object space to world space. It would be faster to transform the pick ray though, since you only have to do that once per object, instead of translating each vertex to world space. You can transform the pick ray from world space to model space by multiplying it by the inverse of the objects world space (or transformations matrix). or like i said, you could just transform each vertex to world space using the objects transform (world) matrix

if you decide to transform the vertices to world space, you will do that right before you call the [font=CourierNew, monospace][size=2]D3DXIntersectTri()[/font]function.

if you decide to transform the pick ray, you will do that outside the loop that goes through each triangle in an object to check for picking. maybe you already knew these last two things, but i thought i would be more thorough

You have to transform your pick ray from world space to the objects model space, OR, you can transform the vertices from object space to world space. It would be faster to transform the pick ray though, since you only have to do that once per object, instead of translating each vertex to world space. You can transform the pick ray from world space to model space by multiplying it by the inverse of the objects world space (or transformations matrix). or like i said, you could just transform each vertex to world space using the objects transform (world) matrix

if you decide to transform the vertices to world space, you will do that right before you call the [font="CourierNew, monospace"]D3DXIntersectTri()[/font]function.

if you decide to transform the pick ray, you will do that outside the loop that goes through each triangle in an object to check for picking. maybe you already knew these last two things, but i thought i would be more thorough


How would I go about transforming the vertices to world space? I know how to translate / scale / rotate its matrix, but how do I use that to change the vertex positions for me to use with D3DXIntersectTri()?
you can use the function D3DXVec3TransformCoord(). So you can do this:

D3DXVECTOR3 vert1, vert2, vert3;

CUSTOMVERTEX* tempVerts;
v_buffer->Lock(0, 0, (void**)&tempVerts, 0);
vert1 = D3DXVECTOR3(tempVerts[0].X, tempVerts[0].Y, tempVerts[0].Z);
vert2 = D3DXVECTOR3(tempVerts[1].X, tempVerts[1].Y, tempVerts[1].Z);
vert3 = D3DXVECTOR3(tempVerts[2].X, tempVerts[2].Y, tempVerts[2].Z);
v_buffer->Unlock();

D3DXVec3TransformCoord(&vert1, &vert1, &objectTransformMatrix);
D3DXVec3TransformCoord(&vert2, &vert2, &objectTransformMatrix);
D3DXVec3TransformCoord(&vert3, &vert3, &objectTransformMatrix);

bool hit = D3DXIntersectTri(
&vert1,
&vert2,
&vert3,
&vPickRayOrig,
&vPickRayDir,
NULL, NULL, NULL);


where objectTransformMatrix is your objects world matrix, or the matrix that stores the transformations

Also, i'm not sure exactly what your checking for picking for, but usually it's not very efficient to be reading vertex data from buffers to do calculations like picking or collision detection. If I were you, i would store a separate array or vector that contains all the vertex positions for the object. It's much faster that way. just a suggestion
Inverse the rotation*object space, i remember doing this :)
:)
the rotation is stored in the objects world space (transformation) matrix. inversing the objects world space matrix not only works for rotating the object, but also for scaling and translating.

so, the best way to do it, like i said above, is inverse the objects world space matrix, and transform your pick ray with the inverse of the objects world space matrix. do this before you enter the loop to check picking for each of the triangles in the object
Thanks, works perfectly. And Iv changed my code to take the vertices from the object at creation, instead of taking them from the vertex buffer as you suggested.

This topic is closed to new replies.

Advertisement