So, what is a simulation anyway?

  • A simulation is a tested scenario used for viewing results/outputs to prepare for them in real world situations

  • These can be used for games like dice rolling, spinners, etc

  • These can be used for practical things such as building structures, testing car crashes, and other things before engaging in them in the real world

  • These simulations can have the option of obeying real world physics (Gravity, collision) or they can go against these norms since this is a fictitious scenario, and couldn't happen in real life

Big Question

  • Which of the following simulations could be the LEAST useful?

  • A retailer trying to identify which products sold the most

  • A restaurant determining the efficiency of robots
  • An insurance company studying the rain impact of cars
  • A sports bike company studying design changes to their new bike design
  • If you guessed a bike company, you're wrong, because the retail simulation was the right answer. Simulating robots in food service, sudying rain impact on vehicles, and new bike design can contribute a lot more to society in comparison to seeing what products sell more than others.

Next Big Question

If you were making a simulation for making a new train station, which of the following would be true about this simulation?

  • It could reveal potential problems/safety issues before construction starts
  • It cannot be used to test the train station in different weather
  • Simulation will add high costs to projects
  • Simulation is not needed because this train station already exists
  • Potential Saftey was the right answer, because you need somewhere to test the safety and ethicness of what you're about to do before you start building it. Otherwise, let's just say you'll have a special plaque for FBI's Most Wanted

Simulation 1:

Both programs below do the same thing. Given a height and a weight, they calculate how long it will take for a object to fall to the ground in a vacuum subjected to normal Earth levels of gravity.

However, the second one is a simulation. It calculates the distance the object has fallen every 0.1 seconds. This is useful for if you wanted a visual representation of a falling object, which pure math can't do as smoothly.

height = float(input("height in meters?"))

weight = input("weight in pounds?")

stuff = (2 * (height / 9.8))**(1/2)

print("It will take", stuff,"seconds for an object that weighs",weight,"pounds","to fall ",height,"meters in a vacuum")
It will take 1.0101525445522108 seconds for an object that weighs 4 pounds to fall  5.0 meters in a vacuum
t = 0
g = 0
d = 0
false = True
while false:
    t = t + 0.1
    d = 9.8 / 2 * (t**2)
    if d >= height:
        false = False
    #print(d) # if you want to print the distance every time it calculates it. Too long to output to a terminal, but this could be useful to display graphically. 
    #print(t)

print(t)
print(d)
1.0999999999999999
5.928999999999999

Simulation 2:

  • This simulation is made in order to simulate movement on a 2d plane vs a 3d plane.

  • How it works: we have multiple variables, if statements and equations under a while command in order to randomy generate steps on a 2d plane. Once it reaches the set destination, it will say that the man made it home after x amount of steps.

  • For the 3D plane, it takes a lot longer due to how big and open the 3d environment is, so there are more if statements in the 3d plane

(explain further)

