Game Development Reference
In-Depth Information
Note
Continue from here using the Chapter5.1_Raycasting project files.
Picking rays
The GetPickingRay() function in the topic's source code involves a large smat-
tering of 3D mathematics that are beyond the scope of this topic. It should be enough
to know that it takes the x-y coordinates of a mouse click, and uses the camera's
data (its position, near plane, far plane, field of view, and aspect ratio) to calculate
and return a btVector3 in world coordinates that points forward from the camera
in the corresponding direction. If the camera moves, or we click somewhere else
on the screen, then we get a new btVector3 pointing forward from that position
instead. Armed with this function, we can add some simple code to create a new
object whenever we click on the right mouse button. This code can be found in the
chapter's source code, in the ShootBox() function. This function is called by the
Mouse() function anytime when the application detects that the right mouse button
was clicked.
Tip
Recall that the Mouse() function was called by FreeGLUT anytime a mouse but-
ton is clicked. It gives us the button, the state (pressed or released), and the x-y
coordinates of the click.
Launch our application and try right-clicking on the mouse. It should create a purple
box and launch it towards the mouse cursor. The following screenshot shows this in
action:
Search WWH ::




Custom Search