A ball is an object¶
Topic: objects, dynamics
- Task:
Implement and test the simulation algorithm with just gravity.
Also add air resistance and bouncing from the ground.
Simulate the throw with different velocities or different physics.
Template: ballistic.py
ballistic.py¶
- class ballistic.Ball(x, y, vx, vy, mass=1.0, radius=0.1)[source]¶
A center-of-mass model for a ball.
The model does not include rotation of the ball.
- Parameters:
x (float) – initial x-coordinate
y (float) – initial y-coordinate
vx (float) – initial velocity x-component
vy (float) – initial velocity y-component
mass (float) – ball mass
radius (float) – ball radius, needed for bouncing
- accelerate(dt)[source]¶
Calculate the velocity of the ball after some time has passed.
The new velocity is calculated by
\[ \begin{align}\begin{aligned}v_x(t + \Delta t) = v_x(t) + \frac{F_x(t)}{m} \Delta t\\v_y(t + \Delta t) = v_y(t) + \frac{F_y(t)}{m} \Delta t.\end{aligned}\end{align} \]The new velocity is saved in the properties of the ball.
- Parameters:
dt (float) – timestep \(\Delta t\)
- apply_air_resistance(drag)[source]¶
Make air resistance act on the ball.
Adds the force \(\vec{F} = - \gamma m \vec{v}\).
- Parameters:
drag (float) – air drag coefficient \(\gamma\)
- apply_bounce(restitution)[source]¶
Make the ball bounce from the ground (ground level is at y = 0).
- Parameters:
restitution (float) – Coefficient for the elasticity of the bounce. 1 = completely elastic, 0 = completely inelastic.
- apply_gravity(g)[source]¶
Make gravity act on the ball.
Adds the force \(F_y = - m g\).
- Parameters:
g (float) – gravitational acceleration \(g\)
- move(dt)[source]¶
Calculate the position of the ball after some has passed.
The new position is calculated by
\[ \begin{align}\begin{aligned}x(t + \Delta t) = x(t) + v_x(t) \Delta t + \frac{1}{2} \frac{F_x(t)}{m} (\Delta t)^2\\y(t + \Delta t) = x(t) + v_y(t) \Delta t + \frac{1}{2} \frac{F_y(t)}{m} (\Delta t)^2.\end{aligned}\end{align} \]The new position is saved in the properties of the ball.
- Parameters:
dt (float) – timestep \(\Delta t\)
- ballistic.animate(ball)[source]¶
Animate the motion of the ball.
- Parameters:
ball (Ball) – the ball whose trajectory will be drawn
- ballistic.draw_animation_frame(frame, xs, ys)[source]¶
Draws the system at a certain time. Used for animation.
- Parameters:
frame (int) – index of the time step to draw
xs (list) – array of ball x-coordinates
ys (list) – array of ball y-coordinates
- ballistic.draw_motion_blur_image(ball)[source]¶
Draws the trajectory of the ball as a series or points.
- Parameters:
ball (Ball) – the ball whose trajectory will be drawn
- ballistic.draw_trajectory(ball)[source]¶
Draws the trajectory of the ball as a line.
- Parameters:
ball (Ball) – the ball whose trajectory will be drawn
- ballistic.main(v_start, angle)[source]¶
Main program.
Creates a ball, throws it at an angle and simulates the throw.
- Parameters:
v_start (float) – initial speed
angle (float) – throwing angle in degrees, measured from the horizontal
- ballistic.run_simulation(ball, g, drag, restitution, dt, simulation_time, recording_dt)[source]¶
Run a dynamic simulation.
The simulation runs repeating the following steps:
Forces are applied on the ball using e.g.
Ball.apply_gravity()
.The ball is moved with
Ball.move()
.The velocity is updated with
Ball.accelerate()
.
The trajectory is saved in the
Ball
object.Note
This function is incomplete!
- Parameters:
ball (Ball) – the moving ball
g (float) – acceleration due to gravity
drag (float) – air resistance factor (between 0 and 1)
restitution (float) – impact elasticity factor (between 0 and 1)
dt (float) – time step \(\Delta t\)
simulation_time (float) – total simulation time
recording_dt (float) – time between trajectory recording