Game Development Reference
In-Depth Information
Locating a Node Object: Using the
Bounds Object
The first thing that we need to look at regarding collision detection is the
javafx.geometry
package's
Bounds
class. This
public abstract
class (and the objects
that it creates) is utilized in the
javafx.scene
package's
Node
class to hold the
bound-
aries
for the Node. As you may have surmised, this is one of the things that we will
leverage to determine collision detection, in conjunction with our collision SVGPath
Shape data, which we will get into after we take a look at how this Bounds object, and
its related
.getBoundsInLocal()
and
.getBoundsInParent()
methods, can work for us.
This Bounds class was created from scratch, using the java.lang.Object master
class, and contains
X
,
Y
, and
Z
coordinates, as well as
width
,
height
, and
depth
val-
ues. Since we will be working in 2D, we will be using the X and Y, as well as the width
and height, values (properties) of the Bounds object. The Bounds class has a single dir-
ect known subclass, called BoundingBox. In case you are wondering, a “direct sub-
class” means the BoundingBox class declaration says BoundingBox
extends
Bounds,
and a “known” class is one that has been
officially
added into the Java 8 JDK.
An example of an “unknown” class would be one of your own customized Bounds
subclasses, if you were to write one, that is. The Java 8 class hierarchy for the
javafx.geometry.Bounds class would look like the following:
java.lang.Object
> javafx.geometry.
Bounds
The Bounds class is used to create Bounds objects. These are used to describe the
Bounds of a Node object, which as we know are JavaFX Scene Graph Node objects.
An important characteristic of a Bounds object is that it can have a
negative
width,
height, or depth. A negative value for any of the Bounds object attributes (properties) is
used to indicate that the Bounds object is
empty
. We will be using this in our code later
on to ascertain when a collision has
not
occurred. As I pointed out earlier in this topic,
sometimes you have to take the “opposite” approach to finding the solution, or the
proper work process, for achieving your game design and programming objectives.
Using Node Local Bounds: The .getBoundsInLocal()
Method