Game Development Reference
In-Depth Information
Destroying objects
So far, we've essentially created a starting point for a picking ray. It is not a true
raycast or picking ray until the ray travels forward in space and performs some type
of collision detection. To destroy an object in our scene, we'll need to use our picking
ray to perform a raycast and tell us the first rigid body with which it collides.
Raycasting in Bullet is handled through the btDynamicsWorld object's rayTest()
function. We provide the starting point (as a btVector3 ), the direction ( btVector3 ),
and an object to store the raycast data inside, which should be one of two different
classes that inherit from RayResultCallback . The object could either be:
ClosestRayResultCallback , which gives the closest collision that the ray
detected from the start location
AllHitsRayResultCallback , which gives an array filled with all of the col-
lisions the ray detected
Which object we want to use will depend on whether we want only the closest hit, or
all of them. We will be using ClosestRayResultCallback , which contains useful
data and member functions for the collision point, such as:
hasHit() , which returns a boolean value and tells us if there was a collision
between the ray and any physics object
m_collisionObject , which is the btCollisionObject our ray hit
m_hitPointWorld , which is the coordinate in world space where the ray de-
tected a collision
The Raycast() function in the topic's source code takes a picking ray and an empty
output RayResult structure, uses it to create a ClosestRayResultCallback ,
and then performs a raycast test. If the raycast was successful, the function fills out
the structure and returns true, allowing us to check the success or failure of the
raycast outside of this function.
Search WWH ::




Custom Search