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

Chapter 8. Off of the Actor object we pass into the .collide() method we will method chain the .getBoundsInParent()<br />

method call, resulting in the object.getSpriteFrame().getBoundsInParent() structure, which is then placed on the<br />

inside of an .intersect() method call. The complete conditional if() construct looks like this:<br />

if( invinciBagel.iBagel.spriteFrame.getBoundsInParent().intersects(<br />

object.getSpriteFrame().getBoundsInParent() ) ){second level of collision detection in here}<br />

This first level of collision detection code will check for the intersection of the iBagel spriteFrame ImageView<br />

Node with the spriteFrame ImageView Node object that is contained in each Actor object that will be passed into this<br />

.collide(Actor object) method call that we are coding currently.<br />

This part of the code will check for a “top level” ImageView Node proximity (collision) before we call a more<br />

“expensive” Shape intersection algorithm, which we are going to implement next, in order to confirm more definitive<br />

collision occurrence (SVGPath collision polygon intersection). Remember that the code we’re putting into place<br />

inside of this .collide() method will be processed for each iteration of the for loop (for each cast member) that is<br />

calling this collide(Actor object) method.<br />

The <strong>Java</strong> code inside of this first conditional if() statement creates a Shape object, named intersection, and sets<br />

it equal to the result of an SVGPath.intersect() method call that references the iBagel SVGPath Shape object and an<br />

SVGPath Shape object passed into the collide(Actor object) method. Both objects call a .getSpriteBound() method,<br />

which we created back in Chapter 8, to access the SVGPath Shape objects needed for the .intersect(Shape1, Shape2)<br />

method call format. Your <strong>Java</strong> code, formatted using two lines of code, for readability, should look like the following:<br />

Shape intersection = SVGPath.intersect( invinciBagel.iBagel.getSpriteBound(),<br />

object.getSpriteBound() );<br />

After we have this intersection data, we will use another conditional if() statement to see if that intersection<br />

Shape object contains any collision data, and if it does (that is, if it doesn’t contain a -1 value) a collision has occurred.<br />

The second nested if() statement will utilize the .getBoundsInLocal().getWidth() method chain, called off of<br />

the intersection Shape object, and will check to see if it is empty (returns a -1 value), or if a collision has occurred. The<br />

collision detection will occur if the intersection Shape object’s Bounds object contains any data value other than -1. In<br />

the body of the if() statement, collisionDetect Boolean flag is set to true of any data is present ( signified by != -1). The<br />

<strong>Java</strong> code for the conditional if() statement should look like the following:<br />

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

To test the collision code, I put the invinciBagel.playiSound0() method call inside of the if(collisionDetect){}<br />

conditional statement. This is why I commented out that .playAudioClip() method call in the .update() method, so<br />

that the only thing that I will hear during a collision is the audio playback. This is a quick, easy, and effective way<br />

to test the collision code, at least for now. Since this is the public boolean collide(Actor object) method, I am also<br />

going to put a return true; line of code at the end of the if() body, that returns a true value from the method call.<br />

I am placing the statement inside of this conditional if() construct, so that if needed, we can use this true Boolean<br />

value returned from the .collide() method call in the .update() method for other processing, if we want to. The<br />

very nature of this .collide() method is to detect if a collision has occurred and then return a value, so we could do<br />

further processing inside of the .collide() method, or more efficiently do this on-collision processing inside of the<br />

.checkCollision() method, using an if(collide(object)=true){invinciBagel.playiSound0();} construct, instead<br />

376<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!