Find angle of launch based on velocity and direction

I am trying to code a World of Warships-esque aiming mechanic, where players aim their mouse at a location, and a turret automatically elevates such that when fired, the projectile will land on the target. I know that the formula for calculating angle based solely on direction and velocity is θ = arcsin(gravity * distance/velocity^2)/2
which I have implemented as

``````script.Function.OnInvoke = function(distance,velocity,mass)
local angle = (math.asin(((workspace.Gravity/1.3))*distance)/velocity^2)/2
print(angle)
print((((workspace.Gravity/1.3))*distance)/velocity^2)
return angle
end
``````

(The object which is fired is affected by gravity less, which is why the gravity is divided by 1.3)
However, this results in an error, as the number within the arcsin is greater than 1; the reason for which I cannot figure out.
The velocity of the projectile is equal to 16 * the lookvector of the cannon.
Any help is much appreciated; I’d rather not have the cannons face directly towards the mouse and make players aim towards the sky to fire properly.

1 Like

The first part of this post can maybe help you Modeling a projectile's motion

The post seems to be about modelling the velocity of a projectile based on distance and height, not the angle of launch.

Not sure about where the formula came from but I do know there is actually a possibility that given the constants gravity, target position, and velocity the solution is actually impossible and that the target positions is out of range.

That may be happening so you will need to adjust the velocity, gravity and such.

Also I found this blog which is pretty good and has a different formula for angle with tan for finding the angle to hit stationary target.

This also doesn’t seem to work; the discriminant (sqrt S^4 … ) is invalid, and the equation breaks as a result. Perhaps it could be because of the variable Y, which wasn’t well defined; I thought it to simply be sqrt(S^2-X^2) (S being the velocity and X the speed), but I may be wrong.
Do the equations assume the particle has a certain mass? Or do I need to implement that as well?
The projectile has a script in it that applies a force equal to its mass multiplied by gravity/1.3, in case of importance.

Nope no mass should be relevant it’s purely kinematic, here is one more resource which should help

From what I can tell, the equation requires a vertical height value (the Y variable), which I assume is either the height of the arc or the starting height. Inputting both sqrt S^2-X^2 and the difference in height between the starting and ending points didn’t work, and I don’t think I can calculate the height of the arc either; so, unless I’ve really screwed up something somewhere, I don’t think this method is going to work. Thanks for the help regardless.

Notably, when I increased the velocity (from 16 to 512), it massively overshot the target, when I used the original formula.

Arcsin has a range of `[pi/2, -pi2]`. `pi/2 > 1` which is why you’re getting values that are greater than 1.

I’m not sure which formula you derived your equation from. are you able to provide that?

I’m not entirely sure, I just searched up ‘angle based on distance and velocity’, but as far as I can tell, it’s derived from the equations x=Vt and h+Vt−(g*t^2)/2, the equations of horizontal and vertical motion (I think.)

It only took me, like, a full day to realise that the force `workspace.Gravity/1.3` was positive, acting against the force of gravity; the variable G should instead have been `workspace.Gravity - workspace.Gravity/1.3`. Plugging the updated variable into the formula on the website worked; many thanks for your help.

1 Like