diff --git a/src/java/com/threerings/jme/JmeApp.java b/src/java/com/threerings/jme/JmeApp.java index e64d04b09..c8b006a2b 100644 --- a/src/java/com/threerings/jme/JmeApp.java +++ b/src/java/com/threerings/jme/JmeApp.java @@ -51,7 +51,8 @@ import com.jme.light.PointLight; import com.jme.math.Vector3f; import com.jme.util.Timer; -import com.threerings.jme.input.GodViewHandler; +import com.threerings.jme.camera.CameraPath; +import com.threerings.jme.camera.GodViewHandler; /** * Defines a basic application framework providing integration with the @@ -180,6 +181,27 @@ public class JmeApp } } + /** + * Starts the camera moving along a path which will be updated every tick + * until it is complete. + */ + public void moveCamera (CameraPath path) + { + if (_campath != null) { + _campath.abort(); + } + _campath = path; + } + + /** + * Returns true if the camera is currently animating along a path, false if + * it is not. + */ + public boolean cameraIsMoving () + { + return (_campath != null); + } + /** * Instructs the application to stop the main loop, cleanup and exit. */ @@ -394,6 +416,13 @@ public class JmeApp float timePerFrame = _timer.getTimePerFrame(); _root.updateGeometricState(timePerFrame, true); + // if there's a camera path, update that as well + if (_campath != null) { + if (_campath.tick(timePerFrame)) { + _campath = null; + } + } + // update our stats display if we have one if (_stats != null) { _stats.update(_timer, _display.getRenderer()); @@ -509,6 +538,7 @@ public class JmeApp protected PropertiesIO _properties; protected DisplaySystem _display; protected Camera _camera; + protected CameraPath _campath; protected InputHandler _input; protected BRootNode _rnode; diff --git a/src/java/com/threerings/jme/camera/CameraPath.java b/src/java/com/threerings/jme/camera/CameraPath.java new file mode 100644 index 000000000..f573174d0 --- /dev/null +++ b/src/java/com/threerings/jme/camera/CameraPath.java @@ -0,0 +1,54 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme.camera; + +import com.jme.renderer.Camera; + +/** + * Used to move the camera along a particular path. + */ +public abstract class CameraPath +{ + /** + * This is called on every frame to allow the path to adjust the position + * of the camera. + * + * @return true if the path is completed and can be disposed, false if it + * is not yet completed. + */ + public abstract boolean tick (float secondsSince); + + /** + * Called if this path is aborted prior to completion due to being replaced + * by a new camera path. + */ + public void abort () + { + } + + protected CameraPath (Camera camera) + { + _camera = camera; + } + + protected Camera _camera; +} diff --git a/src/java/com/threerings/jme/input/GodViewHandler.java b/src/java/com/threerings/jme/camera/GodViewHandler.java similarity index 99% rename from src/java/com/threerings/jme/input/GodViewHandler.java rename to src/java/com/threerings/jme/camera/GodViewHandler.java index 744e0c6a1..4edc78342 100644 --- a/src/java/com/threerings/jme/input/GodViewHandler.java +++ b/src/java/com/threerings/jme/camera/GodViewHandler.java @@ -1,7 +1,7 @@ // // $Id$ -package com.threerings.jme.input; +package com.threerings.jme.camera; import com.jme.math.FastMath; import com.jme.math.Matrix3f; diff --git a/src/java/com/threerings/jme/camera/SwingPath.java b/src/java/com/threerings/jme/camera/SwingPath.java new file mode 100644 index 000000000..45a9799ce --- /dev/null +++ b/src/java/com/threerings/jme/camera/SwingPath.java @@ -0,0 +1,127 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2005 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.jme.camera; + +import com.jme.math.FastMath; +import com.jme.math.Matrix3f; +import com.jme.math.Vector3f; +import com.jme.renderer.Camera; + +import com.threerings.jme.Log; + +/** + * Swings the camera around a point of interest (which should be somewhere + * along the camera's view vector). Also optionally zooms the camera in or out + * (moves it along its view vector) in the process. + * + *

