Home > Games Programming, Graphics, XNA > XNA Ray Picking Sample

XNA Ray Picking Sample

Edit : The article is archived!, please refer to the my XNA Picking Tutorial

Hello! 🙂

This is a simple XNA 3.1 app that I wrote to demonstrate how picking 3D objects works :)…

Make sure you first check the official picking sample, you can find it here (XNA GS4).

Okay back to my app…
What makes my sample different is that you can actually see the camera frustum & the mouse ray, here’s a screen shot:

Ray Picking Sample
as you can see there are three viewports here:
1- Right : The main game scene, camera used : camera1…
2- Left : in this viewport the whole scene is rendered again and can be seen using a second camera (camera2), camera1 is visualized here, camera1 frustum, near/far planes and mouse ray are all rendered here so that you can see how picking works ;)…
3- Bottom : Instructions & info.

Both cameras are free look cameras similar to Counter-Strike free look cameras…

Here are the files:
RayPickingSample Executable
RayPickingSample Source

Revisions:
rev.1 : Fixed cam frustum bug where the tip of the frustum were stuck on some computers, fixed a typo.

Interested in programming details?

In this project you can find a free look camera class called Camera , there’s also a second camera class which is VisualCamera.
VisualCamera class inherits from Camera class and adds  the fields & functions needed for the camera to be rendered (using another camera of course), you can check this class if you wanna know how to visualize a BoundingFrustum.

This is the first time I visualize a camera & use viewports, I’m not saying there are bugs but every thing is possible haha! 😀

The static class Res is the class that holds the game resources used by most game objects, so for example : instead of passing the GraphicsDevice to each object I create, each object will have access to GraphicsDevice through the static class Res.

The static class Utils contains some frequently used functions..

The class Obj is a simplified version of the class I usually use for my XNA games to create 3D objects.

The game will automatically find the screen dimensions and open in full screen mode.

I modeled the blue camera model in 3Ds Max & I tried to make it similar to the one used in 3DS Max ;).

Please if you have any note/question don’t hesitate to tell me!
Comments are always welcome! 😀

  1. Behrouz
    30/07/2011 at 6:57 am

    Thank you very much because of your useful sample. Since my models have more than 1 mesh, I want to detect which mesh in a model has selected. does your sample works in this way?

    your prompt answer will be appreciated.

    B.jedari

    behrouz_jedari@yahoo.com

    • Fuchs
      30/07/2011 at 7:15 am

      Hi and thank you for reading :).
      Short answer : yes
      Long answer : The main picking idea is to unproject the mouse to create a Ray then test it for intersection against each mesh of the model, the function looks something like this:

      // Inside the class of a drawable 3d object named Obj.cs
      public bool CheckRayIntersection(Ray ray)
      {
          BoundingSphere boundingSphere;
      
          foreach (ModelMesh mesh in model.Meshes)
          {
              boundingSphere = mesh.BoundingSphere.Transform(
                  modelTransforms[mesh.ParentBone.Index] * GetWorld());
      
              if (ray.Intersects(boundingSphere) != null)
              {
                  // "mesh" is the mesh you want
                  return true;
              }
          }
          return false;
      }
      

      I hope things are more clear now :).

  1. 25/11/2010 at 1:50 am

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: