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

After you update your iBagel Bagel() constructor method, with accurate SVG data for your collision polygon,<br />

update the iP0 fixed Actor props, using the coordinates for the four corner pixels, to create a square collision polygon,<br />

as can be seen in Figure 16-25. The upper-left origin corner for any image is going to be 0,0, so the first SVG command<br />

is going to be M0,0, or “moveto origin.” Next, we want to draw a “lineto” using the L command, the lower-left corner,<br />

which would use the L0,32 command and data set, since this brick image is 32 pixels tall (Y), and 72 pixels wide (X).<br />

The next data pair will not need to be prefaced by an L command, since any implementation of an SVG data<br />

parsing algorithm will assume the command used for the previous data pair, if none has been explicitly specified. Your<br />

lower-right corner of the brick image will use X,Y coordinate 72,32. The upper-right corner of this image would use X,Y<br />

coordinate 72,0. The Z command can be used to connect the upper-right corner of this prop image with the origin, so<br />

that we have collision detection on the top of the brick, in this particular use-case, using a closed polygon. As you can<br />

see in Figure 16-25, you can either use commas or spaces with SVG data, so both of these method calls should work:<br />

iPR0 = new Prop("M0 0 L0 32 72 32 72 0 Z", 0, 148, iP0);<br />

iPR0 = new Prop("M0,0 L0,32 72,32 72,0 Z", 0, 148, iP0);<br />

OR:<br />

The second example uses the same amount of space in NetBeans 8, and shows the data point pairs better. I am<br />

going to comment out all the code related to the iP1 Actor Prop object, as you can see in Figures 16-25 and 16-26, so<br />

that these larger Prop Actor objects are “disabled” for now, and do not appear on the Stage (and in the Scene), and will<br />

not interfere with our basic code development for implementing collision detection.<br />

Now we are ready to use the Run ➤ Project work process, and make sure that the InvinciBagel character, as well<br />

as the four golden bricks that we are going to use for testing the collision detection code are in place and that the large<br />

mossy rock objects are no longer anywhere to be seen on the Stage. As you can see in Figure 16-27, we have the Scene<br />

set up for developing the basic collision detection code, and we can now focus on putting this <strong>Java</strong> 8 game code into<br />

place, before we start to work on implementing further Scene designs, game play design, game play logic, physics and<br />

scoring engine code, all of which we will be implementing in the next chapter 17.<br />

Figure 16-27. Use the Run ➤ Project work process and test the game<br />

368<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!