VPython for Physics Episode 4 - Animation with Loops



The following is the 4th program of Vpython, demonstrating the animation with loops.
I know you can hardly read the code, to have a better viewing experience, please choose Full Screen at the upper left hand corner.



Firstly, I want have an object in spherical shape, with the radius, its colour would be in green.

my_sphere = sphere ( pos=vector(0,0,0), radius=0.5, color=color.green )
Next, I find that green is not a good colour for demonstration, so I want to change it to red in the next line.
my_sphere.color = color.red  # dot indicate the property of the object.
Then, I wan to make a while loop. Now I make a loop counter, initially it start from 1, the sphere will move an infinitesimally small step in x, y, z coordinate, so I set the size of the steps as follow:

i = 1              # My loop counter.To keep track how of many times it moves.
dx = 0.1        # step size, differenitate form,infinitetestimally small
dy = +0.1
dz = +0.2
                     # Now I have made a loop.
After that, I want the sphere to move 150 steps, i.e., 150 loops. After 150 loops, the sphere will stop, so I set it as "smaller than or equal to" between i and 150. I want the animation to move smoothly and faster, I now set the rate as 50, meaning that the number of frame second.
while (i <= 150):   #most problems in Physic, we use while loop.
                                  # : tells the computer to pay attention to the code as follow.
                                 #if i > 100, the statement becomes false.
       rate(50)             # Number of frames/loops per second, so it can control how fast the object moves.
However, that is not enough! We haven't put these little step to our parameters. Now we need to add these little steps to our corresponding positions.
    my_sphere.pos.x = my_sphere.pos.x + dx # dot indicate the attribute
    my_sphere.pos.y = my_sphere.pos.y + dy
    my_sphere.pos.z = my_sphere.pos.z + dz
Now, the last step, is to set the counter to have a new record to know it know how many loops it has been running.
i = i + 1      #this is a assignment. Take the current value and becomes the new value,new one overwriting the previous one. 
To print the following code, just to let you know, it is done!
print("End of program.")

Feel free to edit the code to see what changes you can make to this simulation!