Thursday, 29 December 2011

Shooting up and down

In my last post I suggested that avoiding angles and using algebra made it easier to solve the more general ballistics problem of aiming at a target that's higher or lower. It does, and here's how it does it.

The launch velocity is a vector with components vx and vy, but this time the target is at (r, h), i.e. has range r and height h (which can be negative). The equations of motion become

These can be combined with the condition that the velocity vector has magnitude s to give a more complicated quadratic in t2 which has the following solution.

As before the expression inside the square root must be positive, otherwise the target is not reachable, something that can be checked before further calculations are done. The times and velocities can be calculated from this without angles or trigonometry, though again the angle is easily calculated if it is needed.

The code implementing this is below, forked from the previous version. It is different in a few other ways. It uses a timer to stop the missiles, based on the already calculated times, as it's difficult to determine when a missile hits the ground if it approaches it from above and below (in game either method or a proximity check with the target could be used). It demarcates the range/coverage of the gun, and both the rendering and the motion calculations are more precise (the latter uses the equations of motion).

No comments:

Post a comment