Ray Picking problem...

May 16, 2012 at 4:12 AM
Edited May 16, 2012 at 4:16 AM

I have been working on this for DAYS.... While new to 3D concepts this cannot be that far off the beaten trail.  I have taken the XNA samples from Microsoft and have used them to understand Ray Intersections in the World, View, Transform constructs.  Which I THINK I have a good handle on...

THEN I tried to apply the simple 'Sphere' model from the samples but I NEVER get a positive result (unless I am looking downward and then I get the ground...

The goal here is to identify the GeoObject, Model, or Material/Texture that just received a mouse click.  Of course we have a predetermined list of known objects that are the only objects in the scene which are clickable...

First we convert the screen mouse location into a Vertex2... and hand onto it for the pending mouse up..

MyMainPage.xaml.cs
 

OnMouseDown (object sender, MouseButtonEventArgs e)
{
 Point location = e.GetPosition(MyDrawingSurface);
 Rect rectangle = new Rect(0, 0, MyDrawingSurface.RenderSize.Width, MyDrawingSurface.RenderSize.Height);
 if (rectangle.Contains(location))
 {
     mouseCaptured = true;
     Scene.ControlManager.HandleMouseDown(new Vector2((float)location.X, (float)location.Y));  // this saves Vector2 as member orginalPosition
 }
}

Next we encounter the mouse up handler:
public void HandleMouseUp()
{
    // Catalyst HitTest for dynamic content - hyperlinks is our main need here
    if (originalPosition.HasValue)
    {

        Ray clickRay = Utilities.CreateRay(originalPosition.Value.X, originalPosition.Value.Y, 
                                          this.scene.Engine.RenderWidth, this.scene.Engine.RenderHeight, 
                                          this.scene.ActiveCamera.World, this.scene.ActiveCamera.View, this.scene.ActiveCamera.Projection);
        scene.Engine.DynamicMaterialHitTest(clickRay);
    }
    originalPosition = null;
}

 The DynamicMaterialHitTest is a method designed to identify the actual item clicked - if it is among the desired items.

******* DOES NOT WORK ***********
public void DynamicMaterialHitTest( Ray ray )
{

    float? selectedDistance = float.MaxValue;
    foreach(Scene s in Scenes)   
      foreach(Model m in s.Models)  //TODO:  limit list to only our targeted objects/models/materials
      {
        //var bounding = new BoundingSphere(m.BoundingInfo.BoundingSphere.Center, m.BoundingInfo.BoundingSphere.Radius);
        //bounding = Utilities.TransformBoundingSphere(bounding, m.Scene.ActiveCamera.World);
       
        var bounding  = new BoundingSphere(m.BoundingInfo.SphereCenterWorld, m.BoundingInfo.SphereRadiusWorld);
  
        float? hit = bounding.Intersects(ray);        

        if (!hit.HasValue || hit == 0.0F)  //Expecting SOMTHING HERE ALWAYS NULL or 0.0
           continue;

        // got one
        if ( hit < selectedDistance)
        {
         selectedDistance = hit;
         selectedModel = m;        // class member var
        }
     }
}

 Apparently I am lost with the Babylon CreateRay method.. I assume from looking at the params passed in that we are UNPROJECTING that to Model values...

SO I have tried both the Model values and the WORLD values held in BoundingInfo... no difference...

So anyone... please what did I miss?  Another pair of eyes will probably jump right to it...

Thanks in advance

3Wiggle