Created a path for ballistic motion and an associated test. Yay for

physics.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3610 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2005-06-22 18:26:54 +00:00
parent 264d0e65d5
commit 6fbdc4e568
2 changed files with 149 additions and 0 deletions
@@ -0,0 +1,68 @@
//
// $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 ballistically.
*/
public class BallisticPath extends Path
{
/**
* 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);
}
}
protected Vector3f _curpos, _velocity, _accel;
protected float _duration, _accum;
protected Vector3f _temp = new Vector3f(0, 0, 0);
}
@@ -0,0 +1,81 @@
//
// $Id$
package com.threerings.jme.sprite;
import java.util.logging.Level;
import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.util.LoggingSystem;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.threerings.jme.JmeApp;
/**
* Used for testing paths.
*/
public class PathTest extends JmeApp
{
protected void initRoot ()
{
super.initRoot();
// set up the camera
Vector3f loc = new Vector3f(250, -600, 0);
_camera.setLocation(loc);
Matrix3f rotm = new Matrix3f();
rotm.fromAngleAxis(-FastMath.PI/2, _camera.getLeft());
rotm.multLocal(_camera.getDirection());
rotm.multLocal(_camera.getUp());
rotm.multLocal(_camera.getLeft());
_camera.update();
Box box = new Box("box", new Vector3f(-1, -1, -1),
new Vector3f(1, 1, 1));
_geom.attachChild(box);
Box box2 = new Box("box2", new Vector3f(-1, -1, -1),
new Vector3f(1, 1, 1));
box2.setLocalTranslation(new Vector3f(560, 0, 0));
_geom.attachChild(box2);
Sphere ball = new Sphere("ball", 10, 10, 1);
Sprite shot = new Sprite();
shot.attachChild(ball);
_geom.attachChild(shot);
Vector3f start = new Vector3f(0, 0, 0);
// start with the "cannon" pointed along the x axis
Vector3f vel = new Vector3f(1, 0, 0);
// rotate it up (around the y axis) the necessary elevation
Quaternion rot = new Quaternion();
rot.fromAngleAxis(-FastMath.PI*27.35f/180f, new Vector3f(0, 1, 0));
// rot.fromAngleAxis(-FastMath.PI*62.65f/180f, new Vector3f(0, 1, 0));
rot.multLocal(vel);
// give the cannon a "muzzle velocity"
vel.multLocal(82f);
System.out.println("Start: " + start);
System.out.println("Velocity: " + vel);
shot.move(new BallisticPath(shot, start, vel, GRAVITY, 7.7f));
// shot.move(new BallisticPath(shot, start, vel, GRAVITY, 15f));
}
public static void main (String[] args)
{
LoggingSystem.getLogger().setLevel(Level.OFF);
PathTest test = new PathTest();
if (!test.init()) {
System.exit(-1);
}
test.run();
}
protected static final Vector3f GRAVITY = new Vector3f(0, 0, -9.8f);
}