Behold, Nenya, Ring of Water and repository for our media and animation related

goodies, both Java 2D and LWJGL/JME 3D.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@1 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2006-06-23 18:07:28 +00:00
commit c2117ee86d
570 changed files with 61913 additions and 0 deletions
@@ -0,0 +1,92 @@
//
// $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.sprite;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
/**
* Moves a sprite ballistically.
*/
public class BallisticPath extends Path
{
/** Gravity: it's the law. */
public static final float G = -9.8f;
/**
* Moves the supplied sprite from the starting coordinate (which will
* be modified) using the starting velocity, under the specified
* acceleration.
*/
public BallisticPath (Sprite sprite, Vector3f start, Vector3f velocity,
Vector3f accel, float duration)
{
super(sprite);
_curpos = start;
_velocity = velocity;
_accel = accel;
_duration = duration;
}
// documentation inherited
public void update (float time)
{
// adjust the position
_velocity.mult(time, _temp);
_curpos.addLocal(_temp);
_sprite.setLocalTranslation(_curpos);
// check to see if we're done
_accum += time;
if (_accum >= _duration) {
_sprite.pathCompleted();
} else {
// adjust our velocity due to acceleration
_accel.mult(time, _temp);
_velocity.addLocal(_temp);
}
}
/**
* Computes and returns the angle of elevation needed to launch a
* ballistic projectile at the specified velocity and have it travel
* the specified range (when it will once again reach the launch
* elevation).
*/
public static float computeElevation (float range, float vel, float accel)
{
return FastMath.asin(accel * range / (vel * vel)) / 2;
}
/**
* Computes and returns the flight time of a projectile launched at an
* angle previously computed with {@link #computeElevation}..
*/
public static float computeFlightTime (float range, float vel, float angle)
{
return range / (vel * FastMath.cos(angle));
}
protected Vector3f _curpos, _velocity, _accel;
protected float _duration, _accum;
protected Vector3f _temp = new Vector3f(0, 0, 0);
}
@@ -0,0 +1,60 @@
//
// $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.sprite;
import com.jme.math.Vector3f;
/**
* Moves a sprite along a straight line.
*/
public class LinePath extends Path
{
/**
* Moves the supplied sprite from the starting coordinate to the
* specified destination coordinate in the specified time.
*/
public LinePath (Sprite sprite, Vector3f start, Vector3f finish,
float duration)
{
super(sprite);
_start = start;
_finish = finish;
_duration = duration;
}
// documentation inherited
public void update (float time)
{
_accum += time;
if (_accum >= _duration) {
_sprite.setLocalTranslation(_finish);
_sprite.pathCompleted();
} else {
_temp.interpolate(_start, _finish, _accum / _duration);
_sprite.setLocalTranslation(_temp);
}
}
protected Vector3f _start, _finish;
protected float _duration, _accum;
protected Vector3f _temp = new Vector3f(0, 0, 0);
}
@@ -0,0 +1,103 @@
//
// $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.sprite;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
/**
* Moves a sprite along a series of straight lines.
*/
public class LineSegmentPath extends Path
{
/**
* Creates a path for the supplied sprite traversing the supplied
* series of points with the specified duration between points.
*
* @param points a list of points between which the sprite will be
* moved linearly (the sprite will be moved immediately to the first
* point).
* @param durations defines the elapsed time between each successive
* traversal. This will as a result be shorter by one element than the
* points array.
*/
public LineSegmentPath (Sprite sprite, Vector3f up, Vector3f orient,
Vector3f[] points, float[] durations)
{
super(sprite);
_up = up;
_orient = orient;
_points = points;
_durations = durations;
updateRotation();
}
// documentation inherited
public void update (float time)
{
// note the accumulated time
_accum += time;
// if we have surpassed the time for this segment, subtract the
// segment time and move on to the next segment
while (_current < _durations.length && _accum > _durations[_current]) {
_accum -= _durations[_current];
advance();
}
// if we have completed our path, move the sprite to the final
// position and wrap everything up
if (_current >= _durations.length) {
_sprite.setLocalTranslation(_points[_points.length-1]);
_sprite.pathCompleted();
return;
}
// move the sprite to the appropriate position between points
_temp.interpolate(_points[_current], _points[_current+1],
_accum / _durations[_current]);
_sprite.setLocalTranslation(_temp);
}
protected void advance ()
{
_current++;
if (_current < _points.length-1) {
updateRotation();
}
}
protected void updateRotation ()
{
_points[_current+1].subtract(_points[_current], _temp);
PathUtil.computeRotation(_up, _orient, _temp, _rotate);
_sprite.getLocalRotation().set(_rotate);
}
protected Vector3f _up, _orient;
protected Vector3f[] _points;
protected float[] _durations;
protected float _accum;
protected int _current;
protected Vector3f _temp = new Vector3f(0, 0, 0);
protected Quaternion _rotate = new Quaternion();
}
@@ -0,0 +1,70 @@
//
// $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.sprite;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
/**
* A ballistic path that orients the sprite toward the velocity vector as
* it traverses the path.
*/
public class OrientingBallisticPath extends BallisticPath
{
/**
* Creates a {@link BallisticPath} that will rotate the sprite so that
* the supplied <code>orient</code> is aligned with the velocity
* vector at all points along the path. If the provided orientation is
* not initially in line with the starting velocity, the sprite will
* be rotated immediately and then adjusted as it follows the path.
*/
public OrientingBallisticPath (
Sprite sprite, Vector3f orient, Vector3f start, Vector3f velocity,
Vector3f accel, float duration)
{
super(sprite, start, velocity, accel, duration);
// TODO: handle orient that is not (1, 0, 0)
_orient = orient;
// compute the up vector (opposite of acceleration)
_up = accel.negate();
_up.normalizeLocal();
_sprite.setLocalRotation(
PathUtil.computeAxisRotation(_up, velocity, _rotate));
}
// documentation inherited
public void update (float time)
{
// do the normal update
super.update(time);
_sprite.setLocalRotation(
PathUtil.computeAxisRotation(_up, _velocity, _rotate));
}
protected Vector3f _orient;
protected Vector3f _up;
protected Quaternion _rotate = new Quaternion();
}
@@ -0,0 +1,51 @@
//
// $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.sprite;
import com.jme.scene.Controller;
/**
* Defines a framework for moving sprites around and notifying interested
* parties when the sprite has completed its path or if the path has been
* cancelled.
*/
public abstract class Path extends Controller
{
/**
* Creates and initializes this path with the sprite it will be
* manipulating.
*/
protected Path (Sprite sprite)
{
_sprite = sprite;
}
/**
* Called when this path is removed from its sprite (either due to
* completion or cancellation).
*/
public void wasRemoved ()
{
}
protected Sprite _sprite;
}
@@ -0,0 +1,44 @@
//
// $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.sprite;
/**
* Implement this interface to find out when a sprite completes or cancels
* its path.
*/
public interface PathObserver extends SpriteObserver
{
/**
* Called when a sprite's path is cancelled either because a new path
* was started or the path was explicitly cancelled with {@link
* Sprite#cancelMove}.
*/
public void pathCancelled (Sprite sprite, Path path);
/**
* Called when a sprite completes its traversal of a path.
*
* @param sprite the sprite that completed its path.
* @param path the path that was completed.
*/
public void pathCompleted (Sprite sprite, Path path);
}
@@ -0,0 +1,95 @@
//
// $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.sprite;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
/**
* Path related utility functions.
*/
public class PathUtil
{
/**
* Computes a rotation to align the X axis with the specified
* orientation and stores it in the provided target. The provided up
* vector is crossed with the new orientation to find the left vector
* and the left vector is recrossed with the orientation to find the
* correct up vector.
*
* @return the supplied target quaternion.
*/
public static Quaternion computeAxisRotation (
Vector3f up, Vector3f orient, Quaternion target)
{
_axes[0].set(orient);
_axes[0].normalizeLocal();
up.cross(_axes[0], _axes[1]);
_axes[1].normalizeLocal();
_axes[0].cross(_axes[1], _axes[2]);
target.fromAxes(_axes);
return target;
}
/**
* Computes a rotation from the one vector to another.
*
* @param axis will be used as the axis of rotation if the two vectors
* are parallel.
*/
public static Quaternion computeRotation (
Vector3f axis, Vector3f from, Vector3f to, Quaternion target)
{
float angle = computeAngle(from, to);
if (angle == FastMath.PI) { // opposite
target.fromAngleAxis(angle, axis);
} else if (angle == 0) { // coincident
target.x = target.y = target.z = 0;
target.w = 1;
} else {
from.cross(to, _axis);
target.fromAngleAxis(angle, _axis);
}
return target;
}
/**
* Computes the angle between two arbitrary vectors.
*/
public static float computeAngle (Vector3f one, Vector3f two)
{
return FastMath.acos(one.dot(two) / (one.length() * two.length()));
}
/**
* Computes the angle between two normalized vectors.
*/
public static float computeAngleNormal (Vector3f one, Vector3f two)
{
return FastMath.acos(one.dot(two));
}
protected static Vector3f[] _axes = {
new Vector3f(), new Vector3f(), new Vector3f() };
protected static Vector3f _axis = new Vector3f();
}
@@ -0,0 +1,244 @@
//
// $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.sprite;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.samskivert.util.ObserverList;
import com.threerings.jme.Log;
/**
* Represents a visual entity that one controls as a single unit. Sprites
* can be made to follow paths which is one of their primary reasons for
* existence.
*/
public class Sprite extends Node
{
/**
* Walks down the hierarchy provided setting the animation speed on
* any controllers found along the way.
*/
public static void setAnimationSpeed (Spatial spatial, float speed)
{
for (int ii = 0; ii < spatial.getControllers().size(); ii++) {
spatial.getController(ii).setSpeed(speed);
}
if (spatial instanceof Node) {
Node node = (Node)spatial;
for (int ii = 0; ii < node.getQuantity(); ii++) {
setAnimationSpeed(node.getChild(ii), speed);
}
}
}
/**
* Walks down the hierarchy provided turning on or off any controllers
* found along the way.
*/
public static void setAnimationActive (Spatial spatial, boolean active)
{
for (int ii = 0; ii < spatial.getControllers().size(); ii++) {
spatial.getController(ii).setActive(active);
}
if (spatial instanceof Node) {
Node node = (Node)spatial;
for (int ii = 0; ii < node.getQuantity(); ii++) {
setAnimationActive(node.getChild(ii), active);
}
}
}
/**
* Walks down the hierarchy provided setting the animation repeat type on
* any controllers found along the way.
*/
public static void setAnimationRepeatType (Spatial spatial, int repeatType)
{
for (int ii = 0; ii < spatial.getControllers().size(); ii++) {
spatial.getController(ii).setRepeatType(repeatType);
}
if (spatial instanceof Node) {
Node node = (Node)spatial;
for (int ii = 0; ii < node.getQuantity(); ii++) {
setAnimationRepeatType(node.getChild(ii), repeatType);
}
}
}
public Sprite ()
{
super("");
setName("sprite:" + hashCode());
}
/**
* Adds an observer to this sprite. Observers are notified when path
* related events take place.
*/
public void addObserver (SpriteObserver obs)
{
if (_observers == null) {
_observers = new ObserverList<SpriteObserver>(
ObserverList.SAFE_IN_ORDER_NOTIFY);
}
_observers.add(obs);
}
/**
* Removes the specified observer from this sprite.
*/
public void removeObserver (SpriteObserver obs)
{
if (_observers != null) {
_observers.remove(obs);
}
}
/**
* Returns true if this sprite is moving along a path, false if not.
*/
public boolean isMoving ()
{
return _path != null;
}
/**
* Instructs this sprite to move along the specified path. Any
* currently executing path will be cancelled.
*/
public void move (Path path)
{
// if there's a previous path, let it know that it's going away
cancelMove();
// save off this path
_path = path;
addController(_path);
}
/**
* Cancels any currently executing path. Any registered observers will
* be notified of the cancellation.
*/
public void cancelMove ()
{
if (_path != null) {
Path oldpath = _path;
_path = null;
oldpath.wasRemoved();
if (_observers != null) {
_observers.apply(new CancelledOp(this, oldpath));
}
}
}
/**
* Called by the active path when it has completed. <em>Note:</em>
* don't call this method unless you are implementing a {@link Path}.
*/
public void pathCompleted ()
{
if (_path == null) {
Log.warning("pathCompleted() called on pathless sprite " +
"(re-completed?) [sprite=" + this + "].");
Thread.dumpStack();
return;
}
Path oldpath = _path;
_path = null;
removeController(oldpath);
oldpath.wasRemoved();
if (_observers != null) {
_observers.apply(new CompletedOp(this, oldpath));
}
}
/**
* Configures the speed of all controllers in our model hierarchy.
*/
public void setAnimationSpeed (float speed)
{
setAnimationSpeed(this, speed);
}
/**
* Configures the active state of all controllers in our model hierarchy.
*/
public void setAnimationActive (boolean active)
{
setAnimationActive(this, active);
}
/**
* Configures the repeat type of all controllers in our model hierarchy.
*/
public void setAnimationRepeatType (int repeatType)
{
setAnimationRepeatType(this, repeatType);
}
/** Used to dispatch {@link PathObserver#pathCancelled}. */
protected static class CancelledOp
implements ObserverList.ObserverOp<SpriteObserver>
{
public CancelledOp (Sprite sprite, Path path) {
_sprite = sprite;
_path = path;
}
public boolean apply (SpriteObserver observer) {
if (observer instanceof PathObserver) {
((PathObserver)observer).pathCancelled(_sprite, _path);
}
return true;
}
protected Sprite _sprite;
protected Path _path;
}
/** Used to dispatch {@link PathObserver#pathCompleted}. */
protected static class CompletedOp
implements ObserverList.ObserverOp<SpriteObserver>
{
public CompletedOp (Sprite sprite, Path path) {
_sprite = sprite;
_path = path;
}
public boolean apply (SpriteObserver observer) {
if (observer instanceof PathObserver) {
((PathObserver)observer).pathCompleted(_sprite, _path);
}
return true;
}
protected Sprite _sprite;
protected Path _path;
}
protected ObserverList<SpriteObserver> _observers;
protected Path _path;
}
@@ -0,0 +1,29 @@
//
// $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.sprite;
/**
* The super-interface for all sprite observers.
*/
public interface SpriteObserver
{
}