Header Sep
Java Tips, Tricks & Code
Score
Login to rate page
July 2004
Adding 3D effects to your 2D game
 

[Back]

There are two ways of adding 3D effects to your 2D game. You can either use the built-in 3D support (such as found in the K700) or you can simulate 3D by using standard 2D API's.

Furthermore, using the Java 3D support in the phone doesn't necessarily mean that all graphics in a MIDlet have to be 3D. Often it makes more sense just to enhance the ordinary 2D game play with small 3D effects. The use of these small effects means that you can keep your core MIDlet code more or less untouched and still get the benefits of 3D.

3D splash screen
The first, and perhaps easiest, effect is to replace an existing 2D splash screen with a 3D animation. A nice splash screen becomes increasingly important as MIDlets get bigger, use more data and take a longer time to load. An example of a MIDlet with a 3D splash screen can be found in the attached downloadable file. The MIDlet's main structure is more or less the same as the one discussed in the previous tip "Display a Java splash screen", but the MIDlet in this example now uses the Mascot Capsule version 3 API's supported by the K700 to display a simple 3D animation. You could also use the JSR-184 API's instead of the Mascot Capsule v3 API's, as these are also supported in the K700 (the Sony Ericsson SDK with support for JSR-184 is not complete and published yet, but you may download the JSR-184 API's from Sun's website, and later optimize your application for the K700). When changing from a still image to a dynamic animation, it's now vital to ensure that the animation thread gets CPU time during the initialization. Regular calls to Thread.sleep(20) in the initialization thread ensure this.

In the animation thread, the animation is stepped forward frame by frame with the use of a timer in the TimerTask scheduled for repeated fixed-rate execution:

      myTimer.scheduleAtFixedRate(new TimerTask(){ 

      public void run() {
                          // Stop the Splash when
             // initialization has finished    
             if(isInitialized){
                 myTimer.cancel();
              }
                            
              splashCanvas.repaint();

              nowFrame += 65536*3;
              if (nowFrame>=maxFrame) {
                 if (maxFrame==0){
                            nowFrame = 0;
                   }else{
                            nowFrame %= maxFrame;
                   }
                 }
       }
   }, 0L, 100L);

3D menus
There is still a lot more we can do without touching the actual game play. The game's menus can be enhanced with 3D in several ways. Buttons, checkboxes and other standard 2D UI details can all easily be replaced by 3D equivalents, but you could also do a whole new interface in 3D, working in ways that aren't possible with 2D.

Depending on how far you go, the effects may not necessarily require to be created by calls to a 3D engine. For simple effects, you can simply simulate the 3D effects with creative 2D drawing.

3D effects in the game
When moving on to the game play, we really have an opportunity to enhance the graphics; both by simulating 3D with different 2D techniques and by using the real 3D support.

Starting with 2D effects, we can, for example, use a LayerManager to cause transparent layers of clouds or smog to appear to drift in front of each other, thus simulating different distances and creating a sense of depth. The same technique can be applied to the background of a standard horizontal scrolling game by creating two layers of the background and making the front layer move faster than the layer behind it. This is shown in the attached MIDlet.

With real 3D support, you can either let the 3D be an active part in the game or just use it in a more passive manner. Active use means that, for example, a spaceship or car controlled by the user is rendered in 3D and reacts to the user input by moving in 3D. Passive use is when you apply the 3D effect more or less independently of user interaction.

Download 3DsplashScreen.zip>>

Download 3Dscroll.zip>>


 


 


 
Score
Login to rate page