Using RayTest

From Physics Simulation Wiki

Jump to: navigation, search

Ray casting is like shooting a virtual laser between two points, and seeing if (and what) it hit. See also the Wikipedia article. There is a number of useful things you can do with ray casting, such as firing weapons.

To do a raycast, you need to:

  1. Create a RayResultCallback object
  2. Do the raytest
  3. Process the results of the raycast

Basic example

// Start and End are vectors
 
btCollisionWorld::ClosestRayResultCallback RayCallback(Start, End);
 
// Perform raycast
World->rayTest(Start, End, RayCallback);
 
if(RayCallback.hasHit()) {
    End = RayCallback.m_hitPointWorld;
    Normal = RayCallback.m_hitNormalWorld;
 
    // Do some clever stuff here
}
Personal tools