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

Collision Detection: Creating SVG<br />

Polygons for the Game Actors and<br />

Writing Code to Detect Collision<br />

Now that we have implemented digital audio for our game sound effects and short-loop music, as well as<br />

implemented our digital imaging related classes that create motion sprites (characters) and fixed sprites (props),<br />

we will now delve into the other major genre or area of new media: vectors. Vectors are utilized in 2D illustration<br />

software (InkScape) as well as 3D modeling and animation software (Blender), and use math to define the shapes that<br />

are used to create the 2D or 3D artwork. This makes vectors the perfect solution for defining custom collision shapes<br />

that perfectly encase our sprites, so that instead of using a complex array of pixels to detect collision, we use a far<br />

simpler (and far more memory and processor efficient) collision polygon, that will perfectly surround our sprite.<br />

Fortunately for us, the <strong>Java</strong>FX SVGPath class in the javafx.scene.shape package allows us to use custom SVG<br />

Path (Shape) data to define our sprite collision boundaries. Not only that, but this SVGPath class (object) is also<br />

highly efficient, as it has zero properties, only a few methods, and a simple SVGPath() constructor method, as you<br />

have seen already in Chapter 8. This means that using the SVGPath class (object) is relatively memory and processor<br />

efficient. In fact, the only method that we will need to use is the .setContent() method that we used in our Actor class<br />

constructor method in Chapter 8. Since we’ll do this once, at game start-up, the SVG Path collision data will be loaded<br />

into system memory and will be used in our collision detection routines, which we will be putting into place later in<br />

this chapter.<br />

In this chapter we’ll take a detailed look at how the SVG Path data can be defined. This is specified by World<br />

Wide Web (W3) Consortium (also known as W3C), the body who defines HTML5. This specification is on their w3.org<br />

website, and is located at the http://www.w3.org/TR/SVG/paths.html URL, if you want to view it in further detail.<br />

After we look at the SVG or Scalable Vector Graphics (in case you are wondering what this stands for) data<br />

format in detail, we will get into using the popular GIMP 2.8 digital image editing software, which you installed<br />

in Chapter 1, and learn how to create collision polygons. We will also take a look at how to use the PhysEd<br />

(PhysicsEditor) collision polygon generation software. This is from a company called CodeAndWeb GmbH, who<br />

makes professional, and affordable, game assets creation software.<br />

We will go through a collision polygon vector asset creation and optimization process using GIMP 2.8, using both<br />

the quick and dirty work process, which allows GIMP to 100% create the collision polygon shape for you, as well as<br />

a more “involved” work process, where you create your own collision polygon yourself, by hand, using GIMP’s Path<br />

tools. After we have learned how to create collision polygons that are compatible with the <strong>Java</strong>FX SVGPath class, we<br />

will spend the remainder of the chapter on <strong>Java</strong> 8 game programming, creating the collision detection engine (code)<br />

that will allow us to detect when our InvinciBagel character comes into contact with any of the other Actor objects in<br />

the game environment (Scene and Stage objects). This is where the book will start to become more advanced (useful).<br />

343

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

Saved successfully!

Ooh no, something went wrong!