Game Development Reference
In-Depth Information
Understanding the basic ray casting
concepts
There are many use cases where collision detection is detected too late in a game.
Let's consider a case, in a game, where we have a door and if the player approaches
it, you want the door to open automatically. If we use collision detection for this case,
then the player has to first collide with the door, in order to open it; or alternatively,
you can create a bigger bounding box (a rigid body with more depth) so that when
the user approaches the door a collision is detected earlier. However, this approach
does not cover the use case of the player not facing the door. For instance, if the player
has its back towards the door, then a collision would be detected and the door would
open. Collision detection does not consider directions.
Ray casting is also used to build the basic AI of a non-playing character. A moving
player might need to alter its direction or speed automatically when there is another
existing object moving in the same direction. Collision is detected only after two
objects have intersected, and often, we need to predict it. In such cases, ray casting is
an effective concept to predict a collision.
Ray casting is used in many cases like:
• Determining the first object intersection by a ray
• Volume ray casting in a direct volume rendering method
• For hidden surface removal in finding the first intersection of a ray cast from
the camera through each pixel of the image
In ray casting, geometric rays are traced from the position of the player to sample a light
ray traveling in the direction of the player. If the traveling ray intersects any colliders
(rigid bodies in our case), a hit is observed, and then we can take the desired action.
The following diagram shows a simple ray casted from a player's position and its
intersection with objects:
 
Search WWH ::




Custom Search