Game Development Reference
In-Depth Information
Chapter 5. Raycasting and Constraints
Many games use a mouse or touch screen as the primary means of controlling ob-
jects; whether it's by selecting, moving, creating, or destroying them.
If we want to implement such a system into our application, then we must find a way
to translate a mouse click (x-y coordinates on the screen) into a method of detecting
the first object underneath the pointer. The answer to this conundrum is raycasting ,
which we will be exploring in this chapter.
Then, once we have a raycasting system in place, we will explore how we can use it
in combination with Bullet's constraint system to move objects with the mouse cursor.
The power of raycasting
Raycasting is a technique that can be used for a variety of different tasks. A common
use is to find objects underneath the cursor from the camera's perspective. This is
typically referred to as picking . However, rays are also used in other tasks, such as
in shooter games to cast a line from the barrel of a weapon to where a bullet might
strike, which could be a wall or another player.
Another common usage of rays is to surround a player character with many rays that
point outwards from the object, that are used as feelers to detect if the player is near
other objects. For example, there could be a ray that points downwards from the play-
er's feet a short distance. If the ray collides with a physical object, then we know that
the player is touching the ground and telling us to play the appropriate animation, or
reset a flag that allows them to jump.
Regardless, all of these concepts are built from the same basic idea; choose a starting
point, pick a direction to travel in (a ray), and move along that direction (cast) until it
collides with something. Let's create a basic picking ray function which exactly does
that.
Search WWH ::




Custom Search