28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16 ■ Collision Detection: Creating SVG Polygons for the Game Actors and Writing Code to Detect Collision<br />

An example of an “unknown” class would be one of your own customized Bounds subclasses, if you were to write<br />

one, that is. The <strong>Java</strong> 8 class hierarchy for the javafx.geometry.Bounds class would look like the following:<br />

java.lang.Object<br />

> javafx.geometry.Bounds<br />

The Bounds class is used to create Bounds objects. These are used to describe the Bounds of a Node object,<br />

which as we know are <strong>Java</strong>FX Scene Graph Node objects. An important characteristic of a Bounds object is that it can<br />

have a negative width, height, or depth. A negative value for any of the Bounds object attributes (properties) is used<br />

to indicate that the Bounds object is empty. We will be using this in our code later on to ascertain when a collision has<br />

not occurred. As I pointed out earlier in this book, sometimes you have to take the “opposite” approach to finding the<br />

solution, or the proper work process, for achieving your game design and programming objectives.<br />

Using Node Local Bounds: The .getBoundsInLocal() Method<br />

One of the important methods in the <strong>Java</strong>FX Scene Graph Node class is the public final Bounds .getBoundsInLocal()<br />

method. This method is the getter method that retrieves the value of the boundsInLocal property. The boundsInLocal<br />

property is “read-only” and holds a rectangular Bounds object for the Node it is contained in. The data that it contains<br />

represents untransformed (original) local coordinate space for a Node. Untransformed means the Node coordinates<br />

prior to their being rotated, translated, or scaled, which represents the Node object’s original (default) coordinates.<br />

For a Node class (object) that extends the Shape class (which our ImageView Node does not), the local Bounds<br />

will also include the space required to implement a non-zero Shape (or Path) stroke, as this might extend “outside” of<br />

the Shape geometry, which is being defined by these position and size attributes. A local Bounds object also includes<br />

any clipping path areas that you may have set, as well as the extents of any special effects that you may have set.<br />

The boundsInLocal property will always have a non-null value, and it is important to note that this method does<br />

not take the Node object’s visibility into account, so the computation is based on the geometry of the Node only. The<br />

boundsInLocal property is automatically computed whenever the geometry of a Node object changes.<br />

We will be using the .getBoundsInLocal() method on an intersection of our InvinciBagel SVGPath Shape data<br />

with our other Scene Actor Prop SVGPath collision Shape data, in order to ascertain if the width of the intersection is<br />

negative one, as -1 in a Bounds object, as we have learned, is empty, or no intersection, which signifies no collision.<br />

Next, let’s take a look at the boundsInParent property, which contains the boundsInLocal data plus transformations.<br />

Using Node Parent Bounds: The .getBoundsInParent() Method<br />

Another one of those important methods to understand in the <strong>Java</strong>FX Scene Graph Node class is the public<br />

final Bounds .getBoundsInParent() method. This method is the getter method that retrieves the value of the<br />

boundsInParent property. The boundsInParent property is a “read-only” property that holds the rectangular Bounds<br />

object for the Node it is contained in. The data that it contains represents the transformed (modified) coordinate<br />

space for a Node. Transformed means the Node coordinates plus any transforms that have taken place since the Node<br />

object’s default, initial or original state. It is named “boundsInParent” because the Bounds object rectangle data will<br />

need to be relative to the Parent Node object’s coordinate system. This represents the ’visual’ bounds of the Node<br />

object, as in what you see on the screen, after the Node has been moved, rotated, scaled, skewed, and so on.<br />

The boundsInParent property is calculated by taking a local Bounds, defined by the boundsInLocal property,<br />

and applying all of the transforms that have taken place, including any calls to .set() methods for the following Node<br />

properties: scaleX, scaleY, rotate, layoutX, layoutY, translateX, translateY, and transforms (the <strong>Java</strong> ObservableList).<br />

Just like the boundsInLocal property, this boundsInParent property will always contain a non-null value.<br />

373

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!