RayCastInfo

Description

RayCastInfo is designed to provide more in-depth results when doing RayCasting in GameCore.  Previously, the RayCast function only returned a handle to the object that was 'hit' by the ray.  RayCastInfo allows you to find out more information about the hit object including the mesh, surface and other low-level details.

The RayCastInfo structure has both Input fields and Output fields, meaning that prior to use, you populate the input fields, and once you call 'RayCast' the output fields are populated by the results of the cast.

RayCastInfo Structure

Inputs:

                Vector origin

                Vector direction

                float maxdistance

                int type

int collisionGroups

Object@ excludeObject

Outputs:

                Vector intersect   // xyz coordinates of the intersection

                vector normal      // normal vector of the intersection

                float distance      // distance at which the intersection occured

                Object@ object   // handle to the object that was collided with

                int objectIndex    // which object the ray intersected.

                int meshIndex     // which mesh the ray intersected

                Surface@ surface  // handle to the specific surface that the ray intersected with

 


Usage Example:

// find the ray that goes through the mouse position
Viewport@ view = GetGameManager().GetViewport( "Main");
Vector cameraPos = view.GetCameraPos();
Vector clickDirection = view.ProjectInverseFromScreen( mouseX, mouseY, 1) - cameraPos;
// find the ray that goes through the mouse position
// raycast for targetted object
//float distance = -1;
RayCastInfo info;
info.origin = cameraPos;
info.direction = clickDirection;
//info.maxDistance = distance;

// either cast against the visible (rendered) geometry or against the physical (collision) geometry)
info.type = GEOM_VISIBLE;
//info.type = GEOM_PHYSICAL; 

// pick which collision groups you are casting against - the default is COLLISION_GROUP_1
info.collisionGroups = COLLISION_GROUP_1 | COLLISION_GROUP_2 | COLLISION_GROUP_3| COLLISION_GROUP_4;

@info.excludeObject = object;   // ignore this object in the raycast (must be a valid object handle)

/*
// old style raycast
Vector intersect;
Vector normal;
Object@ hitObject = GetGameManager().GetWorld().RayCast( cameraPos, clickDirection, distance, intersect, normal, false);
if (hitObject != null)
    Print(" hit object:" + hitObject.GetName());
*/
// new style
bool hit = GetGameManager().GetWorld().RayCast( info, false);
if (hit)
{
    Print("Hit!");
    Print("selected object: " + info.object.GetName());
    Print("Selected mesh index: " + info.meshIndex);
    Print("Selected object index: " + info.meshIndex);
    focus = info.object.GetPosition();
}