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 />

A resulting Bounds object will be contained inside of the coordinate space of a Node object’s Parent object,<br />

however, the Node object need not have a Parent (it can be the Scene Graph root of the Scene) in order to be able to<br />

calculate the boundsInParent attribute. Just like the .getBoundsInLocal() method, this method does not take the Node<br />

object’s visibility into account, so don’t make the mistake of thinking you can circumvent your collision detection<br />

code by “hiding” your Actor Node, which, in our case, is an ImageView Node subclass named spriteFrame.<br />

Since it computes changes to the boundsInLocal property, it is logical that the boundsInParent property will be<br />

computed whenever the geometry of a Node changes, or when any transformations of that Node object occur. For<br />

this very reason it would be a mistake to compute any of these values in a Node based on an expression that depends<br />

upon this variable. For example, the X or Y variables of a Node object, or translateX, translateY shouldn’t be computed<br />

using this boundsInParent property for the purpose of positioning the Node, since prior positioning data is included<br />

in this property, and thus would create a circular reference (somewhat akin to the infamous infinite loop scenario).<br />

Using Node Intersection: The .intersects(Bounds object) Method<br />

Another important method in the <strong>Java</strong>FX Scene Graph Node class where collision detection is concerned is the public<br />

boolean intersects(Bounds localBounds) method. This method will return a true value if the Bounds object that<br />

specifies the local coordinate space for the Node object that this method is being called “off of” intersects the Bounds<br />

object passed into the method using the parameter, which is the Bounds object of the Node that you are trying to<br />

determine intersection with. For instance, to determine if the ImageView Bounds that contains the InvinciBagel sprite<br />

intersects the ImageView Bounds that contains one of the Prop sprites, we would use the following <strong>Java</strong> code format:<br />

iBagel.spriteFrame.getBoundsInParent().intersects(object.getSpriteFrame().getBoundsInParent());<br />

It is important to note that just like the .getBoundsInLocal() and .getBoundsInParent() methods, this method<br />

also does not take the Node object’s visibility attribute into account. For this reason, an intersection test will be based<br />

on geometry for the Node objects in question only, and for our ImageView spriteFrame Node objects, which would<br />

be the geometry (dimensions) of their square (InvinciBagel) or rectangular (Prop) Image containers. The default<br />

behavior of the Node class intersects(Bounds localBounds) function is to check and see if local coordinates for a<br />

Bounds object that is passed into this method call intersects with the local Bounds coordinates of the Node that the<br />

.intersects() method call is being called “off of.” Next, let’s take a closer look at the Shape superclass, and its .intersect()<br />

method.<br />

Using Shape Class Intersect: The .intersect( ) Method<br />

Just like a Node class (object) has an intersection method called .intersects() that is used to determine when Node<br />

objects intersect, the Shape class also has an intersection method, which you can use for Shape object intersection.<br />

This method is called .intersect() (rather than intersects), so that these methods use different naming conventions.<br />

This was probably done so developers won’t be quite as confused between Node.intersects() and Shape.intersect()<br />

methods. We will be using both of these in our collision detection code later on in the chapter—first to detect when<br />

an ImageView Node overlaps another ImageView Node, and then to do a Boolean intersection operation between our<br />

SVG data collision polygons (SVGPath objects named spriteBounds within each Actor object).<br />

The static method format for the Shape class’s .intersect() method takes two Shape objects in its parameter<br />

list, and this method returns a new Shape object. The new Shape object that is created represents an intersection of<br />

(between) two input shapes. As you can imagine, you can also use this method to do Boolean intersection generation<br />

for your vector artwork if you are using the Shape class and its subclasses for this purpose. The format for the method<br />

is static Shape intersect(Shape shape1, Shape shape2), and the way that we’re going to use it looks like this:<br />

Shape shape = SVGPath.intersect(invinciBagel.iBagel.getSpriteBound(), object.getSpriteBound());<br />

374<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!