Book Image

Learning LibGDX Game Development- Second Edition

Book Image

Learning LibGDX Game Development- Second Edition

Overview of this book

Table of Contents (21 chapters)
Learning LibGDX Game Development Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Ray picking


It would be great if we could interact with the game objects. In a 2D scene, it is easy as we can map the 2D coordinates of the game object with the input coordinates. However, in a 3D game, it is different as the game object is positioned in 3D world coordinates and the input is available as 2D screen coordinates. A familiar scenario is a first person shooter game, wherein on shooting the bullet it is traced through the scene until a collision is detected. Ray picking is the process of shooting a line or ray from the camera through the 2D view port into the 3D game world until it hits an object, as shown here:

The following code will explain the process of ray picking in LibGDX. We will extend and update CameraInputController in the create() function as follows:

@Override
public void create() {
...
final BoundingBox box= model.calculateBoundingBox(new BoundingBox());
camController = new CameraInputController(cam) {
private final Vector3 position = new Vector3();

@Override
public...