java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Sitemap
Java Network
Java Forums
Java Tips Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 769
Java SE Tips: 614
Java ME Tips: 201
Java EE Tips: 184
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create a simple driving type game scene using texture mapped cubes E-mail
User Rating: / 5
PoorBest 

Displays a simple driving type game scene, using texture mapped cubes. It assigns an Avatar to the viewer and incorporates simple sounds and collision detection/notification.


Image

import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Vector;

import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BackgroundSound;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Group;
import javax.media.j3d.MediaContainer;
import javax.media.j3d.Node;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.PointSound;
import javax.media.j3d.PositionInterpolator;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Sound;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnCollisionEntry;
import javax.media.j3d.WakeupOnCollisionExit;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Point2f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;

import com.sun.j3d.audioengines.javasound.JavaSoundMixer;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.behaviors.interpolators.RotPosScaleTCBSplinePathInterpolator;
import com.sun.j3d.utils.behaviors.interpolators.TCBKeyFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewerAvatar;

/**
 * Displays a simple driving type game scene, using texture mapped cubes. It
 * assigns an Avatar to the viewer and incorporates simple sounds and collision
 * detection/notification/
 <p>
 * This example does not use the Java3dApplet base class but is based on a
 * SimpleUniverse construction instead. that way we can illustrate the
 * setPlatformGeometry call.
 */
public class AvatarTest extends Applet {

  public BranchGroup createSceneGraph() {
    BranchGroup bg = new BranchGroup();

    TransformGroup tgRoot = addBehaviors(bg);

    createBuildings(tgRoot);
    createRoad(tgRoot);
    createLand(tgRoot);
    createCars(tgRoot);
    createBackground(bg);

    return bg;
  }

  public void createBackground(Group bg) {
    // add the sky backdrop
    Background back = new Background();
    back.setApplicationBounds(getBoundingSphere());
    bg.addChild(back);

    BranchGroup bgGeometry = new BranchGroup();

    // create an appearance and assign the texture image
    Appearance app = new Appearance();
    Texture tex = new TextureLoader("back.jpg"this).getTexture();
    app.setTexture(tex);

    Sphere sphere = new Sphere(1.0f, Primitive.GENERATE_TEXTURE_COORDS
        | Primitive.GENERATE_NORMALS_INWARD, app);

    bgGeometry.addChild(sphere);
    back.setGeometry(bgGeometry);
  }

  public Group createLand(Group g) {
    Land land = new Land(this, g, ComplexObject.GEOMETRY
        | ComplexObject.TEXTURE);
    return land.createObject(new Appearance()new Vector3d(000),
        new Vector3d(111)"land.jpg", null, null);
  }

  public Group createRoad(Group g) {
    Road road = new Road(this, g, ComplexObject.GEOMETRY
        | ComplexObject.TEXTURE);
    return road.createObject(new Appearance()new Vector3d(000),
        new Vector3d(111)"road.jpg", null, null);
  }

