Skip to main content

History: Ray query with MOGRE

Source of version: 5

Copy to clipboard
            The MOGRE classes and its members are different to the Ogre API, especially at the ray tracing. This was very confusing to me. Also there are generally only few informations about ((MOGRE|Mogre)). So I simplified my code to show other Mogre users how to do. --((User:Beauty|Beauty))

For example with a ray from a person into depth you can clamp the person to the ground.

Notice:

* only the ((-AABB|bounding boxes)) (AABB) will be hit, not the object itself (see picture)
* unvisible objects can be hit, too
* at least one frame must be rendered to get a result
* for rays related to a SceneNode you should use ''__World__Position'' and ''__World__Orientation'' to set ray ''Origin'' and ''Direction'' <br/> Note: Unfortunately the ((SceneNode)) properties ''getWorldPosition()'', ''setWorldPosition()'', ''getWorldOrientation()'', ''setWorldOrientation()'' were removed from Ogre. Use ''___getDerived__Position()'', etc. instead
* this code was tested with ((Terrain Scene Manager)) (if it also works with other ones, please add a note here)

{img src="img/wiki_up/Rays_and_BoundingBox_1a.gif" alt="Rays and BoundingBox 1a.gif"}

!!Code snippet
{CODE(wrap="1", colors="c#")} //-- set params --

 Vector3 startPosition = new Vector3(0, 0, 0); 
 Vector3 rayDirection = new Vector3(0, -1, 0);      // here: ray into depth
        
 // OR use position and orientation of a SceneNode
 //SceneNode someNode = mScene.Smgr.GetSceneNode("name_of_node");
 //Vector3 startPosition = someNode.WorldPosition;
 //Vector3 rayDirection = someNode.WorldOrientation.XAxis;  // in this case: base quaternion direction is along XAxis

 Ray myRay = new Ray(startPosition, rayDirection);
 RaySceneQuery raySQuery = mScene.Smgr.CreateRayQuery(myRay);

 // optionally sort the results
 raySQuery.SetSortByDistance(true);  // second possible parameter: maxResults
{CODE}

{CODE(wrap="1", colors="c#")} //-- calculate --

 Vector3 rayHitPoint;
 Single  rayDistance;
 String  rayHitPointName = "";

 RaySceneQueryResult raySQResult = raySQuery.Execute();
 RaySceneQueryResultEntry raySQREntry;

 for (Int16 i = 0; i < raySQResult.Count; i++)  // for all hits
 {
     raySQREntry = raySQResult[i];
     rayDistance = raySQREntry.distance;
     rayHitPoint = myRay.Origin + (myRay.Direction * rayDistance); // calculate hit point

     // save names of objects (or world fragment) that was hit
     if (raySQREntry.worldFragment != null)
         rayHitPointName = raySQREntry.worldFragment.ToString();
     if (raySQREntry.movable != null)
         rayHitPointName = raySQREntry.movable.Name;

     // Also bounding boxes of terrains can be hit.
     // Skip them to prevent hits on strange places ("just in the air").
     if (rayHitPointName.StartsWith("tile["))  // e.g. "tile[1][1,1]"
         continue;

     // ... do something with the hit ...

     if (raySQREntry.worldFragment != null)
     {
         // ... only do something with hits to terrain
     }

     if (raySQREntry.movable != null)
     {
         // ... only do something with hits to other objects
     }

     // If you just want to find a special hit, you can leave the for() loop.
     // The results will be stored in the variables (rayHitPoint, raySQREntry etc.)
     if (this_is_your_hit)
         break;

 } // for{CODE}

{CODE(wrap="1", colors="c#")} //-- if nothing was hit --

 if (raySQResult.IsEmpty)
 {
     // ... do something ...

     // e.g. prevent null values
     rayHitPoint = Vector3.ZERO;
     rayDistance = 0;
     rayHitPointName = "no_hitPoint";
 }{CODE}

!!See also
* ((Ray classes in MOGRE))
* [http://www.ogre3d.org/docs/api/html/classOgre_1_1RaySceneQuery.html|RaySceneQuery Class Reference]
* ((MOGRE Line 3D)) (to show the ray line)
* ((Raycasting to the polygon level - Mogre)) (to avoid incorrect hits)

        

History

Information Version
Sat 04 of Aug, 2012 14:04 GMT-0000 Tubulii Added alternative. 10
Wed 18 of Jul, 2012 10:53 GMT-0000 Tubulii Extended 'Query flags' section with additional code and comments. 9
Tue 17 of Jul, 2012 23:20 GMT-0000 Beauty Improvements for my text, which I wrote some years ago 8
Tue 17 of Jul, 2012 22:51 GMT-0000 Beauty added section "Query flags" 7
Thu 29 of Jul, 2010 09:42 GMT-0000 Beauty corrected links 6
Thu 29 of Jul, 2010 09:40 GMT-0000 Beauty added note for WorldPosition, WorldOrientation ... unfortunately this properties were removed from Ogre /-: 5
Wed 30 of Dec, 2009 07:38 GMT-0000 jacmoe 4
Sun 27 of Dec, 2009 19:04 GMT-0000 jacmoe 3
Sun 27 of Dec, 2009 19:03 GMT-0000 jacmoe 2
Sun 01 of Mar, 2009 16:15 GMT-0000 Smiley80 1