+ */ +public class SwingPath extends CameraPath +{ + /** + * Creates a rotating, zooming path for the specified camera. + * + * @param spot the point of interest around which to swing the camera. + * @param axis the axis around which to rotate the camera. + * @param angle the angle through which to rotate the camera. + * @param angvel the (absolute value of the) velocity at which to rotate + * the camera (in radians per second). + * @param zoom the distance to zoom along the camera's view axis (negative + * = in, positive = out). + */ + public SwingPath (Camera camera, Vector3f spot, Vector3f axis, + float angle, float angvel, float zoom) + { + super(camera); + + if (angle == 0) { + Log.warning("Requested to swing camera through zero degrees " + + "[spot=" + spot + ", axis=" + axis + + ", angvel=" + angvel + ", zoom=" + zoom + "]."); + angle = 0.0001f; + } + if (angvel <= 0) { + Log.warning("Requested to swing camera with invalid velocity " + + "[spot=" + spot + ", axis=" + axis + ", angle=" + angle + + ", angvel=" + angvel + ", zoom=" + zoom + "]."); + angvel = FastMath.PI; + } + + _spot = spot; + _axis = axis; + _angle = angle; + _angvel = (angle > 0) ? angvel : -1 * angvel; + _zoom = zoom; + _zoomvel = _zoom * _angvel / _angle; + +// Log.info("Swinging camera [angle=" + _angle + ", angvel=" + _angvel + +// ", zoom=" + _zoom + ", zoomvel=" + _zoomvel + "]."); + } + + // documentation inherited + public boolean tick (float secondsSince) + { + float deltaAngle = (secondsSince * _angvel); + float deltaZoom = (secondsSince * _zoomvel); + _rotated += deltaAngle; + _zoomed += deltaZoom; + + // clamp our rotation at the target angle and determine whether or not + // we're done + boolean done = false; + if (_angle > 0 && _rotated > _angle || + _angle < 0 && _rotated < _angle) { + deltaAngle -= (_rotated - _angle); + deltaZoom -= (_zoomed - _zoom); + _rotated = _angle; + _zoomed = _zoom; + done = true; + } + + // get a vector from the camera's position to the point around which + // we're going to orbit + Vector3f camloc = _camera.getLocation(); + Vector3f direction = camloc.subtract(_spot); + + // create a rotation matrix + _rotm.fromAxisAngle(_axis, deltaAngle); + + // rotate the center to camera vector and the camera itself + _rotm.mult(direction, direction); + _rotm.mult(_camera.getUp(), _camera.getUp()); + _rotm.mult(_camera.getLeft(), _camera.getLeft()); + _rotm.mult(_camera.getDirection(), _camera.getDirection()); + + // and move the camera to its new location (accounting for the zoom) + float scale = 1 + (deltaZoom / direction.length()); + direction.scaleAdd(scale, _spot); + _camera.setLocation(direction); + _camera.update(); + + return done; + } + + protected Vector3f _spot, _axis; + protected float _angle, _angvel, _rotated; + protected float _zoom, _zoomvel, _zoomed; + protected Matrix3f _rotm = new Matrix3f(); +} diff --git a/src/java/com/threerings/jme/camera/rotate_zoom.dia b/src/java/com/threerings/jme/camera/rotate_zoom.dia new file mode 100644 index 000000000..603bd5cc9 Binary files /dev/null and b/src/java/com/threerings/jme/camera/rotate_zoom.dia differ diff --git a/src/java/com/threerings/jme/camera/rotate_zoom.png b/src/java/com/threerings/jme/camera/rotate_zoom.png new file mode 100644 index 000000000..82ce4f88a Binary files /dev/null and b/src/java/com/threerings/jme/camera/rotate_zoom.png differ