# Python Tutorial 5: Simulating Constant Velocity

## Tutorial Objectives and Overview

In this tutorial, you will learn how to simulate basic constant velocity motion.

• Learn how to give a simple circle object a velocity.

• Learn how to update the position of an object based on its velocity.

• Learn how to represent velocity vectors using an arrow object.

In the previous tutorial, you learned that you can get Tychos to do repetitive calculations, updating the values of variables with new values based on the calculations that were performed. In this lesson, you are going to see that you can get Tychos to perform a repetitive calculation to update the position of an object, and thus simulate motion.

## Velocity is a Rate of Change

Let's look at the definition of velocity, which we can use to calculate the change in distance that a particle will experience in a given change in time. Below is the mathematical representation of an object's velocity in the x dimension:

We can calculate how far the object will travel during a specific change in time by simply representing the change in position as:

The larger the velocity in the x direction, then the larger the position change for the given interval of time.

We are now going to also state that what is true in the x dimension is true for the y dimension (and the z dimension) as well because space is uniform and we can say that our x or y or z dimensions are really quite arbitrary:

Given that an object's initial position is (0, 0), for example, and its position is changing at the rate of 10 meters per second in the x direction and 5 meters per second in the y direction. We want to track the movement of the object by identifying the position at 0.1 second intervals. The table below shows the particle's position from 0 to 1 second:

 Time x position y position 0.1 s 0.2 s 0.3 s 0.4 s 0.5 s 0.6 s 0.7 s 0.8 s 0.9 s 1.0 s

## Iteratively Updating An Object's Position in Tychos

Doing these calculations over and over again can seem a bit tedious, and as the interval time shortens, then the number of calculations required grows...

But as discussed in the previous tutorial, computers are really good at doing these kinds of repetitive calculations. In fact, you can just set up the formula and then have the computer do all the calculations for you.

Click on the link below to open the Tychos scenario in a new browser tab where you can follow along:

Python Tutorial 5 - Simulating 2D Constant Velocity

You should see this code in the Initial State pane:

c1 = circle(pos=vec(50, 50), radius=5, color="green")

This code creates and displays a green circle in the world view grid. Now we want the circle to move, but we have to define the rate at which it is going to move.

Let's say that we wanted to simulate a particle moving along the x axis with a velocity of 10 meters per second:

We can define this velocity as a matrix in code:

v = vec(10,0)

Now we could use this vector to conduct some calculations on how to update the position of the particle. Instead, we are going to introduce here a method commonly used in many programming languages to associate this velocity to the circle c1. This way we can see that this velocity "belongs" to the circle c1 by giving the circle an attribute. Change the above line of code so that it now looks like this:

c1.v = vec(10, 0)

The velocity vector (10, 0) is now an attribute of the particle, just as the position of the particle is also an attribute that we can access like this:

c1.pos = vec(0, -50)

If you press the Start button, you will see that the circle does not move yet. This is because Tychos has no idea what velocity is. You have to define how the velocity is going to update the position. This is done in the Calculations pane.

Remember that the simulated world's time is segmented into units called steps. If the step rate is 20 steps per second, then the change in time (or dt) is .05 seconds. So if a object has a velocity of say 10 meters per second, then we need to define how far it would go in .05 seconds. This will be the rate of position change per step.

The change in position is the change in the particle's position for that step, but the velocity is defined as the rate of change per second. Add this line of code to the Calculations pane:

ds = p1.v * dt

In the above code, we are saying that the change in position (ds) will be calculated by multiplying the object's velocity by the interval time (dt). This will calculate the change in position for this step. It is an identical calculation to the one we defined above, but now just in code:

We can now use this to change the current position of the circle by recalculating the position of the circle every step. Place this line of code just below the previous line of code, once again in the Calculations Pane:

p1.pos = p1.pos + ds

When you press the Start button you should see the circle move!

Let's see if this makes sense. Because the step rate is 1/20 of a second, which is .05 seconds, and the velocity is (10, 0), then for a single step, the circle will move 0.5 units (10 x .05 = 0.5) in the x direction. After 2 steps, which is 1/10 of a second, the circle will have moved 1 unit. After 10 steps, which is 1/2 of a second, the circle will have moved 5 units in the x direction. Finally, you should see that after 20 steps, which is 1 second, the circle will have moved 10 units in the x direction. This is precisely what we want. We want the object to move at 10 units per second along the x direction.

StepTimeChange in X position

1

0.05

.5

2

0.10

1

10

0.50

5

20

1.00

10

Go back to the Initial State pane and change the particle's velocity:

p1.v = vec(10, 5)

When you press the Start button, you should see the that the particle is now traveling in a circle path - it is now traveling in both the x and y dimensions!

Now its time to work on some goals to see if you actually get it:

• Create a circle called p2 with an initial position of (100, 50)

• Make the circle c2 move so that it returns to the origin after 5 seconds.

• Create a circle called c3 with an initial position of (-70, -100)

• Make the circle c3 move so that its position is (30, 30) afte 5 seconds.

You have successfully simulated objects (circles in this case) moving through 2D space at a constant velocity!

### Using Vector Arrows To Represent Velocity

As you learned earlier, Tychos gives us a tool for visualizing a vector quantity. A vector arrow is simply an arrow that points in the direction from a specified origin with the dimensions indicated by the vector quantity - in our case the velocity vector. This can be done like this:

# Define an arrow in the Initial State Pane
a_v1 = arrow(pos=c1.pos, size=c1.v, color="purple")

This draws a vector arrow from the position of the circle and defines the length and direction of the arrow based on the velocity (represented by the circle's "v" attribute here) and then also gives it a color - in this case "purple".

In order for the arrow to follow along with the circle so that is is attached, you should write these two lines of code in the Calculations Pane:

# Update the velocity arrow
a_v1.pos = c1.pos
a_v1.size = c1.v

This vector arrows is used to help represent the motion of the circle, and you will find them very useful for visualizing the motion state of objects.

### Motion Map

Another tool that can really be helpful for visualizing the motion of an object, is a motion map. This is a visual tool used in Physics classes and textbooks to identify past positions of an object as it moves through space. A motion map is composed of "strobe images" that identify previous positions of an object, and give us a better sense of how the object's position is changing in time.

In Tychos, to do this is quite easy. If you want an object to have a motion map, you simply set the motion_map attribute of the object to True:

c1.motion_map = True
a1_v1.motion_map = True

You can then control the number of strobe images in the Settings Pane as well as turn the motion mapping visualization on or off as you desire:

## Conclusion

You should now be familiar with the following:

• How to repetitively perform a calculation to update a variable's value each step.

• Understand that the variable dt represents the time interval duration for each step.

• Understand that the time in seconds in the simulated universe (virtual time) is the result of the step rate multiplied by the time interval (dt)

• How to change an object's position by:

• Defining a velocity vector for the object.

• Multiplying the velocity vector by the time interval to get the change in position.

• Add the change in position to the object's current position in order to update it.

• How to attach arrows representing velocity vectors to an object in order to visualize the object's velocity.

Last updated