Improved Ray Picking

Home | Up | Search | X-Zone News | Services | Book Support | Links | Feedback | Smalltalk MT | The Scrapyard | FAQ | Technical Articles


Written by Robert Dunlop
Microsoft DirectX MVP

Related Articles of Interest:

View Oriented Billboards
Projecting a Ray from 2D Screen Coordinates

Download Demo

Requires DirectX 8.0 Runtimes or Later


This demo shows how to to select objects from screen coordinates, accounting for the viewport scaling and transformation through the world, view, and projection matrices.  This improves upon the the Pick sample provided with the DirectX SDK, which does not allow for world transformations to be applied on a per-object basis.

The application and source code also demonstrate a method for setting up hierarchies of objects, with parent / child transformation dependency.

Ray Picking with Object Transformations

In the SDK's Pick sample, a ray is calculated that passes through the frustum under the selected pixel, in world space.  This ray is then tested for intersection against the the geometry of a mesh rendered to the screen, to determine if and where the user has clicked on the object.

That will work fine as long as the world transformation is an identity matrix.  However, if a world transformation is applied to the objects in the scene, then we can no longer compare a world space ray directly against the mesh in model space.  There are basically two ways to handle this:

  1. Transform each face of the mesh into world space to perform the intersection testing.
  2. For each object, transform the ray into model space using the inverse of the object's world transformation.

In the sample provided on this page, the second method is utilized.  Once the ray is back-transformed into model space, then intersection testing can be performed using the object mesh without per-vertex transformation.

Related Articles of Interest:

View Oriented Billboards
Projecting a Ray from 2D Screen Coordinates

This site, created by DirectX MVP Robert Dunlop and aided by the work of other volunteers, provides a free on-line resource for DirectX programmers.

Special thanks to WWW.MVPS.ORG, for providing a permanent home for this site.

Visitors Since 1/1/2000: Hit Counter
Last updated: 07/26/05.