http://java.sun.com/javaone http://java.sun.com/javaone http://java.sun.com/javaone
JavaOne - Experiencing Java technology through education, industry, and community
  2009 Platinum Cosponsor
Intel
  Cosponsors
Cosponsors
  Gen Session Cosponsors
General Session Cosponsors
  Media Sponsors
Media Cosponsors
Home > Java Technology for Gaming: Challenges and Solutions for Collision Detection

Java Technology for Gaming: Challenges and Solutions for Collision Detection


by Ruth Kusterer

Flying saucers, bouncing cannon balls, and impenetrable mazes -- welcome to the interactive world of Java 3D game development. In Java Technology for Gaming: Challenges and Solutions for Collision Detection (TS-4865), 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 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 the calculations become. The Java programming language does provide a Vector3D class, but apart from that, you must implement many 3D formulas yourself.

You can use any of several approaches to solve the challenge of detecting collisions. Overlap testing relies on trivial vector math and can answer the question of 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 (which 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 an upper limit to the detection sampling rate, because the calculations slow down the game. Network games in particular 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 -- you are entering the realm 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 it 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.

Because physics adds a lot of overhead to your game, it's time to consider optimization strategies. Dividing your world up into grid partitions allows you to reduce the number of collision checks to objects that are actually adjacent. Another solution is to use the plane-sweep algorithm to look 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 jME home page contains documentation and tutorials to get you started with the basics. When you extend the SimpleGame and SimplePhysicsGame classes, you get a default setup 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, and 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, test for events, and trigger state changes. Once you understand how jME works, it's time to start your game project by deriving your own custom game class.

At this point, Robertson showed the audience some games that his students had created as class assignments: The first was a 3D 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 3D terrain to shoot rolling balloons. Thanks to jME, Robertson's students 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 (IDE) 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 WebStart feature in the Project Properties for easy game deployment. Whichever deployment method you choose, you must include the package in your class files, the jME JARs, and a few native 3D libraries bundled with jME.

The session gave a great overview of the importance and complexity of collision detection and game physics, and showed how game developers have solved how common challenges. The most popular and comprehensive solution proposed is the freely available Java 3D game framework jME in combination with a modern IDE.

Have fun coding, and be sure to upload your game to the Java Store soon.

For More Information

» Java 3D API
» jMonkeyEngine (jME)
» Java Store

 

Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies - your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.