  private float getRandomNumber(float basis, float random) {
    return basis + ((floatMath.random() * random * 2(random);
  }

  public Group createBuildings(Group g) {
    BranchGroup bg = new BranchGroup();

    for (int n = (intRoad.ROAD_LENGTH; n < 0; n = n + 10) {
      Building building = new Building(this, bg, ComplexObject.GEOMETRY
          | ComplexObject.TEXTURE | ComplexObject.COLLISION);

      building.createObject(new Appearance()new Vector3d(
          getRandomNumber(-4.0f0.25f), getRandomNumber(1.0f0.5f),
          getRandomNumber(n, 0.5f))new Vector3d(111),
          "house.jpg", null, null);

      building = new Building(this, bg, ComplexObject.GEOMETRY
          | ComplexObject.TEXTURE | ComplexObject.COLLISION);

      building.createObject(new Appearance()new Vector3d(
          getRandomNumber(4.0f0.25f), getRandomNumber(1.0f0.5f),
          getRandomNumber(n, 0.5f))new Vector3d(111),
          "house.jpg", null, null);

    }

    g.addChild(bg);

    return bg;
  }

  public Group createCars(Group g) {
    BranchGroup bg = new BranchGroup();

    for (int n = (intRoad.ROAD_LENGTH; n < 0; n = n + 10) {
      Car car = new Car(this, bg, ComplexObject.GEOMETRY
          | ComplexObject.TEXTURE | ComplexObject.SOUND);

      car.createObject(new Appearance()new Vector3d(getRandomNumber(
          0.0f2.0f), Car.CAR_HEIGHT / 2.0f,
          getRandomNumber(n, 5.0f))new Vector3d(111),
          "car0.jpg""car.wav""collide.wav");
    }

    g.addChild(bg);
    return bg;
  }

  public TransformGroup addBehaviors(Group bgRoot) {
    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    Transform3D zAxis = new Transform3D();
    zAxis.rotY(Math.toRadians(90.0));

    Alpha zoomAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0020000,
        00000);

    PositionInterpolator posInt = new PositionInterpolator(zoomAlpha,
        objTrans, zAxis, 0, -160);

    posInt.setSchedulingBounds(getBoundingSphere());
    objTrans.addChild(posInt);

    bgRoot.addChild(objTrans);

    return objTrans;
  }

  BoundingSphere getBoundingSphere() {
    return new BoundingSphere(new Point3d(0.00.00.0)400.0);
  }

  ViewerAvatar createAvatar() {
    ViewerAvatar va = new ViewerAvatar();
    TransformGroup tg = new TransformGroup();

    Car car = new Car(this, tg, ComplexObject.GEOMETRY
        | ComplexObject.TEXTURE | ComplexObject.COLLISION
        | ComplexObject.COLLISION_SOUND);

    car.createObject(new Appearance()new Vector3d(0, -0.3, -0.3),
        new Vector3d(0.30.31)"platform.jpg", null, "collide.wav");

    tg.addChild(car);
    va.addChild(tg);

    return va;
  }

  public static void main(String[] args) {
    AvatarTest avatarTest = new AvatarTest();

    // Create a simple scene and attach it to the virtual universe
    SimpleUniverse u = new SimpleUniverse();

    PhysicalEnvironment physicalEnv = u.getViewer()
        .getPhysicalEnvironment();

    TransformGroup tg = u.getViewer().getViewingPlatform()
        .getViewPlatformTransform();

    Transform3D t3d = new Transform3D();
    t3d.set(new Vector3f(00.5f0));
    tg.setTransform(t3d);

    CarSteering keys = new CarSteering(tg);
    keys.setSchedulingBounds(avatarTest.getBoundingSphere());

    u.getViewer().setAvatar(avatarTest.createAvatar());

    if (physicalEnv != null) {
      JavaSoundMixer javaSoundMixer = new JavaSoundMixer(physicalEnv);

      if (javaSoundMixer == null)
        System.out.println("Unable to create AudioDevice.");

      javaSoundMixer.initialize();
    }

    // Add everthing to the scene graph - it will now be displayed.
    BranchGroup scene = avatarTest.createSceneGraph();
    scene.addChild(keys);
//    Java3dTree j3dTree = new Java3dTree();

  //  j3dTree.recursiveApplyCapability(scene);

    u.addBranchGraph(scene);

    //j3dTree.updateNodes(u);

    u.getViewingPlatform().getViewPlatform().setActivationRadius(2);
  }
}

/**
 * This class is a simple behavior that invokes the KeyNavigator to modify the
 * view platform transform.
 */

class Building extends ComplexObject {
  private final float BUILDING_WIDTH = 1.0f;

  private final float BUILDING_LENGTH = 1.0f;

  public Building(Component comp, Group g, int nFlags) {
    super(comp, g, nFlags);
  }

  private float getRandomNumber(float basis, float random) {
    return basis + ((floatMath.random() * random * 2(random);
  }

  protected Group createGeometryGroup(Appearance app, Vector3d position,
      Vector3d scale, String szTextureFile, String szSoundFile) {
    int nPrimFlags = 0;

    if ((m_nFlags & ComplexObject.TEXTURE== ComplexObject.TEXTURE) {
      nPrimFlags |= Primitive.GENERATE_TEXTURE_COORDS;
      setTexture(app, szTextureFile);
    }

    return new Box(getRandomNumber(BUILDING_WIDTH, 0.25f),
        (floatposition.y, getRandomNumber(BUILDING_LENGTH, 0.15f),
        nPrimFlags, app);
  }
}

/**
 * This class is a simple behavior that invokes the KeyNavigator to modify the
 * view platform transform.
 */

class Land extends ComplexObject {
  private final float LAND_WIDTH = 100.0f;

  private final float LAND_HEIGHT = 0.0f;

  private final float LAND_LENGTH = -200.0f;

  public Land(Component comp, Group g, int nFlags) {
    super(comp, g, nFlags);
  }

  protected Group createGeometryGroup(Appearance app, Vector3d position,
      Vector3d scale, String szTextureFile, String szSoundFile) {
    QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES
        | GeometryArray.TEXTURE_COORDINATE_2);

    float[] coordArray = -LAND_WIDTH, LAND_HEIGHT, 0, LAND_WIDTH,
        LAND_HEIGHT, 0, LAND_WIDTH, LAND_HEIGHT, LAND_LENGTH,
        -LAND_WIDTH, LAND_HEIGHT, LAND_LENGTH };

    float[] texArray = 0010110};

    quadArray.setCoordinates(0, coordArray, 04);

    if ((m_nFlags & TEXTURE== TEXTURE) {
      quadArray.setTextureCoordinates(00, texArray, 04);
      setTexture(app, szTextureFile);
    }

    Shape3D sh = new Shape3D(quadArray, app);

    BranchGroup bg = new BranchGroup();
    bg.addChild(sh);
    return bg;
  }
}

/**
 * This class is a simple behavior that invokes the KeyNavigator to modify the
 * view platform transform.
 */

class CollisionBehavior extends Behavior {
  private WakeupOnCollisionEntry wakeupOne = null;

  private WakeupOnCollisionExit wakeupTwo = null;

  private WakeupCriterion[] wakeupArray = new WakeupCriterion[2];

  private WakeupCondition wakeupCondition = null;

  private ComplexObject m_Owner = null;

  public CollisionBehavior(Node node, ComplexObject owner) {
    wakeupOne = new WakeupOnCollisionEntry(node,
        WakeupOnCollisionEntry.USE_BOUNDS);
    wakeupTwo = new WakeupOnCollisionExit(node,
        WakeupOnCollisionExit.USE_BOUNDS);

    wakeupArray[0= wakeupOne;
    wakeupArray[1= wakeupTwo;

    wakeupCondition = new WakeupOr(wakeupArray);

    m_Owner = owner;
  }

  /**
   * Override Behavior's initialize method to setup wakeup criteria.
   */
  public void initialize() {
    // Establish initial wakeup criteria
    wakeupOn(wakeupCondition);
  }

  /**
   * Override Behavior's stimulus method to handle the event.
   */
  public void processStimulus(Enumeration criteria) {
    WakeupCriterion genericEvt;

    while (criteria.hasMoreElements()) {
      genericEvt = (WakeupCriterioncriteria.nextElement();

      if (genericEvt instanceof WakeupOnCollisionEntry) {        m_Owner.onCollide(true);
      else if (genericEvt instanceof WakeupOnCollisionExit) {
        m_Owner.onCollide(false);
      }
    }

    // Set wakeup criteria for next time
    wakeupOn(wakeupCondition);
  }
}

/**
 * This class is a simple behavior that invokes the KeyNavigator to modify the
 * view platform transform.
 */

class CarSteering extends Behavior {
  private WakeupOnAWTEvent wakeupOne = null;

  private WakeupCriterion[] wakeupArray = new WakeupCriterion[1];

  private WakeupCondition wakeupCondition = null;

  private final float TRANSLATE_LEFT = -0.05f;

  private final float TRANSLATE_RIGHT = 0.05f;

  TransformGroup m_TransformGroup = null;

  public CarSteering(TransformGroup tg) {
    m_TransformGroup = tg;

    try {
      m_TransformGroup
          .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      m_TransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    catch (Exception e) {
    }

    wakeupOne = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
    wakeupArray[0= wakeupOne;
    wakeupCondition = new WakeupOr(wakeupArray);
  }

  /**
   * Override Behavior's initialize method to setup wakeup criteria.
   */
  public void initialize() {
    // Establish initial wakeup criteria
    wakeupOn(wakeupCondition);
  }

  /**
   * Override Behavior's stimulus method to handle the event.
   */
  public void processStimulus(Enumeration criteria) {
    WakeupOnAWTEvent ev;
    WakeupCriterion genericEvt;
    AWTEvent[] events;

    while (criteria.hasMoreElements()) {
      genericEvt = (WakeupCriterioncriteria.nextElement();

      if (genericEvt instanceof WakeupOnAWTEvent) {
        ev = (WakeupOnAWTEventgenericEvt;
        events = ev.getAWTEvent();
        processAWTEvent(events);
      }
    }

    // Set wakeup criteria for next time
    wakeupOn(wakeupCondition);
  }

  /**
   * Process a keyboard event
   */
  private void processAWTEvent(AWTEvent[] events) {
    for (int n = 0; n < events.length; n++) {
      if (events[ninstanceof KeyEvent) {
        KeyEvent eventKey = (KeyEventevents[n];

        if (eventKey.getID() == KeyEvent.KEY_PRESSED) {
          int keyCode = eventKey.getKeyCode();
          int keyChar = eventKey.getKeyChar();

          Vector3f translate = new Vector3f();

          Transform3D t3d = new Transform3D();
          m_TransformGroup.getTransform(t3d);
          t3d.get(translate);

          switch (keyCode) {
          case KeyEvent.VK_LEFT:
            translate.x += TRANSLATE_LEFT;
            break;

          case KeyEvent.VK_RIGHT:
            translate.x += TRANSLATE_RIGHT;
            break;
          }

          // System.out.println( "Steering: " + translate.x );
          translate.y = 0.5f;

          t3d.setTranslation(translate);
          m_TransformGroup.setTransform(t3d);
        }
      }
    }
  }
}

/**
 * This class is a simple behavior that invokes the KeyNavigator to modify the
 * view platform transform.
 */

class Car extends ComplexObject {
  public static final float CAR_WIDTH = 0.2f;

  public static final float CAR_HEIGHT = 0.2f;

  public static final float CAR_LENGTH = 0.6f;

  public Car(Component comp, Group g, int nFlags) {
    super(comp, g, nFlags);
  }

  private float getRandomNumber(float basis, float random) {
    return basis + ((floatMath.random() * random * 2(random);
  }

  public Bounds getGeometryBounds() {
    return new BoundingSphere(new Point3d(000)0.2);
  }

  protected Group createGeometryGroup(Appearance app, Vector3d position,
      Vector3d scale, String szTextureFile, String szSoundFile) {
    int nPrimFlags = 0;

    if ((m_nFlags & ComplexObject.TEXTURE== ComplexObject.TEXTURE) {
      nPrimFlags |= Primitive.GENERATE_TEXTURE_COORDS;
      setTexture(app, szTextureFile);
    }

    return new Box(CAR_WIDTH, (floatposition.y, getRandomNumber(
        CAR_LENGTH, 0.01f), nPrimFlags, app);
  }
}

/**
 * This class is a simple behavior that invokes the KeyNavigator to modify the
 * view platform transform.
 */

class Road extends ComplexObject {
  public static final float ROAD_WIDTH = 3.0f;

  public static final float ROAD_HEIGHT = 0.01f;

  public static final float ROAD_LENGTH = -200.0f;

  public Road(Component comp, Group g, int nFlags) {
    super(comp, g, nFlags);
  }

  protected Group createGeometryGroup(Appearance app, Vector3d position,
      Vector3d scale, String szTextureFile, String szSoundFile) {
    // creates a segment of road 200 x 2

    QuadArray quadArray = new QuadArray(4, GeometryArray.COORDINATES
        | GeometryArray.TEXTURE_COORDINATE_2);

    float[] coordArray = -ROAD_WIDTH, ROAD_HEIGHT, 0, ROAD_WIDTH,
        ROAD_HEIGHT, 0, ROAD_WIDTH, ROAD_HEIGHT, ROAD_LENGTH,
        -ROAD_WIDTH, ROAD_HEIGHT, ROAD_LENGTH };

    float[] texArray = 0010110};

    quadArray.setCoordinates(0, coordArray, 04);

    if ((m_nFlags & TEXTURE== TEXTURE) {
      quadArray.setTextureCoordinates(00, texArray, 04);
      setTexture(app, szTextureFile);
    }

    Shape3D sh = new Shape3D(quadArray, app);

    BranchGroup bg = new BranchGroup();
    bg.addChild(sh);
    return bg;
  }
}

abstract class ComplexObject extends BranchGroup {
  protected Group m_ParentGroup = null;

  protected int m_nFlags = 0;

  protected BackgroundSound m_CollideSound = null;

  protected Component m_Component = null;

  protected TransformGroup m_TransformGroup = null;

  protected TransformGroup m_BehaviorTransformGroup = null;

  public static final int SOUND = 0x001;

  public static final int GEOMETRY = 0x002;

  public static final int TEXTURE = 0x004;

  public static final int COLLISION = 0x008;

  public static final int COLLISION_SOUND = 0x010;

  public ComplexObject(Component comp, Group group, int nFlags) {
    m_ParentGroup = group;
    m_nFlags = nFlags;
    m_Component = comp;
  }

  public Bounds getGeometryBounds() {
    return new BoundingSphere(new Point3d(000)100);
  }

  private MediaContainer loadSoundFile(String szFile) {
    try {
      File file = new File(System.getProperty("user.dir"));
      URL url = file.toURL();

      URL soundUrl = new URL(url, szFile);
      return new MediaContainer(soundUrl);
    catch (Exception e) {
      System.err.println("Error could not load sound file: " + e);
      System.exit(-1);
    }

    return null;
  }

  protected void setTexture(Appearance app, String szFile) {
    Texture tex = new TextureLoader(szFile, m_Component).getTexture();
    app.setTexture(tex);
  }

  abstract protected Group createGeometryGroup(Appearance app,
      Vector3d position, Vector3d scale, String szTextureFile,
      String szSoundFile);

  protected Group loadGeometryGroup(String szModel, Appearance app)
      throws java.io.FileNotFoundException {
    // load the object file
    Scene scene = null;
    Shape3D shape = null;

    // read in the geometry information from the data file
    ObjectFile objFileloader = new ObjectFile(ObjectFile.RESIZE);

    scene = objFileloader.load(szModel);

    // retrieve the Shape3D object from the scene</