Game Development Reference
In-Depth Information
To find much more about the step simulation in the standard wiki,
Ray casting in Bullet
Ray casting is like shooting a virtual laser between two points and seeing whether it
hit anything and what it hit. There are a number of useful things you can do with ray
casting, such as firing weapons. This is similar to what we learned in
Chapter 13
,
Basic
3D Programming
, under the
Ray picking
section.
To do a ray cast, you need to:
1.
Create a
RayResultCallback
object.
2.
Do the ray test.
3.
Process the results of the ray cast.
The ray test is done by calling the
world.rayTest(rayFrom, rayTo, rayTestCB);
function where
rayFrom
and
rayTo
are objects of the
Vector3
class and
rayTestCB
is
an object of the
ClosestRayResultCallback
class. The
world
object is the dynamics
world where physics is simulated. The result of this method is stored in
rayTestCB
.
A simple test game
We will create a simple physics game just like the previous one. Here, we will
include basic shapes such as a box, sphere, cylinder, and cone and do a simple ray
testing. The program will have a ground shape upon which objects (such as a box
and sphere) will be thrown on touch. There will be buttons on screen, which after
selected will allow the related item to be thrown. The last button demonstrates ray
picking. Your screen will look like this: