Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 11 ■ 3D SCene Configuration: uSing the perSpeCtiveCamera anD pointLight

The units used in a ParallelCamera object are represented using pixel coordinates, so this is exactly like
a 2D digital imaging software package and the 2D StackPane image compositing layer object we are using for
2D UI design, which also references coordinates from the upper-left corner of the screen at 0,0 (X,Y). This is
yet another indicator of the fact that this is a more logical Camera subclass to use for your i2D games, rather
than your i3D games.
There is one sole constructor method for this class that uses an empty parameter area and looks like this:


Camera2D = new ParallelCamera();


Next, let’s take a look at how to create the PerspectiveCamera object, which we’ll be using for our pro
Java 9 game. We’ll see how we will initially configure it for use and how we will add it to the root of our
JavaFX SceneGraph.


Adding a PerspectiveCamera to Your Scene: Using .setCamera()


The first thing that we need to add to the top of the JavaFXGame class is a declaration of the
PerspectiveCamera object using the PerspectiveCamera camera; Java statement, which will then
present a wavy red underline indicator underneath the PerspectiveCamera object (class usage). Use
the Alt+Enter keystroke shortcut to have NetBeans 9 write the import statement for you and then open
up the createSceneGraphNodes() method so you can add that camera object to the top (root) of the
SceneGraph. Instantiate this camera object underneath the root Group instantiation using the camera = new
PerspectiveCamera(true); constructor statement. Then, on the next line, call a .setTranslateZ() method with
a -1000 value to move the camera 1,000 units away from the 0,0,0 center of the 3D scene.
Set the nearClip Camera object attribute to 0.1 by using a .setNearClip() method call off the camera
object, and set the farClip attribute to 5000.0 by using the .setFarClip() method call. Finally, wire this
camera object into your scene object (root), using the .setCamera() method call off of the scene object, and
pass the camera object over using a camera object as the parameter in the .setCamera(camera) method
call. Set your scene object Background value to Color.BLACK by using the .setFill() method call so your 3D
objects will stand out well, using these Java statements:


PerspectiveCamera camera;
...
createSceneGraphNodes() {
camera = new PerspectiveCamera(true);
camera.setTranslateZ(-1000);
camera.setNearClip(0.1);
camera.setFarClip(5000.0);
...
scene.setFill(Color.BLACK);
scene.setCamera(camera);


As you can see in Figure 11-1, your code is error-free, and the camera is now set up and attached to
your 3D scene, which we have now converted into being a 3D scene. It is now a 3D Scene because it uses a
PerspectiveCamera at the top of its rendering pipeline (that is, at its root), so all objects underneath it will
now use the 3D perspective.

Free download pdf