Easiest way to calculate gun rotation for given destination and gravity?

Foreword: I am willing to implement this in Blueprint or C++, whichever is easier. Basically, I need to adjust the height of my tank turret so that it launches a shell that will reach the given destination. Both gravity and projectile speed are constant. Unfortunately, I have little-to-no experience with trigonometry or calculus yet, so I do not know how to calculate this value. I would greatly appreciate it if someone helped me devise the code necessary to find this rotation.

How are you indicating the point where the projectile should end up?
Maybe you can start from the turret? Because thereâ€™s a build-in function that can predict projectile path, where you put in direction and velocity.

I know, but that returns an array of locations. I am attempting to create an aiming system like War Thunder, where a crosshair shows the destination and penetration value of the shell if you fire at any given time. My problem is really how to calculate the true end point with gravity and velocity taken into consideration.

Bump; Iâ€™m still not sure how to accomplish this.

You can get the formula for the trajectory path and reverse it, if youâ€™re good at math.
However, for almost every point there will be two possible trajectories.

Iâ€™ve just looked up war thunder on youtube. Thereâ€™s nothing like that. In third person, you have two crosshairs, one for the camera, one for the turret, and in first person you canâ€™t see the turret, so it doesnâ€™t matter what its angle is.

Iâ€™d suggest you started from the turret. Calculate the trajectory and place the crosshair at the end of it. Move turret with the mouse, not the crosshair.

1 Like

Letâ€™s say the equation for your trajectory is:

Where we have the following variables:

Letâ€™s also say that:

So we can â€śsimplifyâ€ť:

Now we can transpose this equation:

This looks like a quadratic equation where we have:

So, to calculate the gun rotation we have to do this:

This could be used in a MathExpression with this:

(arctan((((((2 * v0) * v0) / (g * x)) / 2) + (sqrt((((pow(((((2 * v0) * v0) / (g * x)) / 2), 2)) + ((y0 * ((2 * v0) * v0)) / ((g * x) * x))) - 1))))))

and

(arctan((((((2 * v0) * v0) / (g * x)) / 2) - (sqrt((((pow(((((2 * v0) * v0) / (g * x)) / 2), 2)) + ((y0 * ((2 * v0) * v0)) / ((g * x) * x))) - 1))))))

which would look like this:

Here is a site to test with: SchrĂ¤ger Wurf nach oben (ohne Luftwiderstandes) â€“ GeoGebra
y0= h, alpha = phi, x=Wurfweite
Remember that these equations use m/s and you will have to use RadiansToDegrees (R2D).
Good luck.

2 Likes