001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/vpbehaviors/FlyBehavior.java,v 1.1 2005/04/20 21:05:12 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.vpbehaviors;
019:
020: import java.awt.event.ComponentEvent;
021: import java.awt.event.MouseEvent;
022: import java.awt.event.KeyEvent;
023: import java.awt.AWTEvent;
024: import java.awt.Component;
025: import java.awt.Cursor;
026: import javax.swing.SwingUtilities;
027:
028: import javax.media.j3d.WakeupOnAWTEvent;
029: import javax.media.j3d.WakeupOnElapsedFrames;
030: import javax.media.j3d.WakeupOr;
031: import javax.media.j3d.WakeupCriterion;
032: import javax.media.j3d.WakeupCondition;
033: import javax.media.j3d.TransformGroup;
034: import javax.media.j3d.Transform3D;
035: import javax.media.j3d.View;
036:
037: import javax.vecmath.Vector3f;
038:
039: /**
040: *<P>This behavior will allow the user to move to any point with any
041: orientation in 3 space.</P>
042: <P>All controls are provided by the mouse. Each mouse button
043: generates a different type of motion while the button is pressed, the
044: distance of the mouse cursor from the center of the canvas3D will
045: control the speed of motion. The behavior of each button is described
046: below with reference to the View coordinate system.</P>
047: <P>Left Mouse Button - Moving the mouse up and down will move the
048: view forward and backward. Moving the mouse left and right will yaw
049: the view around the Y axis.</P>
050: <P>Right Mouse Button - Moving the mouse up and down will move the
051: view up and down the Y axis. Movig the mouse left and right will
052: translate the view along the X axis.</P>
053: <P>Middle Mouse Button - Moving the mouse up and down will pitch the
054: view around the X axis. Moving the mouse left and right will roll the
055: view around the Z axis.</P>
056:
057: * Canvas3D size changes are tracked
058: *
059: * @author Paul Byrne
060: * @version 1.14, 01/18/02
061: */
062: public class FlyBehavior extends J3dFlyMouseBehavior {
063:
064: private Transform3D velocityTransform;
065: private Transform3D yawTransform;
066: private Transform3D rollTransform;
067: private Transform3D pitchTransform;
068: private Transform3D newTargetTransform = new Transform3D();
069:
070: private Vector3f velocity = new Vector3f();
071: private float yawAngle = 0f;
072: private float pitchAngle = 0f;
073: private float rollAngle = 0f;
074:
075: /**
076: * @deprecated use FlyBehavior()
077: */
078: public FlyBehavior(View view) {
079: this ();
080: }
081:
082: public FlyBehavior() {
083: super ();
084:
085: rollTransform = new Transform3D();
086: pitchTransform = new Transform3D();
087: yawTransform = new Transform3D();
088: targetTransform = new Transform3D();
089: velocityTransform = new Transform3D();
090: }
091:
092: public void initialize() {
093: super .initialize();
094: }
095:
096: protected void processAWTEvents(final java.awt.AWTEvent[] events) {
097: for (int i = 0; i < events.length; i++)
098: if (events[i] instanceof MouseEvent) {
099: if (((MouseEvent) events[i]).getID() == MouseEvent.MOUSE_RELEASED)
100: processMouseReleased((MouseEvent) events[i]);
101: else if (((MouseEvent) events[i]).getID() == MouseEvent.MOUSE_DRAGGED)
102: processMouseDragged((MouseEvent) events[i]);
103: else
104: processUnusedEvent(events[i]);
105: } else
106: processUnusedEvent(events[i]);
107: }
108:
109: protected void processMouseReleased(final MouseEvent evt) {
110: if (SwingUtilities.isLeftMouseButton(evt)) {
111: yawAngle = 0;
112: velocity.z = 0;
113: } else if (SwingUtilities.isRightMouseButton(evt)) {
114: velocity.x = 0;
115: velocity.y = 0;
116: } else if (SwingUtilities.isMiddleMouseButton(evt)) {
117: pitchAngle = 0f;
118: rollAngle = 0f;
119: } else {
120: yawAngle = 0f;
121: pitchAngle = 0f;
122: rollAngle = 0f;
123: velocity.x = 0;
124: velocity.z = 0;
125: velocity.y = 0;
126: }
127: motion = false;
128: }
129:
130: protected void processMouseDragged(final MouseEvent evt) {
131: float offsetX = (evt.getX() - canvasCenter.x) / canvasCenter.x;
132: float offsetY = (evt.getY() - canvasCenter.y) / canvasCenter.y;
133: float factorX = 0f;
134: float factorY = 0f;
135:
136: motion = false;
137:
138: if (Math.abs(offsetX) - deadXSize > 0f) {
139: factorX = (float) Math.pow(Math.abs(offsetX) - deadXSize,
140: 2f);
141: motion = true;
142: } else {
143: factorX = 0f;
144: }
145:
146: if (Math.abs(offsetY) - deadYSize > 0f) {
147: factorY = (float) Math.pow(Math.abs(offsetY) - deadYSize,
148: 2f);
149: motion = true;
150: } else {
151: factorY = 0f;
152: }
153:
154: if (offsetX > 0f)
155: factorX = -factorX;
156:
157: if (offsetY < 0f)
158: factorY = -factorY;
159:
160: if (SwingUtilities.isLeftMouseButton(evt)) {
161: //System.out.println( factorX +" "+factorY );
162: yawAngle = MAX_ANGLE * factorX;
163: velocity.z = MAX_VELOCITY * factorY;
164: } else if (SwingUtilities.isRightMouseButton(evt)) {
165: velocity.x = MAX_VELOCITY * factorX;
166: velocity.y = MAX_VELOCITY * factorY;
167: } else if (SwingUtilities.isMiddleMouseButton(evt)) {
168: pitchAngle = MAX_ANGLE * factorY;
169: rollAngle = MAX_ANGLE * factorX;
170: } else {
171: yawAngle = 0f;
172: pitchAngle = 0f;
173: rollAngle = 0f;
174: velocity.x = 0;
175: velocity.z = 0;
176: velocity.y = 0;
177: motion = false;
178: }
179: }
180:
181: protected void integrateTransforms() {
182: if (!collisionEnabled()) {
183: yawTransform.rotY(yawAngle);
184: pitchTransform.rotX(pitchAngle);
185: rollTransform.rotZ(rollAngle);
186:
187: velocityTransform.set(velocity);
188: velocityTransform.mul(yawTransform);
189: velocityTransform.mul(pitchTransform);
190: velocityTransform.mul(rollTransform);
191:
192: targetTG.getTransform(targetTransform);
193: targetTransform.mul(yawTransform);
194: targetTransform.mul(pitchTransform);
195: targetTransform.mul(rollTransform);
196: targetTransform.mul(velocityTransform);
197: targetTG.setTransform(targetTransform);
198: } else {
199: targetTG.getTransform(targetTransform);
200: SweptVolumeCollision collision = collisionControl
201: .getCollisions(targetTransform, newTargetTransform,
202: velocity, rollAngle, pitchAngle, yawAngle);
203: targetTG.setTransform(newTargetTransform);
204: }
205:
206: }
207:
208: }
|