Loops and arrays in physics simulations
Video lecture
Video in Serbian with English subtitles – turn on subtitles (CC) in the YouTube player.
Examples
Example T4.1: A free fall simulation
Enter the starting height of a body and make a free fall simulation that prints the time, the current speed and the current height every 0.05 s.
See the free fall simulation and test it on the web page: kosi-hitac.onrender.com/en/free_fall
Example T4.1b: A free fall simulation down to a given height
Find the moment in time when the body will be at a given height hz
Find the time using the simulation and also by calculation with the formula, then compare the two values
Hint:
Find the moment in time when the body will be at a given height hz:
- The user enters hz
- inside the loop, add a condition (if) that checks whether h<=hz.
- When this is true, record the time
Example T4.2
At the starting moment car 1 is 5 metres from place A, while the starting position of the second car is 350 m. For the entered speeds of the cars, find out whether the first car will catch up with the second one, and after how much time.
Introduction to arrays – the task
Create 20 balls whose centres are placed in a two-dimensional plane at random. The diameter of the balls is r. Let us create 1 ball first.
The position of the centre in the plane is given by the coordinates x and y, chosen at random
If we want x to be a random number in the range 10-290
and y in the range 0-400
x = 10 + Math.random() * 280;
y = Math.random() * 400;
Example T4.3: Balls
Create an array of balls in a horizontal plane at random.
Simulate their movement in 10 steps in random directions
Example T4.3b: Balls
Add an array of balls at the given coordinates
for x:
{ 2,6,-2,14,241,124}
for y:
{ 12,26,55,148,201,140}
Check your knowledge
Open the quiz
Quiz: Loops and arrays in simulations
1. What is the loop used for in the free fall simulation?
2. Why are the results stored in an array?
3. Which condition stops the simulation of a fall to the ground?
4. In free fall the height changes as:
