Add a first person camera control

The following code demonstrates how to apply a basic prefab-like first person camera control mechanism to the application.


01 package tutorial;
02 import javax.media.opengl.GL;
03 import javax.media.opengl.GLDrawable;
04 
05 import de.rico.engine.game.AbstractBaseGame;
06 import de.rico.engine.input.FirstPersonControls;
07 import de.rico.engine.input.InputManager;
08 
09 /**
10  * A class that shows how to add a basic first person camera control system that
11  * relies on convenience class. 
12  @author Frank Bruns
13  *
14  */
15 public class AddFirstPersonCam extends AbstractBaseGame
16 {
17   private FirstPersonControls fpsCamControl = null;
18   private InputManager inputManager = null;
19   
20   /**
21    * Constructor for the new game.
22    @param title title for the window bar
23    @param maxFps maximum possible framerate
24    */
25   public AddFirstPersonCam(String title, int maxFps)
26   {
27     super(title, maxFps);
28   }
29 
30   /* (non-Javadoc)
31    * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int)
32    */
33   @Override
34   public void initResources(GLDrawable gld, GL gl, int width, int height)
35   {
36     // The first person camera controls are:
37     // W - move forward
38     // A - Strafe left
39     // S - Move backward
40     // D - Move right
41     // Q - Move up
42     // E - Move down
43     // Mouse Movement - Move to the direction of the mouse movement
44     
45     
46     // we have to create an instance of the InputManager class and assign it
47     // to the OpenGL canvas
48     this.inputManager = new InputManager(this.getOGLCanvas());
49     // create an instance of the camera controls convenience class
50     // and assign the input manager object to it
51     this.fpsCamControl = new FirstPersonControls(this.inputManager);
52   }
53 
54   /* (non-Javadoc)
55    * @see de.rico.engine.game.AbstractBaseGame#update(long)
56    */
57   @Override
58   public void update(long elapsedTime)
59   {
60     // update the camera control system
61     this.fpsCamControl.update(elapsedTime);
62   }
63 
64   /* (non-Javadoc)
65    * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL)
66    */
67   @Override
68   public void draw(GL gl)
69   {
70     // nothing to draw!
71   }
72   
73   /**
74    * The usual main method. It's the entry point to the application.
75    @param args argument string
76    */
77   public static void main(String args[])
78   {
79     // create a new game
80     AddFirstPersonCam game = new AddFirstPersonCam("Test Game"100);
81     
82     // initialise the camera --> needs to be done!
83     game.initCamera(0f0f0f0f0f0.1f200f);
84     
85     // show framerate --> this is optional
86     game.setDisplayFPS(true);
87     
88     // finally start the game
89     game.start(game);
90   }
91 }
Java2html