Shot Put Simulation with Pygame
The shot put or projectile motion is a classic movement in physics involving two dimensions: horizontal and vertical. This motion can be described by combining uniform motion in the horizontal direction (no acceleration) with uniformly accelerated motion in the vertical direction (due to gravity).
Bernd Fischer
16. Oct 2024 · 3 min read
The key physical quantities in projectile motion are:
-
Initial velocity \( v_0 \): The speed at which the object is thrown.
-
Launch angle \( \theta \): The angle at which the object is thrown relative to the horizontal axis.
-
Acceleration due to gravity \( g \approx 9.81 \, \text{m/s}^2 \).
-
Time \( t \): The time elapsed since the throw.
Here are the formulas for all four important parameters of projectile motion: flight time, maximum height, range, and final velocity.
1. Flight Time (\( t_{\text{total}} \))
The total flight time is the duration the ball remains in the air until it reaches the ground again.
\[ t_{\text{total}} = \frac{2 \cdot v_0 \cdot \sin(\theta)}{g} \]
- \( v_0 \) = initial velocity (m/s)
- \( \theta \) = launch angle (in degrees)
- \( g \) = gravitational acceleration (approx. 9.81 m/s²)
2. Maximum Height (\( y_{\text{max}} \))
The maximum height is the highest position the ball reaches before falling back down.
\[ y_{\text{max}} = \frac{(v_0 \cdot \sin(\theta))^2}{2 \cdot g} \]
Here, only the vertical component of the initial velocity \( v_0 \cdot \sin(\theta) \) is used because it is solely responsible for the change in height.
3. Range (\( x_{\text{max}} \))
The range is the horizontal distance the ball covers until it hits the ground.
\[ x_{\text{max}} = \frac{v_0^2 \cdot \sin(2\theta)}{g} \]
- The term \( \sin(2\theta) \) shows that the maximum throw occurs at an angle of \( \theta = 45^\circ \).
4. Final Velocity
The final velocity can be split into two components: the horizontal velocity (which remains constant) and the vertical velocity (which changes due to gravity).
Horizontal Final Velocity:
\[ v_{x} = v_0 \cdot \cos(\theta) \]
Vertical Final Velocity:
Vertical velocity changes because of gravitational acceleration. At the endpoint (impact), the vertical final velocity is: \[ v_{y} = v_0 \cdot \sin(\theta) - g \cdot t_{\text{total}} \]
Total Final Velocity (\( v_{\text{end}} \)):
The total final velocity can be computed with the Pythagorean theorem since it is the combination of horizontal and vertical velocities:
\[ v_{\text{end}} = \sqrt{v_{x}^2 + v_{y}^2} \]
This gives the velocity of the ball immediately before it hits the ground.
Pygame Simulation
import pygame
import math
# Initialisiere Pygame
pygame.init()
# Fenstergröße
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Kugelwurf Simulation')
# Farben
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Physikalische Konstanten
g = 9.81 # Erdbeschleunigung in m/s²
# Kugelparameter
x0, y0 = 50, height - 50 # Startposition (linke untere Ecke)
v0 = 100 # Startgeschwindigkeit in Pixel/s
angle = 45 # Wurfwinkel in Grad
v0x = v0 * math.cos(math.radians(angle)) # Horizontale Geschwindigkeit
v0y = v0 * math.sin(math.radians(angle)) # Vertikale Geschwindigkeit
t = 0 # Zeit in Sekunden
dt = 1 / 25 # Zeitschritt in Sekunden (25 Frames pro Sekunde)
# Clock-Objekt, um die FPS zu kontrollieren
clock = pygame.time.Clock()
# Simulations-Loop
running = True
while running:
screen.fill(WHITE)
# Beende das Programm, wenn das Fenster geschlossen wird
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Kugelposition basierend auf den Gleichungen des Kugelwurfs
x = x0 + v0x * t
y = y0 - (v0y * t - 0.5 * g * t ** 2)
# Zeichne die Kugel
pygame.draw.circle(screen, RED, (int(x), int(y)), 10)
# Aktualisiere das Display
pygame.display.flip()
# Aktualisiere die Zeit
t += dt
# Kontrolliere die Bildwiederholrate (25 FPS)
clock.tick(25)
# Beende die Simulation, wenn die Kugel den Boden erreicht
if y > height - 50:
running = False
# Beende Pygame
pygame.quit()