## 1 - Kinematics Animations

Glowscript (also called Web VPython) is a programming language based on Python. The syntax is a bit harder than PhysGL, but the output tends to be better as it can run faster. It is also close enough to the usual Python language that students often learn at school. The easiest way to create quality animations is to use an online browser-based platform called

**TRINKET.**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 \Delta t)\]

which calculates the new velocity as a vector and

\[x = x_0 + (v\times \Delta t)\]

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

- initial objects and their parameters such as location and velocity
- set the time step, eg \(\Delta t\) = 0.1
- put the physics into a loop, which then calculates the new parameters of the object(s) for every time step \((\Delta t = dt)\). 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 \Delta t)\]

which calculates the new velocity as a vector and

\[x = x_0 + (v\times \Delta t)\]

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

IMPORTANT NOTE: all computers use radians, so inside the sine and cosines the angle must be expressed as: \(\frac{\theta \times \pi}{180}\)

*x*and*y*components of the velocity are set to \(v_0 \cos{\theta} \) and \( v_0 \sin{\theta}\) respectively. See: ProjectilesIMPORTANT 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.