ASU Learning Sparks

Using Raycasting to Create Virtual Interactions

Written by Robert LiKamWa | May 21, 2023 6:46:39 AM

How can you create the idea of collisions and other interactions in 3D virtual worlds? This is through a concept called raycasting. Raycasting allows mouse clicks, phone taps and other physical interactions to produce virtual interactions with objects in virtual worlds. Developers can work with raycast functions within game engines to produce these interactions.

When interacting with objects in the virtual world, it’s often useful to have functionality where the user can point and click with their mouse, tap on their phone screen, or point a controller to interact with the objects that are in the virtual world. In order to make this happen, we have to use a technique called raycasting. 

Raycasting does what it’s in its name: it casts a ray through the virtual world and sees what object it hits. A ray is a straight line that starts from an origin and projects out in a direction. For screen-based devices, the tap on the screen can be projected as a ray that starts from the center of the virtual camera, through the pixel on the screen, and out into the virtual world. For controllers in virtual reality, the ray can start at the controller and go in the direction that the controller is pointed.

Game engines allow developers to call a raycasting function to see what GameObjects the ray interacts with. The GameObjects have to have Collider geometry associated with them. The raycasting will provide to the developer all of the colliders that the ray hits. The developer can then decide what to do with the various objects that the colliders are attached to, or change the state of the entire game. 

The raycasting function call is configurable to specify certain types of objects, which developers can group as layers. For example, the developer may only want the raycast to select objects that serve as buttons for an immersive user interface. Or they only want to be able to select ground surfaces. The layers allow for that configuration.

The raycasting function also allows developers to specify how far to search along the ray to identify any objects that fall along the ray. Shorter distances will incur less computational overhead, while longer distances will allow for long-distance interactions. Similarly, developers can tell the raycast to either return all objects that fall along the ray or just the first object that the ray hits. By only searching for the first object, the game engine has to perform fewer computations to retrieve the result.

Raycasting is thus a fundamental technique to allow developers to provide object interactions, allowing users to select and interact with virtual objects through their physical interactions with their screens and controllers.