28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

This is the first (easy) part of determining collisions, in this first checkCollision() method, called from inside of<br />

the .update() method, by the pulse event manager. This iterates through the List CURRENT_CAST and calls<br />

the collide(object); method, for each Actor object, to see if the InvinciBagel character has collided with it. As you<br />

can see in Figure 16-31, this <strong>Java</strong> code is error-free, and the next <strong>Java</strong> method body that we will code is the public<br />

boolean collide() method, which we declared for use previously inside of our abstract Hero class, in Chapter 8.<br />

Figure 16-31. Add a for loop, counting from 0 to number of objects in the CURRENT_CAST using .getCurrentCast().size()<br />

Before we code the .collide() method, which is one of the more difficult method bodies that we’ll be coding during<br />

this entire book, we’ll need to look at some of the more complex topics relating to the javafx.scene package’s Node class,<br />

and its Bounds property, and .getBoundsInLocal() and .getBoundsInparent() method call. We will also be looking at<br />

the javafx.scene.shape package’s Shape class, and its .intersect(Shape shape1, Shape shape2) method call. We will<br />

be using all of these inside of our public boolean collide(Actor object) {...} method, so we will need to have this<br />

advanced knowledge in place first, before we write this complex and dense (but exciting) <strong>Java</strong> structure.<br />

Locating a Node Object: Using the Bounds Object<br />

The first thing that we need to look at regarding collision detection is the javafx.geometry package’s Bounds class.<br />

This public abstract class (and the objects that it creates) is utilized in the javafx.scene package’s Node class to hold<br />

the boundaries for the Node. As you may have surmised, this is one of the things that we will leverage to determine<br />

collision detection, in conjunction with our collision SVGPath Shape data, which we will get into after we take a look<br />

at how this Bounds object, and its related .getBoundsInLocal() and .getBoundsInParent() methods, can work for us.<br />

This Bounds class was created from scratch, using the java.lang.Object master class, and contains X, Y, and Z<br />

coordinates, as well as width, height, and depth values. Since we will be working in 2D, we will be using the X and<br />

Y, as well as the width and height, values (properties) of the Bounds object. The Bounds class has a single direct<br />

known subclass, called BoundingBox. In case you are wondering, a “direct subclass” means the BoundingBox class<br />

declaration says BoundingBox extends Bounds, and a “known” class is one that has been officially added into the<br />

<strong>Java</strong> 8 JDK.<br />

372<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!