Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
This is a Jupyter notebook (formly called IPython). It is an interactive programming environment. It combines static texts cells (like this one) with dynamic programming cells (like the one below). To execute the code in a cell, click your cursor in the cell and then hit Shift+Enter. Try this in the cell below.
In order to run VPython you must import the module named vpython. You want to import all of the functions and methods inside the VPython module so you will use the wildcard character *
to tell Python to import everything. You do this by entering the following code into a cell:
from vpython import *
At other times we may need to import other modules. For instance, if we needed to use the sine function as well as the number we would add a line: from math import sin,pi
Now that VPython is available to this notebook, you can create a sphere using the sphere()
command. Give the sphere a name that can be used to idenfity this particular sphere. After hitting Shift+Enter in the cell below you should see a window open showing a white sphere on a black background.
Let's change the color of the ball. To do that, you need to let Python know that the color of this sphere, named ball
should be red. Python uses what is called 'dot notation' to look at properties of objects so the color of this sphere is ball.color
. As another example of this notation, the position of the ball is ball.pos
. In the cell below, enter the following:
ball.color=color.red
To add a second ball, let's say a blue ball located at x=2, y=0, and z=0 we would specify the color and position when we create the sphere. The positions are vectors so you must specify this explicitly. In the cell below create ball_blue
by entering the following:
ball_blue=sphere(pos=vector(2,0,0),color=color.blue)
You can rotate the view by holding down the right mouse button and moving the mouse around. You can zoom in and out by holding down both buttons and moving the mouse (or pinch/extend on a Mac trackpad).
Try It Yourself
Try creating a green ball located at x = -2, y= 2, and z=0. Enter your commands in the cell below.
Hint:
Green is specified by
color.green
Don't forget that the position needs to be a vector
Let's create a new window down below so we don't have to keep scrolling back up to the top. The window the objects are drawn in is called a canvas
. Like the spheres, a canvas should have a name that you can use to identify it. "Scene" is a common name for a canvas so we'll stick with that. The command to create a new window is: scene=canvas()
We'll create a couple of balls in the scene to spruce things up a bit. After you hit Shift+Enter in the cell below use the right mouse button to rotate the scene and both buttons to zoom in and out.
Try It Yourself
The positions of each ball can be found by using the .pos
command. Type ball2.pos
into the cell below and hit Shift+Enter to see the current position of the red ball.
If you can access the position of a ball by calling ball2.pos
, you should be able to change the positions as well. Let's try moving the balls around to new position. Hit Shift+Enter in the cell below to move the balls.
In order to animate the balls we need some way for the program to continue to update the positions. The way to do this is using a loop, which tells a program to perform certain functions over and over and over again. The while
loop will run while a certain condition is true. For instance while t<10
would tell the program to keep running through all of the commands in the loop while t is less than 10. Once t is greater than or equal to 10 the loop will stop.
Try to figure out what the following code will do before you hit Shift+Enter
t=0 while t<10: t=t+1 print t
Did you guess correctly? If not, try to break down why the program started at zero and ended at 9.
There are two important things for you to remember:
You need to put a semi-colon
:
at the end of thewhile
line to tell Python this is where the loop starts.All code that is part of the loop must be indented. Code that isn't indented is not part of the loop.
Try to guess what the following code will do:
t=0 while t<10: print "I'm running" t=t+1 print "I'm done"
What will the output from this program look like? Once you've made a prediction hit evaluate the cell below.
Now let's make use of a while
loop to make a ball move. To do this we will need to specify three things:
The velocity of the ball. We'll make use of the dot notation and call the velocity
ball1.vel
.The time step size. This is how much time elapses during each time we go through the
while
loop. Small time step sizes result in slower but smoother motion. We'll call our time step sizedt
The rate at which the loop runs and updates the canvas. VPython requires that you set this update speed using the
rate()
command. The rate is essentially how many frames per second the screen updates sorate(10)
roughly corresponds to 10 frames per second.
Before you evaluate the cell below, try to predict what will happen. Which ball will move and how will it move? We'll create a new canvas so you don't have to scroll up.
Try It Yourself
Update the code above so that ball2 moves with a velocity of (0,-1,0). This will require two new lines of code; one line to set the velocity of the ball and the second line inside the while
loop to update the position of ball2.
By now you should have a pretty good handle on the basics of VPython. You are ready for the next activity on modeling constant velocity motion