Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 11 ■ 3D SCene Configuration: uSing the perSpeCtiveCamera anD pointLight


.getChildren().add() method chain, and therefore, it will be set up inside of your createSceneGraphNodes()
method, as you will see a bit later during this section of the chapter when we set up this Camera object for
your pro Java 9 game. We’ll also cover ParallelCamera, which is better suited for 2D games.


JavaFX Camera Class: An Abstract Superclass Defining Camera


The public JavaFX Camera superclass is an abstract class, used only to create different types of cameras.
Currently there is an orthographic or ParallelCamera subclass (object) or a PerspectiveCamera subclass
(object). Your application should not attempt to extend this abstract Camera class directly; if you attempt
this, Java will throw an UnsupportedOperationException, and your pro Java 9 game will not compile or run.
The Camera class is kept in the javafx.graphics module in the core javafx.scene package and is a subclass of
Node, as it is ultimately a Node at the top of your SceneGraph. The Camera class implements the Styleable
interface so it can be styled, and it contains the EventTarget interface so it can process events. The Java 9
class hierarchy for the JavaFX Camera class thus looks like the following:


java.lang.Object



javafx.scene.Node
javafx.scene.Camera



The Camera class is the base class for any camera subclass that is used to render scenes. A camera is
used to define how the scene’s coordinate space is rendered on the 2D window (Stage) that a user is looking
at. The default camera (if you don’t create one specifically, which we’ll be doing later in this section) will
be positioned in a scene such that its projection plane in a scene’s coordinate space is at Z=0 (in the exact
middle) and is looking into the screen in the positive Z direction. For this reason, we’ll be backing our
camera 1,000 units away from (-1000) the center of the screen in the code since the i3D GameBoard will be
at “center stage” and located at 0, 0, 0 (X, Y, Z).
The distance in Z units from the camera to the projection plane can be determined by the width and
height of the Scene to which it is attached (which is also the resulting projection plane) and the fieldOfView
parameter for a Camera object. The nearClip and farClip properties for a Camera object are the only
two properties or characteristics defined in this abstract class and are specified in what JavaFX calls eye
coordinate space. This space is defined with the viewer’s eye at the Camera object’s origin, and the projection
plane is one unit in front of the eye in the positive Z direction. The nearClip and farClip properties for any
Camera subclass, such as PerspectiveCamera, can be set using the .setNearClip() and .setFarClip() method
calls. These are two PerspectiveCamera class (object) method calls that we will be utilizing later during this
section of the chapter to configure our SceneGraph camera object.


JavaFX PerspectiveCamera Class: Your 3D Perspective Camera


The JavaFX PerspectiveCamera class extends the Camera class and is used to create a PerspectiveCamera
(object) that is used to render your i3D scene. The PerspectiveCamera class is also kept in the javafx.
graphics module in the core javafx.scene package; it is a subclass of Node and is a Node at the top of your
JavaFX SceneGraph. The PerspectiveCamera class also implements the Styleable interface so it can be
styled and the EventTarget interface so it can process events. The Java 9 class hierarchy for the JavaFX
PerspectiveCamera class looks like the following:


java.lang.Object



javafx.scene.Node
javafx.scene.Camera
javafx.scene.PerspectiveCamera


Free download pdf