FlightSim (Java3D)

The requirement for this project was:

Build a Java3D scene that demonstrates how a user can interact with a scene and modify it.

I chose to create a basic flight simulator to demonstrate the various ways that user interaction (using mouse, keyboard, menus, etc) can alter the world. Note: if I was seriously trying to make a game or a flight simulator, I wouldn’t use Java3D, and I wouldn’t leave the application in a beta stage like this! :)

The idea of the game is to fly around the world and try to score points by clicking/shooting meteors, all while avoiding collisions with objects in the world.

Running Application

Click here, or the above image to run application right from your browser with no setup neccessary. However, Java3D applications must be signed to run via Java Web Start, so you’ll have to accept my unverified digital certificate which I used to sign the JAR. Alternatively, you may download the source below and compile/run yourself.

Controls

Using the controls properly allows for the realistic (within reason) movement of the plane:

  • Arrow keys: to turn left/right or move forward/back (this is a special plane that can go backwards ;))
  • ALT + an arrow key: to strafe left/left, or look up/down
  • CTRL + an arrow key: rolls around, or shifts up and down
  • Mouse click: click on the meteors to burn them, increasing your score

For example, to take off, press ALT + the up arrow to aim at an angle upwards, then press up. To do a roll, press CTRL + the left/right arrow key. To shift left/right, press ALT + the left/right arrow key.

Menu

The user can turn the background on and off using the “Background” drop-down menu. They can exit by going to “File”, then “Exit” (the application can also handle a close request from the operating system (for example, pressing ALT+F4 on Windows)).

Interactivity

This aim of this project is to demonstrate Java3D interactivity, and so I have implemented what I feel are the three main areas of user interaction.

Mouse Interaction

The mouse is used to shoot (or more technically, “pick”) meteor objects within the scene. Adding provided utility behaviours, to rotate/translate objects, etc, is very simplebut not too useful with regards to this game – and hence they are not in place.

Keyboard Interaction

The keyboard controls are the most interesting form of interaction within this game, as it allows for various manoeuvres of the plane by using CTRL and ALT, as described above. The user can do nose-dives, barrel-roles, and rudder-shifts, etc, whilst flying across the scene and shooting meteors.

Menu Interaction

The menu was provided as a further example of how one can interact with a Java3D program. It allows the user to turn on and off a background. Adding other features, like changing the grass to rock, adding new meteors, or creating extra runways, would be very easy.

Collisions

The plane can collide with everything in the scene (the land, runway, meteors), except the sky. Each time colision is detected, the user’s “Collision” count increases. Nothing realistic actually happens when the user collides, except that they are notified.

Scene Design

The Java3D scene is basically a large field with a runway and a sky background. There are randomly-sized spinning meteors placed throughout the scene in random places. Several textures are used to create a more realistic looking world.

Implementation

I have attempted to demonstrate a solid and thorough understanding of the major components and funtionality with regards to interaction of Java3D. The code is commented well and provides a much better demonstration of this.

Class Summary

RunFlightSim

RunFlightSim is the entry class and is responsible for initially setting everything up and then creating and displaying the GUI.

It creates and adds an ‘avatar’; how the user’s “virtual self” appears in the virtual world. In this case, the avatar consists of the cockpit, a collision count, and a score count. These remain static throughout (apart from the counts increasing).

Next, it proceeds to create and add Land, Road, and Background objects to the scene. Then, it is responsible for randomly placing random-sized Meteor objects across the scene.

RunFlightSim also adds a PlaneControls behaviour to the overall scene, to allow keyboard interaction, and then an instance of PickBehaviour, thus allowing objects to be pickable.

It is RunFlightSim which also allows the background to be changed, and for the score and collision counts to be set and read.

Land

Land, which extends TransformGroup, creates a textured QuadArray to represent a large peace of land.

It sets its own ‘capabilities’, thus allowing it to be ‘collidable’. After this, it adds a CollideBehaviour to itself, so collisions can be detected. In doing so, it also hands the CollideBehaviour a reference to a Text2D object (which it was originally handed from RunFlightSim), which is the graphical display of the collision score. Now when CollideBehaviour detects a collision with the Land, it can increase the count.

Runway

Runway, which extends TransformGroup, creates a textured QuadArray to represent a long runway.

It sets its own ‘capabilities’, thus allowing it to be ‘collidable’. After this, it adds a CollideBehaviour to itself, so collisions can be detected. In doing so, it also hands the CollideBehaviour a reference to a Text2D object (which it was originally handed from RunFlightSim), which is the graphical display of the collision score. Now when CollideBehaviour detects a collision with the Runway, it can increase the count.

Meteor

Meteor, which extends TransformGroup, creates a textured Sphere to represent a meteor. When instantiated, it is handed random values in order to position itself randomly on the scene, with a random size.

The Sphere is given the capability to be “picked” and also to have its Appearance modified. The Sphere is then added to a new TransformGroup which has its capabilities set in such a way to allow re-writing of its transform information. The RotationInterpolator object is then added, which makes the meteor continuously spin.

The Meteor also sets its own ‘capabilities’, thus allowing it to be ‘collidable’. Next, it adds a CollideBehaviour to itself. In doing so, it hands the CollideBehaviour a reference of a Text2D (which is the graphical display of the collision score). These are provided so the CollideBehaviour can increase the collision score as necessary.

CollideBehaviour

CollideBehaviour extends Behaviour and is responsible for detecting and dealing with collisions. It uses the USE_GEOMETRY capability to detect geometry collisions (rather than by checking a Bound, say). The Land, Runway, and Meteor objects have all registered with a CollideBehaviour, and so when the user collides with any of these objects, the CollideBehaviour detects it.

Upon detection, it updates a collision count, using a static method from RunFlightSim. It then proceeds to update the Text2D (displayed on the top right of the avatar) which represents the collision count.

PickBehaviour

PickBehaviour extends PickMouseBehavior and is responsible for detecting and dealing with mouse-clicks. The PickMouseBehavior class is the Java3D’s base class for mouse picking behaviors. It is originally added to the overall scene in RunFlightSim. Upon detection of a click, a method (updateScene()) is invoked and handed the x and y coordinates of the click. PickCanvas, a subclass of PickTool, simplifies figuring out which Java3D object the user clicked by using the x and y coordinates from Canvas’s mouse events. It chooses to pick the ‘closest’ shape, as this is what the user would have intended.

If PickBehaviour determines that it was a meteor which was clicked, it changes the metero’s Appearance node, and marks it as shot, so as to not re-count it if re-shot. Finally, the Text3D is updated on the avatar to reflect the new score.

PlaneControls

PlaneControls is a class that extends Behavior, which detects and processes key presses. It is used to control the plane. When being created, it is handed the TransformGroup to which it should act on (in this case, the world).

Upon key detection, PlaneControls carries out the relevant transform on the TransformGroup.

As stated earlier, the user can move in a number of ways.

  • Arrow keys: to turn left/right or move forward
  • ALT + an arrow key: to strafe left/left, or look up/down
  • CTRL + an arrow key: rolls around, or shifts up and down
  • Mouse click: click on the meteors to burn them, increasing your score

There exits another utility behaviour class called KeyNavigatorBehavior, which allows the user to modify the view platform transform, just like PlaneControls. Its main advantage is that it is very smooth. However, it is very limited, and would not allow for the functionality which PlaneControls provides.

Requirements

Files and Running

Or click here to download them all.

To run, simply compile all classes (ensuring the Java3D JARs are on your classpath) and run RunFlightSim.