TGE/Collision/CastRayExample

From TDN

Below is an example of code for use of the castRay. The code below wants to take the myPoint and finds the highest object in the Z direction which might be above or below myPoint. An example use would be a teleport where you would want to ensure that the player is teleported to a point above the highest object.

The code assumes the myPoint is a Point3F and has been initialized already to a x,y in which you wish to find the highest object in the 'z' direction.

// Setup the currentContainer to either the server container or the client container. 
// In your code you might know if you are on the server or not already. In which case you
// could use the gClientContainer or gServerContainer directly
Container * currentContainer;
if(isClientObject()) // Client object. 
{
   currentContainer = &gClientContainer;
}
else // Server object. 
{
   currentContainer =  &gServerContainer;
}

RayInfo rInfo; // Will return info on what the castRay found including object information.
Point3F start, end;
Point3F myPoint; // Init this to the point which you want to goto

start.x = end.x = myPoint.x; //some x (whatever value you want)
start.y = end.y = myPoint.y; //some y (whatever value you want)
start.z = 2000; // Really high, might be over kill
end.z = -2000; // really low, might be overkill

// Look for flags set for pretty much everything.
// Note: An object could have more than one flag set
if(currentContainer->castRay(start, end, 
   StaticObjectType | 
   InteriorObjectType | 
   WaterObjectType | 
   ShapeBaseObjectType | 
   StaticShapeObjectType | 
   ItemObjectType | 
   TerrainObjectType |
   StaticTSObjectType 
   , &rInfo))
{
     myPoint.z = rInfo.point.z;
}
else
{
   // didn't find anything, no TerrainObjectType or anything... 
   // you decide how to handle this error
}

--Dan 02:00, 26 Oct 2005 (PST)