/Users/lyon/j4p/src/j3d/cr325/KeyNavigatorBehaviorGun.java

1    package j3d.cr325; 
2     
3    import gui.run.RunJob; 
4     
5    import javax.media.j3d.*; 
6    import javax.vecmath.Vector3d; 
7    import java.awt.*; 
8    import java.awt.event.KeyEvent; 
9    import java.util.Enumeration; 
10    
11   import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior; 
12    
13    
14   /** 
15    * KeyBehavior is a generic behavior class to take key presses and move a 
16    * TransformGroup through a Java3D scene. The actions resulting from the key strokes 
17    * are modified by using the Ctrl, Alt and Shift keys. 
18    * <p/> 
19    * (version 1.0) reconstructed class to make more generic. 
20    * <p/> 
21    * MODIFIED: 
22    * 
23    * @author Andrew AJ Cain, Swinburne University, Australia 
24    *         <acain@it.swin.edu.au> 
25    *         <p/> 
26    *         edited from code by: 
27    *         Gary S. Moss <moss@arl.mil> 
28    *         U. S. Army Research Laboratory 
29    *         <p/> 
30    *         CLASS NAME: 
31    *         KeyBehavior 
32    *         <p/> 
33    *         PUBLIC FEATURES: 
34    *         // Data 
35    *         <p/> 
36    *         // Constructors 
37    *         <p/> 
38    *         // Methods: 
39    *         <p/> 
40    *         COLLABORATORS: 
41    * @version 1.0, 25 September 1998 aajc 
42    */ 
43   public class KeyNavigatorBehaviorGun extends KeyNavigatorBehavior { 
44       protected static final double FAST_SPEED = 2.0; 
45       protected static final double NORMAL_SPEED = 1.0; 
46       protected static final double SLOW_SPEED = 0.1; 
47    
48       private TransformGroup viewTransformGroup; 
49       private Transform3D viewTransform3D; 
50       private WakeupCondition keyCriterion; 
51    
52       private final static double TWO_PI = (2.0 * Math.PI); 
53       private double eps = 32; 
54       private double rotateXAmount = Math.PI / eps; 
55       private double rotateYAmount = Math.PI / eps; 
56       private double rotateZAmount = Math.PI / eps; 
57    
58       private double moveRate = 0.1; 
59       private double speed = NORMAL_SPEED; 
60    
61       private int forwardKey = KeyEvent.VK_UP; 
62       private int backKey = KeyEvent.VK_DOWN; 
63       private int leftKey = KeyEvent.VK_LEFT; 
64       private int rightKey = KeyEvent.VK_RIGHT; 
65       private int spaceKey = KeyEvent.VK_SPACE; 
66    
67       TransformGroup gunTransformGroup = null; 
68       TransformGroup bulletTransformGroup = null; 
69    
70       public void setBulletTransformGroup(TransformGroup bulletTransformGroup) { 
71           this.bulletTransformGroup = bulletTransformGroup; 
72       } 
73    
74       public void setGunTransformGroup(TransformGroup gunTransformGroup) { 
75           this.gunTransformGroup = gunTransformGroup; 
76       } 
77    
78       public KeyNavigatorBehaviorGun(TransformGroup viewTransformGroup) { 
79           super(viewTransformGroup); 
80           this.viewTransformGroup = viewTransformGroup; 
81           viewTransform3D = new Transform3D(); 
82    
83       } 
84    
85       public void initialize() { 
86           WakeupCriterion[] keyEvents = new WakeupCriterion[2]; 
87    
88           keyEvents[0] = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); 
89           keyEvents[1] = new WakeupOnAWTEvent(KeyEvent.KEY_RELEASED); 
90           keyCriterion = new WakeupOr(keyEvents); 
91           wakeupOn(keyCriterion); 
92       } 
93    
94    
95       public void processStimulus(Enumeration criteria) { 
96           if (isSpace(criteria)) 
97            fireBullet(); 
98           else super.processStimulus(criteria); 
99       } 
100      boolean isSpace(Enumeration criteria){ 
101               WakeupCriterion wakeup; 
102          AWTEvent[] event; 
103   
104          while (criteria.hasMoreElements()) { 
105              wakeup = (WakeupCriterion) criteria.nextElement(); 
106              if (!(wakeup instanceof WakeupOnAWTEvent)) 
107                  continue; 
108   
109              event = ((WakeupOnAWTEvent) wakeup).getAWTEvent(); 
110              for (int i = 0; i < event.length; i++) { 
111                  if (event[i].getID() == KeyEvent.KEY_PRESSED) { 
112                      return isSpace((KeyEvent) event[i]); 
113                  } 
114              } 
115          } 
116          return false; 
117      } 
118      boolean isSpace(KeyEvent ke){ 
119          return (ke.getKeyCode() == spaceKey); 
120      } 
121      protected void processKeyEvent(KeyEvent ke) { 
122   
123          int keycode = ke.getKeyCode(); 
124   
125          if (ke.isShiftDown()) 
126              speed = FAST_SPEED; 
127          else 
128              speed = NORMAL_SPEED; 
129          if (ke.isAltDown()) 
130              altMove(keycode); 
131          else if (ke.isControlDown()) 
132              controlMove(keycode); 
133          else 
134              standardMove(keycode); 
135          //moveGun(); 
136      } 
137   
138   
139      //moves forward backward or rotates left right 
140      private void standardMove(int kc) { 
141          System.out.println(kc); 
142          if (kc == forwardKey) 
143              moveForward(); 
144          else if (kc == backKey) 
145              moveBackward(); 
146          else if (kc == leftKey) 
147              rotLeft(); 
148          else if (kc == rightKey) 
149              rotRight(); 
150          else if (kc == spaceKey) 
151              fireBullet(); 
152      } 
153   
154      //moves left right, rotate up down 
155      protected void altMove(int keycode) { 
156          if (keycode == forwardKey) 
157              rotUp(); 
158          else if (keycode == backKey) 
159              rotDown(); 
160          else if (keycode == leftKey) 
161              moveLeft(); 
162          else if (keycode == rightKey) 
163              moveRight(); 
164      } 
165   
166      //move up down, rot left right 
167      protected void controlMove(int keycode) { 
168          if (keycode == forwardKey) 
169              moveUp(); 
170          else if (keycode == backKey) 
171              moveDown(); 
172          else if (keycode == leftKey) 
173              rollLeft(); 
174          else if (keycode == rightKey) 
175              rollRight(); 
176      } 
177   
178      private void moveForward() { 
179          doMove(new Vector3d(0.0, 0.0, -getMovementRate())); 
180      } 
181   
182      private void moveBackward() { 
183          doMove(new Vector3d(0.0, 0.0, getMovementRate())); 
184      } 
185   
186      private void moveLeft() { 
187          doMove(new Vector3d(-getMovementRate(), 0.0, 0.0)); 
188      } 
189   
190      private void moveRight() { 
191          doMove(new Vector3d(getMovementRate(), 0.0, 0.0)); 
192      } 
193   
194      private void moveUp() { 
195          doMove(new Vector3d(0.0, getMovementRate(), 0.0)); 
196      } 
197   
198      private void moveDown() { 
199          doMove(new Vector3d(0.0, -getMovementRate(), 0.0)); 
200      } 
201   
202      protected void rotRight() { 
203          doRotateY(getRotateRightAmount(), getRotateRightAmount()); 
204      } 
205   
206      protected void rotUp() { 
207          doRotateX(getRotateUpAmount(), getRotateUpAmount()); 
208      } 
209   
210      protected void rotLeft() { 
211          doRotateY(getRotateLeftAmount(), getRotateLeftAmount()); 
212      } 
213   
214      protected void rotDown() { 
215          doRotateX(getRotateDownAmount(), getRotateDownAmount()); 
216      } 
217   
218      protected void rollLeft() { 
219          doRotateZ(getRollLeftAmount(), getRollLeftAmount()); 
220      } 
221   
222      protected void rollRight() { 
223          doRotateZ(getRollRightAmount(), getRollRightAmount()); 
224      } 
225   
226      protected void doRotateY(double radians, double theMove) { 
227          viewTransformGroup.getTransform(viewTransform3D); 
228          Transform3D toMove = new Transform3D(); 
229          toMove.rotY(radians); 
230          viewTransform3D.mul(toMove); 
231          viewTransformGroup.setTransform(viewTransform3D); 
232   
233          if (gunTransformGroup == null) return; 
234          Vector3d gunMove = new Vector3d(1, 1, 1); 
235          Transform3D gunTransform3D = new Transform3D(); 
236          gunTransform3D.rotY(radians * theMove); 
237          gunTransform3D.setTranslation(gunMove); 
238          viewTransform3D.mul(gunTransform3D); 
239          gunTransformGroup.setTransform(viewTransform3D); 
240      } 
241   
242      protected void doRotateX(double radians, double theMove) { 
243          viewTransformGroup.getTransform(viewTransform3D); 
244          Transform3D toMove = new Transform3D(); 
245          toMove.rotX(radians); 
246          viewTransform3D.mul(toMove); 
247          viewTransformGroup.setTransform(viewTransform3D); 
248   
249          if (gunTransformGroup == null) return; 
250   
251          Vector3d gunMove = new Vector3d(1, 1, 1); 
252          Transform3D gunTransform3D = new Transform3D(); 
253          gunTransform3D.rotX(radians * theMove); 
254          gunTransform3D.setTranslation(gunMove); 
255          viewTransform3D.mul(gunTransform3D); 
256          gunTransformGroup.setTransform(viewTransform3D); 
257      } 
258   
259      protected void doRotateZ(double radians, double theMove) { 
260          viewTransformGroup.getTransform(viewTransform3D); 
261          Transform3D toMove = new Transform3D(); 
262          toMove.rotZ(radians); 
263          viewTransform3D.mul(toMove); 
264          viewTransformGroup.setTransform(viewTransform3D); 
265   
266          if (gunTransformGroup == null) return; 
267          Vector3d gunMove = new Vector3d(1, 1, 1); 
268          Transform3D gunTransform3D = new Transform3D(); 
269          gunTransform3D.rotZ(radians * theMove); 
270          gunTransform3D.setTranslation(gunMove); 
271          viewTransform3D.mul(gunTransform3D); 
272          gunTransformGroup.setTransform(viewTransform3D); 
273      } 
274   
275      protected void doMove(Vector3d theMove) { 
276          viewTransformGroup.getTransform(viewTransform3D); 
277          Transform3D theMoveTransform3d = new Transform3D(); 
278          theMoveTransform3d.setTranslation(theMove); 
279          viewTransform3D.mul(theMoveTransform3d); 
280          // this is where you need to set the gun 
281          // transform group too: 
282          // gunTransformGroup.setTransform(transform3D); 
283          viewTransformGroup.setTransform(viewTransform3D); 
284          if (gunTransformGroup == null) return; 
285   
286   
287          Vector3d gunMove = new Vector3d(1, 1, 1); 
288   
289          gunMove.add(theMove); 
290   
291          Transform3D gunTransform3D = new Transform3D(); 
292          gunTransform3D.setTranslation(gunMove); 
293          viewTransform3D.mul(gunTransform3D); 
294          gunTransformGroup.setTransform(viewTransform3D); 
295      } 
296   
297      private void fireBullet() { 
298          // fire a bean from pp 9 of chapter 15 of fps 
299          // Get the transform group for the bullet. 
300          Transform3D tempT3d = new Transform3D(); 
301          gunTransformGroup.getTransform(tempT3d); 
302          bulletTransformGroup.setTransform(tempT3d); 
303          new RunJob(0.5,false,1000){ 
304              public void run() { 
305                  bulletMove(); 
306              } 
307          }; 
308      } 
309   
310      public void bulletMove() { 
311          Transform3D tempT3d = new Transform3D(); 
312          bulletTransformGroup.getTransform(tempT3d); 
313          Transform3D toMove = new Transform3D(); 
314          toMove.setTranslation(new Vector3d(0.0, 0, -0.050)); 
315          tempT3d.mul(toMove); 
316          bulletTransformGroup.setTransform(tempT3d); 
317      } 
318   
319   
320      protected double getMovementRate() { 
321          return moveRate * speed; 
322      } 
323   
324      protected double getRollLeftAmount() { 
325          return rotateZAmount * speed; 
326      } 
327   
328      protected double getRollRightAmount() { 
329          return -rotateZAmount * speed; 
330      } 
331   
332      protected double getRotateUpAmount() { 
333          return rotateYAmount * speed; 
334      } 
335   
336      protected double getRotateDownAmount() { 
337          return -rotateYAmount * speed; 
338      } 
339   
340      protected double getRotateLeftAmount() { 
341          return rotateYAmount * speed; 
342      } 
343   
344      protected double getRotateRightAmount() { 
345          return -rotateYAmount * speed; 
346      } 
347   
348      public void setRotateXAmount(double radians) { 
349          rotateXAmount = radians; 
350      } 
351   
352      public void setRotateYAmount(double radians) { 
353          rotateYAmount = radians; 
354      } 
355   
356      public void setRotateZAmount(double radians) { 
357          rotateZAmount = radians; 
358      } 
359   
360      public void setMovementRate(double meters) { 
361          moveRate = meters; // Travel rate in meters/frame 
362      } 
363   
364      public void setForwardKey(int key) { 
365          forwardKey = key; 
366      } 
367   
368      public void setBackKey(int key) { 
369          backKey = key; 
370      } 
371   
372      public void setLeftKey(int key) { 
373          leftKey = key; 
374      } 
375   
376      public void setRightKey(int key) { 
377          rightKey = key; 
378      } 
379  } 
380