Pro Java 9 Games Development Leveraging the JavaFX APIs

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

The PerspectiveCamera object defines the viewing volume for a perspective projection. Imagine a
truncated right-facing pyramid, as most cameras are represented visually in 3D software such as Blender or
3D Studio Max.
There are two (overloaded) constructor methods for this class. One has an empty parameter area, like this:


camera = new PerspectiveCamera();


The second uses the boolean value fixedEyeAtCameraZero attribute (or parameter or characteristic),
which is the one we are going to use in our camera object declaration, instantiation, and configuration Java
code, like this:


camera = new PerspectiveCamera(true);


Of course, we will also declare a PerspectiveCamera camera at the top of the class, and use Alt+Enter to
have NetBeans 9 write an import statement for this class for us. The PerspectiveCamera has a fieldOfView
value, which can be used to change the field of view (FOV) angle for a camera projection and is measured in
degrees. I will leave the FOV at its default value and assume that this default FOV gives the best visual result,
as determined by the JavaFX Development Team.
My tendency in using i3D for games and for simulations is to “dolly,” or move the camera along the
Z (in and out of the scene) transformation axis rather than to use FOV value changes, as even in real life
changing a camera lens (like going from 24mm to 105mm) tends to change perspective more drastically. In
my experience, using different 3D virtual cameras, this change in perspective is even more drastic in virtual
3D than it is when using real-life cameras.
By default, your PerspectiveCamera is located at center of your scene on creation (instantiation)
and looks along (is pointed down) the positive z-axis. If you construct a PerspectiveCamera using
PerspectiveCamera(false), then the coordinate system defined by the camera will have its 0,0 origin located
in the upper-left corner of the panel, with the y-axis pointing down and the z-axis pointing away from
the viewer (into the screen). If a PerspectiveCamera node is added to the scene graph, the transformed
position and orientation of the camera will define the position of the camera and the direction that the
camera is looking. In the default camera, where fixedEyeAtCameraZero is false, the eye position Z value is
adjusted in Z such that the projection matrix generated using the specified fieldOfView will produce units
at Z = 0 (on the projection plane) using device-independent pixels. This matches your characteristics for a
ParallelCamera. When the Scene is resized, the objects in the scene on the projection plane (Z = 0) will stay
the same size, but more or less content in your scene is viewable, which is more appropriate for 2D camera
and 2D scroller use than 3D camera use, where resizing a camera will instead zoom your scene. This is why
a PerspectiveCamera is usually instantiated using the PerspectiveCamera(true), as we will be doing later
during this section of the chapter.
When fixedEyeAtCameraZero is set to true, the eye position is fixed at (0, 0, 0) in the local coordinates
of the camera. The projection matrix will be generated using the default (or specified) fieldOfView attribute,
and a projection volume will be mapped on the window (viewport or Stage object) such that it will be
“stretched” (zoomed) over more or fewer device-independent pixels at the point of the projection plane.
When the Scene size attribute is changed, the objects in a scene will shrink or grow proportionally, but the
visible extent (bounds) of the content will be unchanged.
The JavaFX Development Team recommends setting this fixedEyeAtCameraZero to true if you’re planning
to transform (move or dolly) your camera object. Transforming your camera when fixedEyeAtCameraZero is
set to false may lead to results that are not intuitive to the end user.

Free download pdf