This project has moved and is read-only. For the latest updates, please go here.

Implementing picking of DirectX::Models of DirectXTK

Jul 24, 2014 at 2:32 PM
Hello friends,

I know this topic has been covered in general many times on the Internet but I just can't get it to work. There is always probably some differences between my code and the examples which makes the math go wrong. So my question is: how do I implement picking of DirectXTK Models? Even just checking against BoundingBox would be enough for a start.

Jul 24, 2014 at 4:47 PM
Edited Jul 24, 2014 at 4:50 PM
Bounding-volume picking is fairly trivial. ModelMesh contains a BoundingSphere and a BoundingBox which is typically in 'model' coordinates. You can use the standard DirectXMath routines to Transform it into world coordinates, then perform a ray test using Intersects.

BTW, you may want to look at the DirectMath Collision sample.

Per-triangle testing is more challenging because you ideally don't keep the vertex buffer/index buffer of the original model in standard memory but instead keep it in static Direct3D resources. You could keep a shadow-copy in RAM, but the better solution it usually to keep a simplified version of the mesh in memory (sometimes called a collision-mesh) and then perform per-triangle testing against it if needed.
Marked as answer by walbourn on 7/24/2014 at 8:48 AM
Jul 24, 2014 at 5:20 PM
Thanks. I forgot to make transform for the mesh, that's why I couldn't get it to work. It is probably working now. And yeah the BoundingBox test sometimes picks wrong object because the box is larger than the model in some cases. Its not possible to get vertex/index info from D3D resources? And if I load the model using CreateFromCMO(), how I store the required data from the model?
Jul 24, 2014 at 6:48 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 27, 2014 at 10:27 AM
Edited Jul 27, 2014 at 10:43 AM
One minor problem seems to exist. The pick works as long as I don't rotate the camera. What is the translation I need to do for the ray to take into account this camera rotation?

Edit, more info:
  • The more I rotate camera to the right, the more the ray is headed to the left (and vice versa)
  • The more I tilt camera up, the more the ray is headed to down (and vice versa)
Jul 27, 2014 at 4:55 PM
For picking you typically use the inverse projection+view matrix to get it back to world space. You then either (a) transform the ray from world-to-model space and then test the collision, or (b) transform the bounding volume to world space and then test the collision.

The math for converting a projection-view ray back to world space is covered extensively in the ray-tracing literature.