Flying saucers, bouncing cannon balls, and impenetrable mazes--welcome to the interactive world of Java3D game development! In his technical session "Java Technology for Gaming", James Robertson from the Computer Science department at the University of Maryland introduced JavaOne attendees to typical challenges and solutions for collision detection. He showed several collision detection approaches, talked about the implementation of game physics, and finally presented the jMonkeyEngine gaming framework.

Well implemented game physics and collision detection are essential for adding realism to any game. Badly executed physics severely break the immersion and disrupt gameplay--common complaints are that player avatars fall through floors or get stuck in walls or doorframes.

Detecting when two objects collide seems a trivial mathematical task at first. But the more objects you introduce, and the faster they move, the more complex and costly calculations become. Java does provide a Vector3D class, but apart from that you must implement many 3D formulas yourself.

Several approaches to solve the challenge of detecting collisions exist: Overlap testing relies on trivial vector math and can answer the question whether two objects (such as spheres or cubes) intersect at this moment. Intersection testing additionally predicts at what point in time the collision will occur, given a certain trajectory. The third method reduces complexly shaped objects to bounding volumes, either cuboids or spheroids; you can choose between algorithms with axis-aligned bounding boxes (faster, but less precise), and oriented bounded boxes (fits the object's shape better, but is more costly).

And don't underestimate the impact of an object's speed on the precision of collision detection: If the game does not test for collisions often enough, it may miss a high-speed object and allow it to pass through an obstacle. On the other hand there is a upper limit to the detection sampling rate, because the calculations slow down the game. Especially network games suffer from glitches in collision detection when packet latency occurs.

As soon as you venture beyond Pong and Tetris, your game requires more real-world behavior than mere collision detection can offer--we are entering the realms of game physics. Projectiles are affected by gravity. Rolling or colliding objects are slowed down by friction depending on their texture. An impulse should change accordingly after collision. An object's coefficient of restitution determines whether it hits the ground like a rock or bounces off like a ball.

All these changes in a motion's direction and intensity can be described by formulas. If you implement it well, nothing stops you from fiddling with values to add interesting effects to your game, such as low-gravity environments or slippery ground.

Since physics add a lot of overhead to your game, it's time to consider optimization strategies. Dividing up your world into grid partitions allows you to reduce the number of collision checks to objects that are actually adjacent. Another solution is the plane sweep algorithm that looks for changes, frame to frame, for each axis.

Instead of implementing and optimizing this all by yourself, Robertson recommends using the free jMonkeyEngine (jME). jME is a Java 3D game engine that is ready to handle not only collisions and physics, but also particle systems, shaders, terrains, skyboxes, fog, and much more.

The www.jmonkeyengine.com homepage contains documentation and tutorials to get you started with the basics. When you extend the "SimpleGame" and "SimplePhysicsGame" classes, you get a default set-up that lets you get familiar with all features quickly--it even equips your first simple game with a default properties dialog for storing user settings.

Every jME game has an init and an update method. The init method initializes the game: This is were you load sounds, images, objects, and define bounds and controllers. The update method is the game's main event loop. Here you define the game's reaction to user input and collisions, you test for events and trigger state changes. When you have understood how jME works, It's time to start your game project by deriving your own custom game class.

At this point it was time for Robertson to show us the games that his students had created as class assignments: The first was a 3-dimensional asteroid-like space shooter with sound effects. The second was an arcade-style game where you shoot bugs flying past, but spare the bees. In the third you assumed a first person perspective and walked over a 3-dimensional terrain to shoot rolling balloons. Thanks to jME they completed these assignments in only a few weeks. jME handles collision detection for objects as well as terrains, and it even allows you to show the assumed bounding volumes for debugging purposes.

Using an integrated development environment simplifies maintaining the complex project structure and library collections that you will encounter as your game grows. Robertson himself prefers the Netbeans IDE, but other IDEs are valid options, too. In the Q&A section, one of the attendees added a recommendation to use the NetBeans IDE's Web Start feature in the Project Properties for easy game deployment. Which ever deployment method you choose, the package has to include your class files, the jME JARs, and a few native 3-D libraries bundled with jME.

The session gave a great overview of the importance and complexity of collision detection and game physics, and how common challenges have been solved. The most popular and comprehensive solution proposed is the freely available Java3D game framework jMonkeyEngine in combination with a modern IDE. Have fun coding, and don't forget to upload your game to store.java.com soon!

Comments:

Post a Comment:
Comments are closed for this entry.

This blog copyright 2009 by GD