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

The reason that the Shape class’s .intersect() method works so well for our collision detection application is<br />

because we can later use a .getBoundsInLocal().getWidth() method chain, to determine if a collision has occurred,<br />

or rather, if it has not occurred by looking for a -1 empty value. We’ll call this method chain that looks for a -1 off of<br />

this new Shape object, which we will name intersection, in our .collide(object) method, using the following line of<br />

code:<br />

if (intersection.getBoundsInLocal().getWidth() != -1) { collisionDetect = true; }<br />

The way this .intersect() method works is that it only processes the geometric data that occupies your input<br />

shapes, which in our application is the spriteBound SVGPath object inside of our Actor objects. This is why we’re<br />

using an SVGPath Shape object (for maximum collision cage or polygon definition), purely for its geometric data<br />

values, and why we’ve installed this spriteBounds SVGPath Shape object container in our Actor object design, because<br />

we intend on using this geometric data for our collision detection, and not for vector illustration artwork (we are not<br />

stroking, or filling, the SVGPath, for instance, and please, no subtle jokes here folks, we’re learning game development<br />

right now).<br />

What this means is that the geometric areas of the input Shape objects being considered by the algorithm in the<br />

.intersect() method is “math based” only. This means the algorithm is independent of the type of Shape (subclass)<br />

being processed, as well as independent of configuration for the Paint object being used for filling or stroking (please,<br />

again briefly resist the temptation to snicker one more time, for the sake of learning <strong>Java</strong> 8 game development).<br />

Before the final intersection calculation, the areas of the input Shape objects are transformed to the Parent Node<br />

object’s coordinate space (think: boundsInParent). In this way, the resulting shape will only include those areas<br />

(mathematical geometric areas, more precisely) which were contained in the areas of the two input Shape objects<br />

passed into the method using the parameter list. Therefore, if there’s any data (something other than a -1 empty data<br />

value) in the resulting Shape object, which we will be calling intersection in the code we will be writing next, there has<br />

been some level of intersection, and even a small area of intersection (overlap) needs to signal a detected collision.<br />

Now we are ready to use the technical <strong>Java</strong> 8 class and method information that we have learned over the past<br />

several pages of the chapter to create the core collision detection logic using only around a dozen lines of <strong>Java</strong> code.<br />

We’ll add another dozen lines of code before the end of the chapter, to process the collision for our game play.<br />

Overriding the Abstract Hero Class: .collide( ) Method<br />

Finally the time has come to override and implement the public boolean collide (Actor object) method that we<br />

installed in our abstract Hero class back in Chapter 8. This is a critical method to our game play, as it determines when<br />

our primary InvinciBagel character comes into contact with other elements in the game. The result of this contact is<br />

scoring, as well as things changing visually on the game screen, so it becomes pivotal to everything that we will be<br />

doing in the next chapter to implement game play elements into the InvinciBagel game. The first thing we want to do<br />

is to install a Boolean “flag” variable called collisionDetect, and set it equal to a false value (collision not detected)<br />

at the top of the .collide() method. The <strong>Java</strong> code for this statement setting up the collisionDetect flag should look like<br />

the following:<br />

boolean collisionDetect = false;<br />

The next step in determining if a collision has occurred is to use a <strong>Java</strong> conditional if() statement. This allows<br />

us to test if the ImageView Node objects that contain the sprite Image assets have intersected by using the Node<br />

class .intersects() method call in conjunction with the .getBoundsInParent() method call off of each spriteFrame<br />

ImageView Node object. The first spriteFrame.getBoundsInParent() method call is the one that we method chain<br />

the .intersects() method call off of, since what we are trying to do is to ascertain collision with our primary game<br />

character. Since we want to reference the invinciBagel object (InvinciBagel class) and its iBagel Actor object, this<br />

construct would take the form of an invinciBagel.iBagel.spriteFrame.getBoundsInParent().intersects() method<br />

call structure. Since the Bounds object from the Actor object that we are testing for collision needs to be inside of the<br />

.intersects(Bounds localBounds) method call, we need to use the .getSpriteFrame() method call we developed in<br />

www.it-ebooks.info<br />

375

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

Saved successfully!

Ooh no, something went wrong!