# How to retrieve vertex information from ID3DXMesh

I need to know the position of each vertex (and triangles) from a mesh (a pointer to ID3DXMesh, created by calling functions like D3DXCreateBox, D3DXCreateTeapot and D3DXCreateSphere) to perform some collision detection.

I've tried using GetVertexBuffer() and GetIndexBuffer() but I don't know how to use the structures to retrieve point data in the world coordinate system. How can I achieve that?

• If you have the data in local space and your collision is in world space, have you considered using an inverted matrix to convert the collision test into local space of the model. It would be cheaper than trying to test it in world space. – ErnieDingo Mar 8 '18 at 21:52

You can look at the legacy DirectX SDK Direct3D 9 sample Pick or the Direct3D 10 sample Pick10.

Generally you don't actually want to use the same geometry to render as you do for doing collision detection. Most games will first use simple bounding geometry like spheres, axis-aligned boxes, oriented-boxes or cylinders and make decisions from that.

If after bounding volume tests you determine a possible intersection and you need face-level intersection testing, then you'd ideally do a face-by-face collision using a simplified mesh rather than the original complex geometry that is often made to avoid issues with convexity. For CAD and editors, you would likely do a face-by-face collision test of the 'true' geometry, but you'd maybe do that with a system memory copy rather than the render copy in the VB/IB for efficiency. With Direct3D 9, you can generally read POOL_MANAGED resources but that's because there is already a system-memory copy in addition to the video memory copy. With Direct3D 10.x/11.x you'd want to maintain the collision data yourself outside the IB/VB rather than force the IB/VB to be in a 'DYNAMIC' mappable location.

And you can of course use various GPGPU techniques to try to accelerate collision detection, but that's quite complicated to get robust.

• I'll take a look, thanks! BTW, I understand that it is better to do broad phase collision detection (i.e with spheres or AABBs) before doing narrow phase (SAT, GJK, etc). I'm currently in the step of implementing narrow phase, that's why I want the actual mesh. – nairdaen Feb 11 '15 at 7:29