O'Reilly logo

WebGL Game Development by Sumeet Arora

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing picking using ray casting

Let's now start implementing what we have discussed. The algorithm we will apply is as follows:

  1. Create a rigid body for each scene object.
  2. Calculate the click coordinates of the canvas, convert these coordinates to normalized device coordinates, and create a vector.
  3. "Unproject" the vector using the camera projection matrix.
  4. Create a ray segment using the vector.
  5. Check for an intersection.
  6. If an intersection is found, then change the color of the selected object.

Using a rigid body (collider) for each scene object

Now, we will modify our StageObject class again to create a bounding box. If we revisit the constructor of our jigLib.JBox class, then its constructor takes the width, depth, and height of the corresponding ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required