# Сalculation of projectile movement in 3D space

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

# We calculate the initial vertical speed vz0
vz0 = (2 * dz * g) / horizontal_distance

# We calculate the initial horizontal velocities

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

When asking for help, you are more likely to get good answers if you explain what result you want and what you are seeing instead, rather than just saying “it doesn’t work”.

Anyway, your math is wrong. The flight time and initial vertical velocity are:

``````total_flight_time = horizontal_distance / v0
vz0 = (dz + g / 2 * total_flight_time ** 2) / total_flight_time
``````

Result:

``````Time: 0.1 sec, X: 0.71, Y: 0.71, Z: 1.57
Time: 0.2 sec, X: 1.41, Y: 1.41, Z: 3.04
Time: 0.3 sec, X: 2.12, Y: 2.12, Z: 4.42
Time: 0.4 sec, X: 2.83, Y: 2.83, Z: 5.69
Time: 0.5 sec, X: 3.54, Y: 3.54, Z: 6.87
Time: 0.6 sec, X: 4.24, Y: 4.24, Z: 7.95
Time: 0.7 sec, X: 4.95, Y: 4.95, Z: 8.93
Time: 0.8 sec, X: 5.66, Y: 5.66, Z: 9.82
``````

I want to make the movements of an artillery shell or just a cannon without taking into account physics, wind, etc. Like a parabola. With the ability to adjust initial velocity and inclination. The projectile must fly from point a to point b.