From dfa9216b895c0629d3b86a366581f1fa180b1750 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 22 Jun 2005 18:50:38 +0000 Subject: [PATCH] Do dat math! git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3612 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/jme/sprite/BallisticPath.java | 21 +++++++++++++++ .../com/threerings/jme/sprite/PathTest.java | 26 ++++++++++++------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/java/com/threerings/jme/sprite/BallisticPath.java b/src/java/com/threerings/jme/sprite/BallisticPath.java index 8b0302782..996afdf2d 100644 --- a/src/java/com/threerings/jme/sprite/BallisticPath.java +++ b/src/java/com/threerings/jme/sprite/BallisticPath.java @@ -21,6 +21,7 @@ package com.threerings.jme.sprite; +import com.jme.math.FastMath; import com.jme.math.Vector3f; /** @@ -62,6 +63,26 @@ public class BallisticPath extends Path } } + /** + * 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); diff --git a/tests/src/java/com/threerings/jme/sprite/PathTest.java b/tests/src/java/com/threerings/jme/sprite/PathTest.java index d026fce02..40dcacf1a 100644 --- a/tests/src/java/com/threerings/jme/sprite/PathTest.java +++ b/tests/src/java/com/threerings/jme/sprite/PathTest.java @@ -34,13 +34,15 @@ 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); Box box2 = new Box("box2", new Vector3f(-1, -1, -1), new Vector3f(1, 1, 1)); - box2.setLocalTranslation(new Vector3f(560, 0, 0)); + box2.setLocalTranslation(new Vector3f(range, 0, 0)); _geom.attachChild(box2); Sphere ball = new Sphere("ball", 10, 10, 1); @@ -52,19 +54,25 @@ public class PathTest extends JmeApp // 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)); + float angle = BallisticPath.computeElevation(range, muzvel, -9.8f); + rot.fromAngleAxis(angle, new Vector3f(0, 1, 0)); rot.multLocal(vel); - // give the cannon a "muzzle velocity" - vel.multLocal(82f); - System.out.println("Start: " + start); + // give the cannon its muzzle velocity + vel.multLocal(muzvel); + + float time = BallisticPath.computeFlightTime(range, muzvel, angle); + shot.move(new BallisticPath(shot, start, vel, GRAVITY, time)); + + System.out.println("Range: " + range); + System.out.println("Muzzle velocity: " + muzvel); + System.out.println("Angle: " + angle + " (" + + (angle * 180 / FastMath.PI) + ")"); + System.out.println("Flight time: " + time); 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)