Hi folks.
Much like a cannon ball, I’m looking to figure out the angle in which a projectile needs to be “fired” in order to hit a target.
The target can exist at any x,y,z and may be moving, although, I can do without this parameter.
/* Trajectory.js
* This script is attached to a simple cylinder to act as a firing tube
*/
var theTarget : GameObject;
// The Target: a simple cube
var theSphere : GameObject;
// The Projectile: a simple sphere with a rigidbody
private var sphereVelocity : Vector3 = theSphere.rigidbody.velocity;
// 20
private var sphereMass : float = theSphere.rigidbody.mass;
// 1
private var sphereDrag : float = theSphere.rigidbody.drag;
// 0.00
private var worldGravity : Vector3 = Physics.gravity;
// 9.81
private var boxPosition : Vector3 = theTarget.transform.position;
private var myPosition : Vector3 = transform.position;
private var ballPosition : Vector3 = theSphere.transform.position;
private var heading : Vector3 = boxPosition - myPosition ;
Okay – here’s where I fail to recall my education in Algebra, Calc, Trig; basicaly the entire concept of Math goes out the window :?
// shortened version
private var v = sphereVelocity; // 20
private var m = sphereMass; // 1
private var d = sphereDrag; // 0.00 (air resistance??)
private var g = worldGravity; // 9.81
private var h = heading; // whatever it is
/* comment since it's broken
private var angle: Vector3 = (?????)
// the angle to launch the sphere
private var range: float = Mathf.Round( v * v
* Mathf.Sin ( ( (2*angle)
* Mathf.Deg2Rad ) ) )
/ (2 * g) ); // TODO
private var timeTillImpact : float; // TODO
private var distanceAtImpact: Vector3; // TODO
// theSphere.rigidbody.applyForce(ballPosition.forward * v);
// or
theSphere.velocity = transform.TransformDirection( Vector3 ( 0, 0, v ) );
// fire the sphere (not sure which to use yet)
*/
I would really appreciate it if anyone can help me understand what the appropriate algorithm(s) to use is.
Thanks much!!