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))))))


(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.