Question
Predict trajectory of a bouncing ball
Key Points:
- I have a default ball trajectory generated using some code(provided below). Lets name this trajectory Initial Trajectory.
- Next I have an actual ball whose trajectory I need to estimate.
- Now I need to improve the Initial Trajectory iteratively (to better represent actual ball trajectory) based on the coordinates of the actual ball as it moves through the environment.
- For example lets say after 5 units of time, I have 5 positions of where the actual ball has been through. Now I need to uses these 5 points and the Initial Trajectory to predict the future trajectory of the actual ball. I am expecting the actual trajectory to improve with time as more positions of the actual ball comes in.
Initial Trajectory Visualization:
Initial Trajectory Code:
import numpy as np
import matplotlib.pyplot as plt
# Constants
g = 9.81 # gravity (m/s^2)
e = 0.8 # coefficient of restitution
theta = np.radians(45) # launch angle (radians)
v0 = 10 # initial velocity (m/s)
x0, y0 = 0, 2 # initial position (m)
vx0 = v0 * np.cos(theta) # initial horizontal velocity (m/s)
vy0 = v0 * np.sin(theta) # initial vertical velocity (m/s)
t_step = 0.01 # time step for simulation (s)
t_max = 5 # max time for simulation (s)
# Initialize lists to store trajectory points
x_vals = [x0]
y_vals = [y0]
# Simulation loop
t = 0
x, y = x0, y0
vx, vy = vx0, vy0
while t < t_max:
t += t_step
x += vx * t_step
y += vy * t_step - 0.5 * g * t_step ** 2
vy -= g * t_step
if y <= 0: # Ball hits the ground
y = 0
vy = -e * vy
x_vals.append(x)
y_vals.append(y)
if len(x_vals) > 1 and x_vals[-1] == x_vals[-2] and y_vals[-1] == y_vals[-2]:
break # Stop simulation if ball stops bouncing
# Plotting the trajectory
plt.figure(figsize=(10, 5))
plt.plot(x_vals, y_vals, label="Trajectory of the Ball")
plt.xlabel("Horizontal Distance (m)")
plt.ylabel("Vertical Distance (m)")
plt.title("Trajectory of a Bouncing Ball")
plt.legend()
plt.grid(True)
plt.show()
QUESTION:
- How can I combine a default bouncing ball trajectory and some coordinates of actual ball trajectory to predict the future trajectory of the actual ball.
NOTE:
- I am going with this method of trajectory approximation to better represent real world ball trajectory without having to fine tune complex parameters like energy loss on ball bounce, friction, type of floor, ball elasticity etc.
EDIT:
Actual Ball Trajectory Data:
X: [7.410000e-03 9.591000e-02 2.844100e-01 5.729100e-01 9.614100e-01
1.449910e+00 2.038410e+00 2.726910e+00 3.373700e+00 4.040770e+00
4.800040e+00 5.577610e+00 6.355180e+00 7.132750e+00 7.910320e+00
8.687900e+00 9.465470e+00 1.020976e+01 1.092333e+01 1.163690e+01
1.235047e+01 1.306404e+01 1.377762e+01 1.449119e+01]
Y: [2.991964 2.903274 2.716584 2.431894 2.049204 1.568514 0.989824 0.313134
0.311512 0.646741 0.88397 1.0232 1.064429 1.007658 0.852887 0.600116
0.249345 0.232557 0.516523 0.702488 0.790454 0.78042 0.672385 0.466351]
Graph(Actual Ball Trajectory):
- Note the ball being dropped from a certain height with some horizontal velocity.
- The graph is in 3D but I have simplified the data points to be 2D only (one of the axis is always zero) to keep the problem simpler.