3 - Modelling a Block on a Ramp
A common problem in physics is to model a block sliding down a ramp, with and without friction. For simplicity, I will use a ball instead of a block, but the ball will not roll. To model the ramp I created a box, with the axis tilted at an angle. The vector components for the axis are \(\cos{\theta}\) and \(\sin{\theta}\). To show the angle, the ground was included with the y-component of the position based on \((L\sin{\theta})/2\). Running this code and changing the angle shows that the ramp and the ground stay in contact.
As with any coded simulation, it is necessary to create an object. The syntax is: BallA = sphere(pos, radius, color). Note: color must be spelled the American way. Capitalisation is not necessary, but tends to make the code easier to read. The position variable must be a vector: pos = vector(x,y,z). It is important to use POS rather than a shorthand letter (such as x) as the position is based on a 3-D grid. Run the code to check that you have a ball. The comment command is #. Anything after this will not be read by the computer, it is purely for the benefit of the reader. Adding a make_trail = True command will leave a dot behind the sphere at its previous location. To leave a trail of dots, add trail_type = "points".
The basic concept of the code is to use the following steps:
\[v = v_0 + (a\times dt)\]
which calculates the new velocity as a vector and
\[x = x_0 + (v\times dt)\]
which calculates the new position, also as a vector. It is important that the new velocity is calculated before the position, otherwise the new position will be based on the previous velocity. This is especially important for more advanced simulations.
SYNTAX NOTE: In python, the physics loop must be indented and does not finish with the usual END command. The rate command controls how many loops are run per second. Set to rate(1/dt) to run in real time. The starting command of the loop must finish with a colon.
- initial objects and their parameters such as location and velocity
- set the time step, eg dt = 0.1
- put the physics into a loop, which then calculates the new parameters of the object(s) for every time step. This would be a very tedious job to do by hand, but computers do it faster and don't complain.
\[v = v_0 + (a\times dt)\]
which calculates the new velocity as a vector and
\[x = x_0 + (v\times dt)\]
which calculates the new position, also as a vector. It is important that the new velocity is calculated before the position, otherwise the new position will be based on the previous velocity. This is especially important for more advanced simulations.
SYNTAX NOTE: In python, the physics loop must be indented and does not finish with the usual END command. The rate command controls how many loops are run per second. Set to rate(1/dt) to run in real time. The starting command of the loop must finish with a colon.
Now you can play with changing the initial parameters. For example: giving the ball an initial horizontal velocity and a vertical acceleration will model a ball rolling off a table (projectile). Having the position, velocity and accelerations all as vectors means that you don't need to deal with components. The output image automatically rescales itself.
To make a more interesting projectile, we can define an initial speed \( v_0 \) and an angle \( \theta \). The x and y components of the velocity are set to \(v_0 \cos{\theta} \) and \( v_0 \sin{\theta}\) respectively. See: Projectiles
IMPORTANT NOTE: all computers use radians, so inside the sine and cosines the angle must be expressed as: \(\frac{\theta \times \pi}{180}\)
IMPORTANT NOTE: all computers use radians, so inside the sine and cosines the angle must be expressed as: \(\frac{\theta \times \pi}{180}\)
To add more realism, we can add the ground in to the simulation and set the loop to stop once the ball reaches the ground again. The loop cannot work with vectors, but it is the y-component that we are interested in. Note that I had to fiddle with the ground dimensions and position to ensure that the ball started above the ground, otherwise the loop would not start. SYNTAX: BallA.pos.y
ACTIVITIES
- Vary the launch positions, angles and speeds.
- Change the size and colour of the ground as well as the ball.
- Launch two balls at the same time at different speeds.