001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/vpbehaviors/HoverBehavior.java,v 1.1 2005/04/20 21:05:13 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 in 3 space whilst
041: maintaining the orientation of -ve Y is down.</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: *
054: * Canvas3D size changes are tracked
055: *
056: * @author Paul Byrne
057: * @version 1.15, 01/18/02
058: */
059: public class HoverBehavior extends J3dFlyMouseBehavior {
060:
061: private Transform3D velocityTransform;
062: private Transform3D transform;
063: private Transform3D newTargetTransform = new Transform3D();
064:
065: private Vector3f velocity = new Vector3f();
066: private float yawAngle = 0f;
067:
068: /**
069: * @deprecated use HoverBehavior()
070: */
071: public HoverBehavior(View view) {
072: super ();
073: }
074:
075: public HoverBehavior() {
076: super ();
077: }
078:
079: public void initialize() {
080: transform = new Transform3D();
081: targetTransform = new Transform3D();
082: velocityTransform = new Transform3D();
083: super .initialize();
084: }
085:
086: protected void processAWTEvents(final java.awt.AWTEvent[] events) {
087: for (int i = 0; i < events.length; i++)
088: if (events[i] instanceof MouseEvent) {
089: if (((MouseEvent) events[i]).getID() == MouseEvent.MOUSE_RELEASED)
090: processMouseReleased((MouseEvent) events[i]);
091: else if (((MouseEvent) events[i]).getID() == MouseEvent.MOUSE_DRAGGED)
092: processMouseDragged((MouseEvent) events[i]);
093: else
094: processUnusedEvent(events[i]);
095: } else
096: processUnusedEvent(events[i]);
097:
098: }
099:
100: protected void processMouseReleased(final MouseEvent evt) {
101: if (SwingUtilities.isLeftMouseButton(evt)) {
102: yawAngle = 0;
103: velocity.z = 0;
104: motion = false;
105: } else if (SwingUtilities.isRightMouseButton(evt)) {
106: velocity.x = 0;
107: velocity.y = 0;
108: motion = false;
109: } else if (SwingUtilities.isMiddleMouseButton(evt)) {
110: motion = false;
111: } else {
112: yawAngle = 0f;
113: velocity.x = 0;
114: velocity.z = 0;
115: velocity.y = 0;
116: motion = false;
117: }
118: }
119:
120: protected void processMouseDragged(final MouseEvent evt) {
121: float offsetX = (evt.getX() - canvasCenter.x) / canvasCenter.x;
122: float offsetY = (evt.getY() - canvasCenter.y) / canvasCenter.y;
123: float factorX = 0f;
124: float factorY = 0f;
125:
126: motion = false;
127:
128: if (Math.abs(offsetX) - deadXSize > 0f) {
129: factorX = (float) Math.pow(Math.abs(offsetX) - deadXSize,
130: 2f);
131: motion = true;
132: } else {
133: factorX = 0f;
134: }
135:
136: if (Math.abs(offsetY) - deadYSize > 0f) {
137: factorY = (float) Math.pow(Math.abs(offsetY) - deadYSize,
138: 2f);
139: motion = true;
140: } else {
141: factorY = 0f;
142: }
143:
144: if (offsetX > 0f)
145: factorX = -factorX;
146:
147: if (offsetY < 0f)
148: factorY = -factorY;
149:
150: if (SwingUtilities.isLeftMouseButton(evt)) {
151: //System.out.println( factorX +" "+factorY );
152: yawAngle = MAX_ANGLE * factorX;
153: velocity.z = MAX_VELOCITY * factorY;
154: } else if (SwingUtilities.isRightMouseButton(evt)) {
155: velocity.x = MAX_VELOCITY * factorX;
156: velocity.y = MAX_VELOCITY * factorY;
157: } else {
158: yawAngle = 0f;
159: velocity.x = 0;
160: velocity.z = 0;
161: velocity.y = 0;
162: motion = false;
163: }
164: }
165:
166: protected void integrateTransforms() {
167: if (!collisionEnabled()) {
168: transform.rotY(yawAngle);
169:
170: velocityTransform.set(velocity);
171: velocityTransform.mul(transform);
172:
173: targetTG.getTransform(targetTransform);
174: targetTransform.mul(transform);
175: targetTransform.mul(velocityTransform);
176: targetTG.setTransform(targetTransform);
177: } else {
178: targetTG.getTransform(targetTransform);
179: SweptVolumeCollision collision = collisionControl
180: .getCollisions(targetTransform, newTargetTransform,
181: velocity, 0f, 0f, yawAngle);
182: targetTG.setTransform(newTargetTransform);
183: }
184:
185: }
186:
187: }
|