I am trying to map a trajectory path between two point. All I know is the two points in question and the distance between them. What I would like to be able to calculate is the velocity and angle necessary to hit the end point.
I would also like to be able to factor in some gravity and wind so that the path/trajectory is a little less ‘perfect.’ Its for a computer game.
Thanks F.
This entire physical situation can be described using the SUVAT equations of motion, since the acceleration at all times is constant.
The following explanation presumes understanding of basic algebra and vector maths. If you’re not familiar with it, I strongly recommend you go read up on it before attempting to write the sort of game you have proposed. It also assumes you’re dealing with 2D, though if you’re dealing with 3D most of the same applies, since it’s all in vector form – you just end up solving a cubic instead of quadratic, for which it may be best to use a numerical solver.
Physics
(Note: vectors represented in bold.)
Basically, you’ll want to start by formulating your equation for displacement (in vector form):
r is the displacement relative to the start position, u is the initial velocity, a is the acceleration (constant at all times). t is of course time.
a is dependent on the forces present in your system. In the general case of gravity and wind:
where
iis the unit vector in the x direction andjthe unit vector in the y direction.gis the acceleration due to gravity (9.81 ms^-2 on Earth).F_wis the force vector due to the wind (this term disappears for no wind) – we’re assuming this is constant for the sake of simplicity.mis the mass of the projectile.Then you can simply substitute the equation for
ainto the equation forr, and you’re left with an equation of three variables (r,u,t). Next, expand your single vector equation forrinto two scalar equations (for x and y displacement), and use substitution to eliminatet(maths might get a bit tricky here). You should be left with a single quadratic equation with onlyranduas free variables.Now, you want to solve the equation for
r = [target position] - [start position]. If you pick a certain magnitude for the initial velocityu(i.e. speed), then you can write the x and y components of u asU cos(a)andU sin(a)respectively, whereUis the initial speed, and a the initial angle. This can be rearranged and with a bit of trignometry, you can finally solve for the angle a, giving you the launch velocity!Algorithm
Most of the above description should be worked out on paper first. Then, it’s simply a matter of writing a function to solve the quadratic formula and apply some inverse trigonometric functions to get the result.
P.S. Sorry for all the maths/physics in this post, but it was unavoidable! The OP seemed to be asking more about the physical rather than computational side of this, anyway, so that’s what I’ve provided. Hopefully this is still useful to both the OP and other people.