Monkey Fighter Monkey Fighter
Games Help Help Search Search Shop Shop
Asteroids

Lesson 2: Actors

3D Game Programming - Java Programming Language

OpenGL Game Programming For the Web!

Home Page

3D Asteroids Home

Files

Lesson2.zip

Lessons

1. Models
2. Actors
3. Keyboard
4. Fonts
5. Collisions
6. Timing
7. Particles

Play Now!

3D Asteroids Normal
3D Asteroids Tiny
3D Asteroids 800x600
3D Asteroids 1024x768

Find Books

- Java Game Coding
- Game Programming
- Java Programming
- OpenGL



Java Game Programming
Developing Games in Java


Java Game Programming
Java Game Programming


If models are molds, actor are the objects made from those molds. All the visible rocks, bullets and space ships in the game are actors. They can be created, destroyed, rotated or moved at any time during game play. In this lesson, several rocks are created, rotated and moved across the display. They are initialized so that the game engine manages all movement updates. But, before we look at the code, we should look at the examine the actor class. This will reveal the capabilities of actors that are currently implemented.


Actor Field Summary

The following is a list of fields that have public privileges. I know that I am violating proper OOD by allowing these variables to be accessed in this manner. However, this is a personal project with only one developer. So, I believe this practice is an acceptable way to reduce overhead. Further, this program is a game. So, we want to reduce overhead as much as possible. So, please don't let my development technique interfere with your learning enjoyment.


float x, y, z These variables manage the actor's the x, y and z coordinates. Read these values to obtain an actor's coordinates. write to these values to freely move the actor.
float xs, ys, zs These variables manage the actor's speed along the associated plane and are used in conjunction with the game engine's updateActor() method.
float xa, ya, za These variables manage the actor's acceleration along the associated plane and are used in conjunction with the game engine's updateActor() method.
float xr, yr, zr These variables manage the actor's rotation around the associated axis. Read these values to obtain the actor's rotational values. Write these values to freely rotate the actor.
float xrs, yrs, zrs These variables manage the actor's rotational speed around the associated axis and are used in conjunction with the game engine's updateActor() method.
float xra, yra, zra These variables manage the actor's rotational acceleration along the associated axis and are used in conjunction with the game engine's updateActor() method.
float xmin, ymin, zmin These variables manage the clipping region of an actor when the game engines updateActor() method is used. If an actor's coordinate is less than its clipping value it will be moved to its max clipping value.
float xmax, ymax, zmax These variables manage the clipping region of an actor when the game engine's updateActor() method is used. If an actor's coordinate is greater than its clipping value it will be moved to its min clipping value.
int age This variable manage the actor's age when the game engine's updateActor() method is used. Each call to updateActor() method increments this value by one.
int die When an actor's age is greater than die the actor is terminated. This value only works when the game engine's updateActor() method is used. Use it when an actor should only exist for a predetermined time. Otherwise, to manually terminate an actor, it is best to use the killActor() method.


Asteroid

The Code

Getting some rocks displayed and moving on on the screen is very easy. In fact, there are only 18 lines of code which need to be added to Lesson 1 to add any number of moving rocks to the screen. The code below details how this is done.


import java.util.*;

We will be using an ArrayList to manage the arbitrary number of rocks (and bullets). So, add this import statement to your list of imports. This package contains two classes that are needed, List and ArrayList.


  List aBullets = new ArrayList(99);
  List aRocks   = new ArrayList(99);

These two lines are added to the global variables. They will allow us to add any number of bullet and rock actors to the game. Notice that I made the names of the lists plural to indicate that it is a collection of actors. I also initiated the ArrayLists to 99, which is a more actors then I believe will ever be displayed on the screen. However, 99 is not the limit as it would be in an array. An ArrayList is like an array that can grow to any size like a linked list but, unlike a linked list, an ArrayList always has a fixed number of indexed elements. Here we are starting the list with 99 but if more are needed, the class will compensate. The ArrayList is used here instead of a linked list because the ArrayList is claimed to have performance benefits over a Linked List when the list is maintains a fixed size. Here the fixed size is 99. In my playable 3D Asteroids game, you would need get to Level 104 before the aRocks ArrayList is augmented and I don't think it is possible to have more than 99 bullets on the screen simultaneously.


  private void addRocks(int qty)
  { Actor aRock=null;
    float x, y;

    for(int t=0; t<qty; t++)
    { x=(float)(Math.random()*180f)-(90f);  // Random number between -90 and 90
      y=(float)(Math.random()*180f)-(90f);  // Random number between -90 and 90
      aRocks.add(aRock = gEngine.addActor(mRock, x, y, -200f)); // Add new rock
      aRock.xmax=aRock.ymax= 100f;                              // clip rock at 100
      aRock.xmin=aRock.ymin=-100f;                              // clip rock at -100
      aRock.xs  = (float)(Math.random()*0.2f)-(0.1f);           // random x speed
      aRock.ys  = (float)(Math.random()*0.2f)-(0.1f);           // random y speed
      aRock.zrs = (float)(Math.random()*0.5f)-(0.25f);          // random z rotation
      aRock.yrs = (float)(Math.random()*0.5f)-(0.25f);          // random y rotation
  } }

This is the only method that needs to be added. It adds qty rocks at random locations and with random direction and spin. I commented the code so the only line that should need explaining is the one that adds the new rock. What that does is calls addActor() to create a new actor, aRock, which is added to the list of rocks, aRocks. Since aRocks contains a reference to the newly created actor, I didn't have to add the extra assignment to aRock. However, the additional reference, aRock, provides an elegant way to update the clipping, speed and rotation of the new actor. Without aRock, additional code would be needed to parse aRocks for the newly created actor.


  public void run()
  { gEngine = new GameEngine(getSize().width, getSize().height, getCodeBase());
    add("Center", gEngine);
    gEngine.models.add(mPlayer = new Model(getCodeBase() + "Ship.glo"));
    gEngine.models.add(mBullet = new Model(getCodeBase() + "Bullet.glo"));
    gEngine.models.add(mRock   = new Model(getCodeBase() + "Rock1.glo"));
    while(gEngine.modelCount<3) sleep(100);

    aPlayer = gEngine.addActor(mPlayer, 0, 0, -200);

    addRocks(4);              // *** New Line ***

    while(!stop)
    { gEngine.updateActors(); // *** New Line ***
      gEngine.sDisplay();
      sleep(10);
} } }

There are only two new lines left to add to your program. The first one, addRocks(4);, adds four rocks to the game. The second, gEngine.updateActors();, updates the position, rotation, clipping, etc. of all the actors in the game.


Asteroids

Your applet should displays four rocks moving across the display. The player's space craft is visible, motionless and centered in the display.



Copyright ©2005, Robert Walsh, All Rights reserved.