import random
x = 0
y = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    step = random.randrange(4)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1

    turn = turn + 1

    if x == 0 and y == 0:
        nights = nights + 1
        print("The Man Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y != 0:
        print("(", x,y, ")")
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Avaerage", average, "Ones that when't too long ", stopped)
import random
x = 0
y = 0
z = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    #rando movement
    step = random.randrange(6)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1
    if step == 4:
        z = z+1
    if step == 5:
        z = z-1
    #Turn counter
    turn = turn + 1
    #Goal check
    if x == 0 and y == 0 and z == 0:
        nights = nights + 1
        print("The Bird Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y + z != 0:
        print("(", x,y, ") ","| ", z)
    #Too long Stoper
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        z = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Avaerage", average,"Ones that when't too long ", stopped)

Simulations in the wild

Simulations are used extremely frequently in real life applications. One of the most common examples of simulations are video games. A games physics engine can accurately simulate objects colliding

Another example is Blender, the software used in 3d animations class, here at Del Norte. Blender is made up of many small simulations, but one big one it uses is simulating the way light bounces off of and interacts with objects.

HW !!!

There are several video game engines that can use simulations created with Python. Some examples include the Unreal Engine, CryEngine, and the Panda3D game engine. Python can be used to create complex simulations for use in video games, and many game developers use it for this purpose. Some game engines even have built-in support for Python, making it easy to integrate Python simulations into your game.

Other examples include the popular Unreal Engine and Unity, as well as CryEngine and Godot. Python can be used to create game logic, AI, and other gameplay elements in these engines. It can also be used for scripting and automating tasks, such as building levels or performing analytics on game data.

Using Python to create simulations for video games has several advantages. Python is a high-level language, which means that it is easy to read and understand. This makes it a good choice for developing complex simulations that would be difficult to implement in a lower-level language. Additionally, Python has a large and active community of users, which means that there are many resources available to help developers create simulations using the language.

Overall, the use of Python for creating simulations in video games allows developers to create complex and realistic environments and scenarios in their games, enhancing the player experience.

img1 img1

pos = 50  # The ball is initially 50 units above the ground
vel = 10  # The ball is initially moving upwards at a velocity of 10 units/second

# Define the acceleration due to gravity
g = -9.8  # The acceleration due to gravity is 9.8 m/s^2 downwards

# Define the timestep (the interval at which we will update the position of the ball)
dt = 0.1  # We will update the position of the ball every 0.1 seconds

# Define a variable to keep track of the time
time = 0

# Define the main loop of the simulation
while True:
    # Update the position of the ball using the formula:
    # pos = pos + vel * dt
    pos = pos + vel * dt

    # Update the velocity of the ball using the formula:
    # vel = vel + g * dt
    vel = vel + g * dt

    # If the ball hits the ground (if its position is less than or equal to 0), reverse its direction
    if pos <= 0:
        vel = -vel

    # Increment the time by the timestep
    time = time + dt

    # Print the current position and velocity of the ball
    print(f"Time: {time:.1f} s | Position: {pos:.1f} m | Velocity: {vel:.1f} m/s")

    # End the simulation after 5 seconds
    if time >= 5:
        break
Time: 0.1 s | Position: 51.0 m | Velocity: 9.0 m/s
Time: 0.2 s | Position: 51.9 m | Velocity: 8.0 m/s
Time: 0.3 s | Position: 52.7 m | Velocity: 7.1 m/s
Time: 0.4 s | Position: 53.4 m | Velocity: 6.1 m/s
Time: 0.5 s | Position: 54.0 m | Velocity: 5.1 m/s
Time: 0.6 s | Position: 54.5 m | Velocity: 4.1 m/s
Time: 0.7 s | Position: 54.9 m | Velocity: 3.1 m/s
Time: 0.8 s | Position: 55.3 m | Velocity: 2.2 m/s
Time: 0.9 s | Position: 55.5 m | Velocity: 1.2 m/s
Time: 1.0 s | Position: 55.6 m | Velocity: 0.2 m/s
Time: 1.1 s | Position: 55.6 m | Velocity: -0.8 m/s
Time: 1.2 s | Position: 55.5 m | Velocity: -1.8 m/s
Time: 1.3 s | Position: 55.4 m | Velocity: -2.7 m/s
Time: 1.4 s | Position: 55.1 m | Velocity: -3.7 m/s
Time: 1.5 s | Position: 54.7 m | Velocity: -4.7 m/s
Time: 1.6 s | Position: 54.2 m | Velocity: -5.7 m/s
Time: 1.7 s | Position: 53.7 m | Velocity: -6.7 m/s
Time: 1.8 s | Position: 53.0 m | Velocity: -7.6 m/s
Time: 1.9 s | Position: 52.2 m | Velocity: -8.6 m/s
Time: 2.0 s | Position: 51.4 m | Velocity: -9.6 m/s
Time: 2.1 s | Position: 50.4 m | Velocity: -10.6 m/s
Time: 2.2 s | Position: 49.4 m | Velocity: -11.6 m/s
Time: 2.3 s | Position: 48.2 m | Velocity: -12.5 m/s
Time: 2.4 s | Position: 47.0 m | Velocity: -13.5 m/s
Time: 2.5 s | Position: 45.6 m | Velocity: -14.5 m/s
Time: 2.6 s | Position: 44.1 m | Velocity: -15.5 m/s
Time: 2.7 s | Position: 42.6 m | Velocity: -16.5 m/s
Time: 2.8 s | Position: 41.0 m | Velocity: -17.4 m/s
Time: 2.9 s | Position: 39.2 m | Velocity: -18.4 m/s
Time: 3.0 s | Position: 37.4 m | Velocity: -19.4 m/s
Time: 3.1 s | Position: 35.4 m | Velocity: -20.4 m/s
Time: 3.2 s | Position: 33.4 m | Velocity: -21.4 m/s
Time: 3.3 s | Position: 31.3 m | Velocity: -22.3 m/s
Time: 3.4 s | Position: 29.0 m | Velocity: -23.3 m/s
Time: 3.5 s | Position: 26.7 m | Velocity: -24.3 m/s
Time: 3.6 s | Position: 24.3 m | Velocity: -25.3 m/s
Time: 3.7 s | Position: 21.7 m | Velocity: -26.3 m/s
Time: 3.8 s | Position: 19.1 m | Velocity: -27.2 m/s
Time: 3.9 s | Position: 16.4 m | Velocity: -28.2 m/s
Time: 4.0 s | Position: 13.6 m | Velocity: -29.2 m/s
Time: 4.1 s | Position: 10.6 m | Velocity: -30.2 m/s
Time: 4.2 s | Position: 7.6 m | Velocity: -31.2 m/s
Time: 4.3 s | Position: 4.5 m | Velocity: -32.1 m/s
Time: 4.4 s | Position: 1.3 m | Velocity: -33.1 m/s
Time: 4.5 s | Position: -2.0 m | Velocity: 34.1 m/s
Time: 4.6 s | Position: 1.4 m | Velocity: 33.1 m/s
Time: 4.7 s | Position: 4.7 m | Velocity: 32.1 m/s
Time: 4.8 s | Position: 7.9 m | Velocity: 31.2 m/s
Time: 4.9 s | Position: 11.0 m | Velocity: 30.2 m/s
Time: 5.0 s | Position: 14.0 m | Velocity: 29.2 m/s
Time: 5.1 s | Position: 17.0 m | Velocity: 28.2 m/s