/Users/lyon/j4p/src/j3d/cr325/KeyBehavior.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    
12   /** 
13    * KeyBehavior is a generic behavior class to take key presses and move a 
14    * TransformGroup through a Java3D scene. The actions resulting from the key strokes 
15    * are modified by using the Ctrl, Alt and Shift keys. 
16    * <p/> 
17    * (version 1.0) reconstructed class to make more generic. 
18    * <p/> 
19    * MODIFIED: 
20    * 
21    * @author Andrew AJ Cain, Swinburne University, Australia 
22    *         <acain@it.swin.edu.au> 
23    *         <p/> 
24    *         edited from code by: 
25    *         Gary S. Moss <moss@arl.mil> 
26    *         U. S. Army Research Laboratory 
27    *         <p/> 
28    *         CLASS NAME: 
29    *         KeyBehavior 
30    *         <p/> 
31    *         PUBLIC FEATURES: 
32    *         // Data 
33    *         <p/> 
34    *         // Constructors 
35    *         <p/> 
36    *         // Methods: 
37    *         <p/> 
38    *         COLLABORATORS: 
39    * @version 1.0, 25 September 1998 aajc 
40    */ 
41   public class KeyBehavior extends Behavior { 
42       protected static final double FAST_SPEED = 2.0; 
43       protected static final double NORMAL_SPEED = 1.0; 
44       protected static final double SLOW_SPEED = 0.1; 
45    
46       private TransformGroup viewTransformGroup; 
47       private Transform3D viewTransform3D; 
48       private WakeupCondition keyCriterion; 
49    
50       private final static double TWO_PI = (2.0 * Math.PI); 
51       private double eps = 32; 
52       private double rotateXAmount = Math.PI / eps; 
53       private double rotateYAmount = Math.PI / eps; 
54       private double rotateZAmount = Math.PI / eps; 
55    
56       private double moveRate = 0.1; 
57       private double speed = NORMAL_SPEED; 
58    
59       private int forwardKey = KeyEvent.VK_UP; 
60       private int backKey = KeyEvent.VK_DOWN; 
61       private int leftKey = KeyEvent.VK_LEFT; 
62       private int rightKey = KeyEvent.VK_RIGHT; 
63       private int spaceKey = KeyEvent.VK_SPACE; 
64    
65       TransformGroup gunTransformGroup = null; 
66       TransformGroup bulletTransformGroup = null; 
67       TransformGroup targetTransformGroup = null; 
68    
69       public void setTargetTransformGroup(TransformGroup targetTransformGroup){ 
70           this.targetTransformGroup = targetTransformGroup; 
71       } 
72    
73       public void setBulletTransformGroup(TransformGroup bulletTransformGroup) { 
74           this.bulletTransformGroup = bulletTransformGroup; 
75       } 
76    
77       public void setGunTransformGroup(TransformGroup gunTransformGroup) { 
78           this.gunTransformGroup = gunTransformGroup; 
79       } 
80    
81       public KeyBehavior(TransformGroup viewTransformGroup) { 
82           this.viewTransformGroup = viewTransformGroup; 
83           viewTransform3D = new Transform3D(); 
84    
85       } 
86    
87       public void initialize() { 
88           WakeupCriterion[] keyEvents = new WakeupCriterion[2]; 
89    
90           keyEvents[0] = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); 
91           keyEvents[1] = new WakeupOnAWTEvent(KeyEvent.KEY_RELEASED); 
92           keyCriterion = new WakeupOr(keyEvents); 
93           wakeupOn(keyCriterion); 
94       } 
95    
96    
97       public void processStimulus(Enumeration criteria) { 
98           WakeupCriterion wakeup; 
99           AWTEvent[] event; 
100   
101          while (criteria.hasMoreElements()) { 
102              wakeup = (WakeupCriterion) criteria.nextElement(); 
103              if (!(wakeup instanceof WakeupOnAWTEvent)) 
104                  continue; 
105   
106              event = ((WakeupOnAWTEvent) wakeup).getAWTEvent(); 
107              for (int i = 0; i < event.length; i++) { 
108                  if (event[i].getID() == KeyEvent.KEY_PRESSED) { 
109                      processKeyEvent((KeyEvent) event[i]); 
110                  } 
111              } 
112          } 
113          wakeupOn(keyCriterion); 
114      } 
115   
116      protected void processKeyEvent(KeyEvent ke) { 
117   
118          int keycode = ke.getKeyCode(); 
119   
120          if (ke.isShiftDown()) 
121              speed = FAST_SPEED; 
122          else 
123              speed = NORMAL_SPEED; 
124          if (ke.isAltDown()) 
125              altMove(keycode); 
126          else if (ke.isControlDown()) 
127              controlMove(keycode); 
128          else 
129              standardMove(keycode); 
130          //moveGun(); 
131      } 
132   
133   
134      //moves forward backward or rotates left right 
135      private void standardMove(int kc) { 
136          System.out.println(kc); 
137          if (kc == forwardKey) 
138              moveForward(); 
139          else if (kc == backKey) 
140              moveBackward(); 
141          else if (kc == leftKey) 
142              rotLeft(); 
143          else if (kc == rightKey) 
144              rotRight(); 
145          else if (kc == spaceKey) 
146              fireBullet(); 
147      } 
148   
149      //moves left right, rotate up down 
150      protected void altMove(int keycode) { 
151          if (keycode == forwardKey) 
152              rotUp(); 
153          else if (keycode == backKey) 
154              rotDown(); 
155          else if (keycode == leftKey) 
156              moveLeft(); 
157          else if (keycode == rightKey) 
158              moveRight(); 
159      } 
160   
161      //move up down, rot left right 
162      protected void controlMove(int keycode) { 
163          if (keycode == forwardKey) 
164              moveUp(); 
165          else if (keycode == backKey) 
166              moveDown(); 
167          else if (keycode == leftKey) 
168              rollLeft(); 
169          else if (keycode == rightKey) 
170              rollRight(); 
171      } 
172   
173      private void moveForward() { 
174          doMove(new Vector3d(0.0, 0.0, -getMovementRate())); 
175      } 
176   
177      private void moveBackward() { 
178          doMove(new Vector3d(0.0, 0.0, getMovementRate())); 
179      } 
180   
181      private void moveLeft() { 
182          doMove(new Vector3d(-getMovementRate(), 0.0, 0.0)); 
183      } 
184   
185      private void moveRight() { 
186          doMove(new Vector3d(getMovementRate(), 0.0, 0.0)); 
187      } 
188   
189      private void moveUp() { 
190          doMove(new Vector3d(0.0, getMovementRate(), 0.0)); 
191      } 
192   
193      private void moveDown() { 
194          doMove(new Vector3d(0.0, -getMovementRate(), 0.0)); 
195      } 
196   
197      protected void rotRight() { 
198          doRotateY(getRotateRightAmount(), getRotateRightAmount()); 
199      } 
200   
201      protected void rotUp() { 
202          doRotateX(getRotateUpAmount(), getRotateUpAmount()); 
203      } 
204   
205      protected void rotLeft() { 
206          doRotateY(getRotateLeftAmount(), getRotateLeftAmount()); 
207      } 
208   
209      protected void rotDown() { 
210          doRotateX(getRotateDownAmount(), getRotateDownAmount()); 
211      } 
212   
213      protected void rollLeft() { 
214          doRotateZ(getRollLeftAmount(), getRollLeftAmount()); 
215      } 
216   
217      protected void rollRight() { 
218          doRotateZ(getRollRightAmount(), getRollRightAmount()); 
219      } 
220   
221      protected void doRotateY(double radians, double theMove) { 
222          viewTransformGroup.getTransform(viewTransform3D); 
223          Transform3D toMove = new Transform3D(); 
224          toMove.rotY(radians); 
225          viewTransform3D.mul(toMove); 
226          viewTransformGroup.setTransform(viewTransform3D); 
227   
228          if (gunTransformGroup == null) return; 
229          Vector3d gunMove = new Vector3d(1, 1, 1); 
230          Transform3D gunTransform3D = new Transform3D(); 
231          gunTransform3D.rotY(radians * theMove); 
232          gunTransform3D.setTranslation(gunMove); 
233          viewTransform3D.mul(gunTransform3D); 
234          gunTransformGroup.setTransform(viewTransform3D); 
235      } 
236   
237      protected void doRotateX(double radians, double theMove) { 
238          viewTransformGroup.getTransform(viewTransform3D); 
239          Transform3D toMove = new Transform3D(); 
240          toMove.rotX(radians); 
241          viewTransform3D.mul(toMove); 
242          viewTransformGroup.setTransform(viewTransform3D); 
243   
244          if (gunTransformGroup == null) return; 
245   
246          Vector3d gunMove = new Vector3d(1, 1, 1); 
247          Transform3D gunTransform3D = new Transform3D(); 
248          gunTransform3D.rotX(radians * theMove); 
249          gunTransform3D.setTranslation(gunMove); 
250          viewTransform3D.mul(gunTransform3D); 
251          gunTransformGroup.setTransform(viewTransform3D); 
252      } 
253   
254      protected void doRotateZ(double radians, double theMove) { 
255          viewTransformGroup.getTransform(viewTransform3D); 
256          Transform3D toMove = new Transform3D(); 
257          toMove.rotZ(radians); 
258          viewTransform3D.mul(toMove); 
259          viewTransformGroup.setTransform(viewTransform3D); 
260   
261          if (gunTransformGroup == null) return; 
262          Vector3d gunMove = new Vector3d(1, 1, 1); 
263          Transform3D gunTransform3D = new Transform3D(); 
264          gunTransform3D.rotZ(radians * theMove); 
265          gunTransform3D.setTranslation(gunMove); 
266          viewTransform3D.mul(gunTransform3D); 
267          gunTransformGroup.setTransform(viewTransform3D); 
268      } 
269   
270      protected void doMove(Vector3d theMove) { 
271          viewTransformGroup.getTransform(viewTransform3D); 
272          Transform3D theMoveTransform3d = new Transform3D(); 
273          theMoveTransform3d.setTranslation(theMove); 
274          viewTransform3D.mul(theMoveTransform3d); 
275          // this is where you need to set the gun 
276          // transform group too: 
277          // gunTransformGroup.setTransform(transform3D); 
278          viewTransformGroup.setTransform(viewTransform3D); 
279          if (gunTransformGroup == null) return; 
280   
281   
282          Vector3d gunMove = new Vector3d(0, 0, 1); 
283   
284          gunMove.add(theMove); 
285   
286          Transform3D gunTransform3D = new Transform3D(); 
287          gunTransform3D.setTranslation(gunMove); 
288          viewTransform3D.mul(gunTransform3D); 
289          gunTransformGroup.setTransform(viewTransform3D); 
290      } 
291   
292      private void fireBullet() { 
293          // fire a bean from pp 9 of chapter 15 of fps 
294          // Get the transform group for the bullet. 
295          Transform3D tempT3d = new Transform3D(); 
296          gunTransformGroup.getTransform(tempT3d); 
297          bulletTransformGroup.setTransform(tempT3d); 
298          //targetSwitch.setWhichChild(0); 
299          new RunJob(.75,false,100){ 
300              public void run() { 
301                  bulletMove(); 
302                 // System.out.println(getBulletTargetDistance()); 
303                  getBulletTargetDistance(); 
304              } 
305          }; 
306      } 
307   
308      public void bulletMove() { 
309          Transform3D tempT3d = new Transform3D(); 
310          bulletTransformGroup.getTransform(tempT3d); 
311          Transform3D toMove = new Transform3D(); 
312          toMove.setTranslation(new Vector3d(0.0, 0, -0.50)); 
313          tempT3d.mul(toMove); 
314          bulletTransformGroup.setTransform(tempT3d); 
315          // to debug, print the distance to the target each 
316          // time you move the bullet. 
317          // check to see if you are closeToTarget 
318          // if you are, then kapowski! 
319          // if not, just return. 
320          // if(closeToTarget()) blammo(); 
321          // blammo targetSwitch.setWhichChild(1); 
322      } 
323   
324      private boolean closeToTarget() { 
325          double sqLen = getBulletTargetDistance(); 
326          if (sqLen < 1) return true; 
327          return false; 
328      } 
329   
330     //  private boolean closeToTarget() 
331    /* The beam is close if its current position (currVec) 
332       is a short distance from the target position (targetVec). 
333    */ 
334   // { 
335    //  beam.getLocalToVworld( localT3d );    // get beam's TG in world coords 
336    //  localT3d.get(currVec);                // get (x,y,z) component 
337      // System.out.println("currVec: " + currVec); 
338   
339    //  currVec.sub(targetVec);    // distance between two positions 
340     // double sqLen = currVec.lengthSquared(); 
341     // if (sqLen < HIT_RANGE*HIT_RANGE) 
342     //   return true; 
343    //  return false; 
344   // }  // end of closeToTarget() 
345     //SceneGraphObject.getLocalToVworld(Transform3D t) 
346     //       Retrieves the local coordinates to 
347     // // virtual world coordinates transform 
348     // for this node in the scene graph. 
349      private double getBulletTargetDistance() { 
350          Vector3d targetVector = new Vector3d();   // location of target 
351          Vector3d bulletVector = new Vector3d(); 
352          Transform3D bulletTransform = new Transform3D(); 
353         // bulletTransformGroup.getTransform(bulletTransform); 
354        bulletTransformGroup.getLocalToVworld( bulletTransform ); 
355        System.out.println(bulletTransform); 
356        // get beam's TG in world coords 
357    //  localT3d.get(currVec);                // get (x,y,z) component 
358      // System.out.println("currVec: " + currVec); 
359          //System.out.println(bulletTransform); 
360          //          bulletTransform.get(bulletVector); 
361          // The following throws an exception, no right to read... 
362          //bulletTransformGroup.getLocalToVworld(bulletTransform); 
363          //bulletTransform.get(bulletVector) 
364          //------------------------------ 
365          Transform3D targetTransform = new Transform3D(); 
366          targetTransformGroup.getTransform(targetTransform); 
367          //targetTransformGroup.getLocalToVworld(targetTransform); 
368   
369          targetTransform.get(targetVector); 
370   
371          bulletVector.sub(targetVector); 
372          double sqLen = Math.sqrt(bulletVector.lengthSquared()); 
373          //System.out.println(sqLen); 
374          return sqLen; 
375      } 
376   
377   
378      protected double getMovementRate() { 
379          return moveRate * speed; 
380      } 
381   
382      protected double getRollLeftAmount() { 
383          return rotateZAmount * speed; 
384      } 
385   
386      protected double getRollRightAmount() { 
387          return -rotateZAmount * speed; 
388      } 
389   
390      protected double getRotateUpAmount() { 
391          return rotateYAmount * speed; 
392      } 
393   
394      protected double getRotateDownAmount() { 
395          return -rotateYAmount * speed; 
396      } 
397   
398      protected double getRotateLeftAmount() { 
399          return rotateYAmount * speed; 
400      } 
401   
402      protected double getRotateRightAmount() { 
403          return -rotateYAmount * speed; 
404      } 
405   
406      public void setRotateXAmount(double radians) { 
407          rotateXAmount = radians; 
408      } 
409   
410      public void setRotateYAmount(double radians) { 
411          rotateYAmount = radians; 
412      } 
413   
414      public void setRotateZAmount(double radians) { 
415          rotateZAmount = radians; 
416      } 
417   
418      public void setMovementRate(double meters) { 
419          moveRate = meters; // Travel rate in meters/frame 
420      } 
421   
422      public void setForwardKey(int key) { 
423          forwardKey = key; 
424      } 
425   
426      public void setBackKey(int key) { 
427          backKey = key; 
428      } 
429   
430      public void setLeftKey(int key) { 
431          leftKey = key; 
432      } 
433   
434      public void setRightKey(int key) { 
435          rightKey = key; 
436      } 
437  } 
438