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