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
This commit is contained in:
@@ -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 <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);
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
@@ -24,8 +24,10 @@ public class PathTest extends JmeApp
|
|||||||
{
|
{
|
||||||
super.initRoot();
|
super.initRoot();
|
||||||
|
|
||||||
|
float range = 56, muzvel = 24;
|
||||||
|
|
||||||
// set up the camera
|
// set up the camera
|
||||||
Vector3f loc = new Vector3f(250, -600, 0);
|
Vector3f loc = new Vector3f(range/2, -range*2, 0);
|
||||||
_camera.setLocation(loc);
|
_camera.setLocation(loc);
|
||||||
Matrix3f rotm = new Matrix3f();
|
Matrix3f rotm = new Matrix3f();
|
||||||
rotm.fromAngleAxis(-FastMath.PI/2, _camera.getLeft());
|
rotm.fromAngleAxis(-FastMath.PI/2, _camera.getLeft());
|
||||||
@@ -34,8 +36,6 @@ public class PathTest extends JmeApp
|
|||||||
rotm.multLocal(_camera.getLeft());
|
rotm.multLocal(_camera.getLeft());
|
||||||
_camera.update();
|
_camera.update();
|
||||||
|
|
||||||
float range = 560, muzvel = 82;
|
|
||||||
|
|
||||||
Box box = new Box("box", new Vector3f(-1, -1, -1),
|
Box box = new Box("box", new Vector3f(-1, -1, -1),
|
||||||
new Vector3f(1, 1, 1));
|
new Vector3f(1, 1, 1));
|
||||||
_geom.attachChild(box);
|
_geom.attachChild(box);
|
||||||
@@ -45,9 +45,8 @@ public class PathTest extends JmeApp
|
|||||||
box2.setLocalTranslation(new Vector3f(range, 0, 0));
|
box2.setLocalTranslation(new Vector3f(range, 0, 0));
|
||||||
_geom.attachChild(box2);
|
_geom.attachChild(box2);
|
||||||
|
|
||||||
Sphere ball = new Sphere("ball", 10, 10, 1);
|
|
||||||
Sprite shot = new Sprite();
|
Sprite shot = new Sprite();
|
||||||
shot.attachChild(ball);
|
shot.attachChild(box);
|
||||||
_geom.attachChild(shot);
|
_geom.attachChild(shot);
|
||||||
|
|
||||||
Vector3f start = new Vector3f(0, 0, 0);
|
Vector3f start = new Vector3f(0, 0, 0);
|
||||||
@@ -65,7 +64,8 @@ public class PathTest extends JmeApp
|
|||||||
vel.multLocal(muzvel);
|
vel.multLocal(muzvel);
|
||||||
|
|
||||||
float time = BallisticPath.computeFlightTime(range, muzvel, angle);
|
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("Range: " + range);
|
||||||
System.out.println("Muzzle velocity: " + muzvel);
|
System.out.println("Muzzle velocity: " + muzvel);
|
||||||
|
|||||||
Reference in New Issue
Block a user