Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 4665
Kernel: Python 2 (SageMath)

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.

print "Shift+Enter runs the code in these cells"

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 π\pi we would add a line: from math import sin,pi

from vpython import * #Put your cursor in this cell and hit Shift+Enter to import VPython vpython.__version__ #Display which version of VPython is used - 1.0.7 at the time of this writing
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
u'1.0.7'

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.

ball=sphere() #We've called this sphere 'ball'. Note that everything after the hashtag symbol is a comment and ignored by Python

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)

ball_blue=sphere(pos=vector(2,0,0),color=color.blue)

The blue sphere will appear in the frame above so you'll need to scroll up to see it.

You can rotate the view by placing your cursor in the frame and 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()

#Insert commands for green ball here

Let's create a new window down below so we don't have to keep scrolling back up to the top. The frame 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 but you can use any valid variable name to identify the canvas. The command to create a new frame 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. The red sphere is hidden behind the white ball so you need to rotate the frame to see it.

scene=canvas() ball1=sphere() ball2=sphere(pos=vector(0,0,-2),color=color.red) ball3=sphere(pos=vector(1,2,0),color=color.orange)
MIME type unknown not supported

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.

#Insert command here

You can change the positions as well by reassigning ball2.pos a different vector value. Let's try moving the balls around to new position. Hit Shift+Enter in the cell below to move the balls.

ball1.pos=vector(2,2,2) ball2.pos=vector(-2,4,0) ball3.pos=vector(0,0,0)

You should see that the spheres have shifted positions. This should give us an idea how to animate the spheres to move across the frame.

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 in the cell below.

t=0 while t<10: t=t+1 print t

t=0 while t<10: print t t=t+1

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:

  1. You need to put a semi-colon : at the end of the while line to tell Python this is where the loop starts.

  2. All code that is part of the loop must be indented four spaces (or one tab). 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 evaluate the cell below.

t=0 while t<10: print "I'm running" t=t+1 print "I'm done"

Now let's make use of a while loop to make a ball move. To do this we will need to specify three things:

  1. The velocity of the ball. We'll make use of the dot notation and call the velocity ball1.vel.

  2. 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 size dt

  3. 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 so rate(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.

scene2=canvas() ball1=sphere() ball2=sphere(pos=vector(0,0,-2),color=color.red) ball3=sphere(pos=vector(1,2,0),color=color.orange) ball3.vel=vector(1,0,0) dt=0.1 t=0 while t<1000: rate(10) ball3.pos=ball3.pos+ball3.vel*dt t=t+1
MIME type unknown not supported

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