Good afternoon, I am asking here about a problem with calculating the movement of a projectile from point a to point b. I do projectile flight calculations for 3D and display each point in the console.
In my case, x and y are the horizontal plane and z is the vertical plane. The movement of the projectile should not adhere to wind, weather conditions, etc. It’s just a projectile flying along a curve like a parabola.
The code that has turned out at the moment but does not achieve the desired results.
import math
x0, y0, z0 = 0, 0, 0 # Start coordinates
x1, y1, z1 = 5, 5, 9 # End coordinates
v0 = 10.0 # Initial velocity
g = 9.81 # Gravity
# We calculate the angle and vz0 to achieve the goal
dx = x1 - x0
dy = y1 - y0
dz = z1 - z0
horizontal_distance = math.sqrt(dx ** 2 + dy ** 2)
# Calculating the angle in radians
angle_rad = math.atan2(dy, dx)
# We calculate the initial vertical speed vz0
vz0 = (2 * dz * g) / horizontal_distance
# We calculate the initial horizontal velocities
vx0 = v0 * math.cos(angle_rad)
vy0 = v0 * math.sin(angle_rad)
dt = 0.1
t = 0.0
x, y, z = x0, y0, z0
# Total flight time
total_flight_time = math.sqrt((2 * dz) / g)
while t < total_flight_time:
x = x0 + vx0 * t
y = y0 + vy0 * t
z = z0 + vz0 * t - 0.5 * g * t ** 2
print(f"Time: {t:.1f} sec, X: {x:.2f}, Y: {y:.2f}, Z: {z:.2f}")
t += dt