anim = FuncAnimation(fig, animate, frames=np.linspace(0, t_max, 300), interval=20, blit=True)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Parameters
R = 5 # Radius of the disk
v0 = 1 # Initial radial velocity of the puck
omega = 0.2 # Angular velocity of the rotating disk
num_points = 10
# 1. Double the maximum time, so that we could see puck touch the border after going through the center.
# Original was R / v0 (time to reach center). New is 2 * R / v0 (time to reach other side).
t_max = 2 * R / v0
# Initialize figure and axis
fig, ax = plt.subplots()
ax.set_xlim(-R-1, R+1)
ax.set_ylim(-R-1, R+1)
# Draw the disk
disk = plt.Circle((0, 0), R, color='lightgray', fill=True)
ax.add_artist(disk)
# Puck as a point
puck, = ax.plot([], [], 'bo', zorder=10) # zorder makes puck appear on top
# Ground reference points on the circumference of the disk
theta_ground = np.linspace(0, 2*np.pi, num_points, endpoint=False) # Use endpoint=False for better spacing
x_ground = R * np.cos(theta_ground)
y_ground = R * np.sin(theta_ground)
ground_points, = ax.plot(x_ground, y_ground, 'ro')
# 3. Add next to each ground point text id
# Initialize text objects for each ground point
ground_point_texts = []
for i in range(num_points):
# Place text slightly outside the point for better visibility
text = ax.text(x_ground[i] * 1.1, y_ground[i] * 1.1, str(i),
ha='center', va='center', color='black')
ground_point_texts.append(text)
# 4. Draw straight line from puck's starting point to the other side of disk's border.
# Initialize the line object. We'll make it green and a bit thicker.
line, = ax.plot([], [], 'g-', lw=2)
# Define the line's endpoints in the disk's own coordinate system (it's a diameter)
line_start_points = np.array([
[R, -R], # x-coordinates of the line's ends
[0, 0] # y-coordinates of the line's ends
])
# Animation function
def animate(t):
# Puck's radial position
# It starts at R, goes to 0 at t=t_max/2, and to -R at t=t_max
r_puck = -v0 * t + R
# Angle in the rotating frame (disk rotates with omega)
phi_disk = -omega * t
# Transform puck's coordinates to the lab frame (non-rotating perspective)
x_puck = r_puck * np.cos(phi_disk)
y_puck = r_puck * np.sin(phi_disk)
# Update puck position
puck.set_data(x_puck, y_puck)
# 2. Reverse direction of rotation of ground points (clockwise)
# We achieve this by using a negative angle (-omega * t) for rotation.
# cos(-a) = cos(a)
# sin(-a) = -sin(a)
rotation_angle = -omega * t
rotation_matrix = np.array([
[np.cos(rotation_angle), -np.sin(rotation_angle)],
[np.sin(rotation_angle), np.cos(rotation_angle)]
])
# Rotate ground reference points
rotated_points = rotation_matrix @ np.vstack((x_ground, y_ground))
x_rotated, y_rotated = rotated_points
# Update ground points' positions
ground_points.set_data(x_rotated, y_rotated)
# 3. Update text id positions
for i, txt in enumerate(ground_point_texts):
txt.set_position((x_rotated[i] * 1.1, y_rotated[i] * 1.1))
# 4. Make the line move at the same rate as ground points
# We apply the same rotation to the line's endpoints
rotated_line = rotation_matrix @ line_start_points
line.set_data(rotated_line[0, :], rotated_line[1, :])
# Return all artists that have been updated
return [puck, ground_points, line] + ground_point_texts
# Create animation
# The list of artists returned by animate() is passed to blit=True
anim = FuncAnimation(fig, animate, frames=np.linspace(0, t_max, 300), interval=30, blit=True)
plt.gca().set_aspect('equal', adjustable='box')
plt.title("Puck's Trajectory from Disk's Inertial Frame")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Show the animation
plt.show()