More geometric jockeying that you can shake a stick at. The

LineSegmentPath now orients sprites along the path (and even handles an
arbitrary notion of "forward" for the sprite).


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3620 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2005-06-23 21:12:17 +00:00
parent 2bf5936317
commit f3dec66a8d
4 changed files with 152 additions and 28 deletions
@@ -21,6 +21,7 @@
package com.threerings.jme.sprite;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
/**
@@ -39,11 +40,15 @@ public class LineSegmentPath extends Path
* traversal. This will as a result be shorter by one element than the
* points array.
*/
public LineSegmentPath (Sprite sprite, Vector3f[] points, float[] durations)
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
@@ -57,6 +62,9 @@ public class LineSegmentPath extends Path
if (_accum > _durations[_current]) {
_accum -= _durations[_current];
_current++;
if (_current < _points.length-1) {
updateRotation();
}
}
// if we have completed our path, move the sprite to the final
@@ -73,9 +81,18 @@ public class LineSegmentPath extends Path
_sprite.setLocalTranslation(_temp);
}
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();
}
@@ -50,8 +50,8 @@ public class OrientingBallisticPath extends BallisticPath
_up = accel.negate();
_up.normalizeLocal();
computeRotation(velocity);
_sprite.getLocalRotation().set(_rotate);
_sprite.setLocalRotation(
PathUtil.computeAxisRotation(_up, velocity, _rotate));
}
// documentation inherited
@@ -60,24 +60,11 @@ public class OrientingBallisticPath extends BallisticPath
// do the normal update
super.update(time);
computeRotation(_velocity);
_sprite.getLocalRotation().set(_rotate);
}
protected void computeRotation (Vector3f velocity)
{
_axes[0].set(velocity);
_axes[0].normalizeLocal();
_axes[0].cross(_up, _axes[1]);
_axes[1].normalizeLocal();
_axes[1].cross(_axes[0], _axes[2]);
_axes[2].cross(_axes[0], _axes[1]);
_rotate.fromAxes(_axes);
_sprite.setLocalRotation(
PathUtil.computeAxisRotation(_up, _velocity, _rotate));
}
protected Vector3f _orient;
protected Vector3f _up;
protected Vector3f[] _axes = {
new Vector3f(), new Vector3f(), new Vector3f() };
protected Quaternion _rotate = new Quaternion();
}
@@ -0,0 +1,96 @@
//
// $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();
_axes[0].cross(up, _axes[1]);
_axes[1].normalizeLocal();
_axes[1].cross(_axes[0], _axes[2]);
_axes[2].cross(_axes[0], _axes[1]);
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();
}