Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making the camera follow units


This recipe will cover some principles on how to make the camera follow something in the game world. While it might seem like an easy task at first, there are some tricky bits too.

Getting ready

The recipe will build upon the Creating an RTS camera AppState object recipe of this chapter. All of the steps described in this recipe will be applied to AppState.

How to do it...

To make the camera follow units, perform the following steps:

  1. We start by adding two new variables, which we'll use for the new functionality. A Vector3f variable, called targetLocation, will be used to track the target, and a Boolean variable called follow, will be used to declare whether the camera should track the target or not. These are set from external classes.

  2. Out of convenience, we also define a final Vector3f variable, called UNIT_XZ, which we set to(1f, 0, 1f). We'll use this to convert 3D positions to 2D.

  3. Then, we need to add some functionality in the update method just before cam.setLocation...