The following is the 5th program of VPython, demonstrating the simulation of circular motion of a sphere. 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 need to create my sphere named my_sphere with a spherical shape so called sphere with the coordinate in (1, 0, 0), as VPython using position vector represent the object position. The radius has a radius of 0.25 in size, Also, I can set the colour of an object in a wider spectrum by using vector. it is represented by RGB code. So I set colour=vector(1,1,0)
my_sphere = sphere( pos=vector(1,0,0), radius=0.25, color=vector(1,1,0), make_tail=True)Now we see the sphere! However, as I found that after making the loop, it perform the circular motion with the trajectory remain that makes a whole circle, That's not what I want, shorter trajectory following the sphere can visualise the motion of the sphere more clearly. Now I want to make a tail to track the trajectory of the sphere overtime, so I set make_trail=True. As the workspace is in black, so I now set the colour of the tail in vector form. Besides, I can also set the object's opacity. I now set the tail's opacity as 1 to make it visible.
# color = vector() uses RGB code
#my_sphere.visible = False is a code to deactivate an object but you still want it running
my_sphere = sphere( pos=vector(1,0,0), radius=0.25, color=vector(1,1,0), make_tail=True, retain = 100, tail_color = vector(1,0,1), opacity = 1.0 )
# color = vector() uses RGB code. retain = retain 100 data point on the screen, the tail's length opacity is to make the thing visibility
#my_sphere.visible = False is a code to deactivate an object but you still want it running
After setting the feature of the sphere with the tail, we need to set the time as 0 for the beginning, the shift of time would be very little i.e. 01, you can think of it as time step size.
time = 0 # Time in simulation.
dt = 0.01 # Time step size.
Next, we are going to make a while loop. For the time not bigger than 1000, it will keep running, with the rate of 100, i.e., 1000 frames per second.
# Make a loop.
while (time <= 1000):
rate(100) # Number of frames/loops per second.
# my_sphere.pos.x = my_sphere.pos.x + dx # Dot indicates an attribute.
To make a circular motion, we need to make use of the unity of a circle by introducing the sine and cosine function. Now it is like that, the sphere moves periodically in cosine function in x direction, while it moves in sine function in y direction. For the dot means the attribute of the parameter. As mentioned in Episode 4, we need to reset the time as the function is running, now we set the time a new record by the shift of dt.
my_sphere.pos.x = cos(time) #from -1 to 0 then to 1 on x-axis.
my_sphere.pos.y = sin(time) #from -1 to 0 then to 1 on y-axis.
# my_sphere.pos.x = my_sphere.pos.x + dx # Dot indicates an attribute.
time = time + dt
Here I make two more spheres to illustrate the difference in motion between sine function and cosine function.
your_sphere = sphere( pos=vector(1,0,0), radius=0.25, color=vector(1,1,1), make_trail=True, retain = 100, trail_color = vector(1,0,1), opacity = 1.0 )
#it is the white sphere.
their_sphere = sphere( pos=vector(1,0,0), radius=0.25, color=vector(0,1,0), make_trail=True, retain = 100, trail_color = vector(1,0,1), opacity = 1.0 )
#it is the green sphere.
The white sphere is called your_sphere, representing the motion in horizontal movement governing by cosine function, while the green sphere is called their_sphere, representing the motion in vertical movement governing by sine function.
your_sphere.pos.x = cos(time)#from -1 to 0 then to 1 on x-axis.As we can see that they can meet at some points as the same time. It means that they have the same amplitude at some angle. 𝜽=0°, all coincident at one point. When 𝜽=180°, white sphere and yellow sphere coincident at one point. They all perform simple harmonic motion. Circular motion is also simple harmonic motion. As we can see all the spheres move in a 2D plane, no matter how you rotate the way you view the simulation.
their_sphere.pos.y = sin(time)#from -1 to 0 then to 1 on y-axis.
To print the following code, just to let you know, it's done!
print("End of program.")
Feel free to edit the code to see what changes you can make to this simulation!