From 8ef2a508c92962eed36f1f11e1d32d59db145473 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 22 Jun 2005 20:21:45 +0000 Subject: [PATCH] Created a specialization of the ballistic path that orients the sprite in the direction of motion as it traverses the path. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3614 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../jme/sprite/OrientingBallisticPath.java | 86 +++++++++++++++++++ .../com/threerings/jme/sprite/PathTest.java | 12 +-- 2 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 src/java/com/threerings/jme/sprite/OrientingBallisticPath.java diff --git a/src/java/com/threerings/jme/sprite/OrientingBallisticPath.java b/src/java/com/threerings/jme/sprite/OrientingBallisticPath.java new file mode 100644 index 000000000..a5eeb1cde --- /dev/null +++ b/src/java/com/threerings/jme/sprite/OrientingBallisticPath.java @@ -0,0 +1,86 @@ +// +// $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.Matrix3f; +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 orient 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); + + // rotate the sprite to start + rotate(orient, velocity); + } + + // documentation inherited + public void update (float time) + { + // keep track of the old velocity before we update + _ovelocity.set(_velocity); + + // do the normal update + super.update(time); + + // rotate the sprite accordingly + rotate(_ovelocity, _velocity); + } + + protected void rotate (Vector3f oorient, Vector3f norient) + { + // compute the cross product to get the normal to the plane + // defined by the velocity vectors + oorient.cross(norient, _normal); + + // compute the angle between the two vectors + float angle = FastMath.acos( + oorient.dot(norient) / (norient.length() * oorient.length())); + + // now use that to compute a rotation matrix from the old to the + // new vector + _rotate.fromAngleAxis(angle, _normal); + + // finally rotate the sprite accordingly + _sprite.getLocalRotation().multLocal(_rotate); + } + + protected Vector3f _ovelocity = new Vector3f(); + protected Vector3f _normal = new Vector3f(); + protected Quaternion _rotate = new Quaternion(); +} diff --git a/tests/src/java/com/threerings/jme/sprite/PathTest.java b/tests/src/java/com/threerings/jme/sprite/PathTest.java index 40dcacf1a..62ca3296c 100644 --- a/tests/src/java/com/threerings/jme/sprite/PathTest.java +++ b/tests/src/java/com/threerings/jme/sprite/PathTest.java @@ -24,8 +24,10 @@ public class PathTest extends JmeApp { super.initRoot(); + float range = 56, muzvel = 24; + // set up the camera - Vector3f loc = new Vector3f(250, -600, 0); + Vector3f loc = new Vector3f(range/2, -range*2, 0); _camera.setLocation(loc); Matrix3f rotm = new Matrix3f(); rotm.fromAngleAxis(-FastMath.PI/2, _camera.getLeft()); @@ -34,8 +36,6 @@ public class PathTest extends JmeApp rotm.multLocal(_camera.getLeft()); _camera.update(); - float range = 560, muzvel = 82; - Box box = new Box("box", new Vector3f(-1, -1, -1), new Vector3f(1, 1, 1)); _geom.attachChild(box); @@ -45,9 +45,8 @@ public class PathTest extends JmeApp box2.setLocalTranslation(new Vector3f(range, 0, 0)); _geom.attachChild(box2); - Sphere ball = new Sphere("ball", 10, 10, 1); Sprite shot = new Sprite(); - shot.attachChild(ball); + shot.attachChild(box); _geom.attachChild(shot); Vector3f start = new Vector3f(0, 0, 0); @@ -65,7 +64,8 @@ public class PathTest extends JmeApp vel.multLocal(muzvel); float time = BallisticPath.computeFlightTime(range, muzvel, angle); - shot.move(new BallisticPath(shot, start, vel, GRAVITY, time)); + shot.move(new OrientingBallisticPath( + shot, new Vector3f(1, 0, 0), start, vel, GRAVITY, time)); System.out.println("Range: " + range); System.out.println("Muzzle velocity: " + muzvel);