Getting Started with VPython

1. Create Your First Program

Get started by visiting http://www.glowscript.org/ in your web-broswer.  Any browser should be OK but Chrome is recommended.  At the upper right of the screen, click the “sign in” link.  Access to GlowScript is gated behind Google so you will need to have and to use Google credentials to log in.  By creating an account and signing in you will be able to save your programs.  Next follow the “your programs are here” link and then click “Create New Program”.  Give your program a name and you should see an empty editor window as shown below:

The line GlowScript 2.7 VPython tells which version of GlowScript you are using.  The version may be different than 2.7 but do not modify this line.

Create your first program by typing the following command which will create a white cube

box()

and then select the “Run this program” link at the top left of the editor window.  You should see something similar to the display below.

2. Zooming and Rotating

By default, the origin 〈0 , 0, 0〉 is at the center of the scene, and the “camera” (that is, your point of view) is looking directly at the origin. When you first run the program, the coordinate system has the +x direction to the right, the +y direction pointing up, and the +z direction coming out of the screen toward you. This is the same convention used in the Matter & Interactions textbook.

You can zoom or rotate the display either on your GlowScript program (not on the output shown above which is just a static image) using the following gestures:

  • On a two-button mouse, hold down both mouse buttons and move the mouse up and down to make the camera move closer or farther away from the center of the scene. (On a one-button mouse, hold down the ALT key and the mouse button.)
  • On a two-button mouse, hold down the right mouse button alone and move the mouse to make the camera “revolve” around the scene, while always looking at the center. (On a one-button mouse, hold down CTRL and the mouse button.)
  • On a mobile device use two fingers pinch gesture to zoom or a single finger drag gesture to rotate.

3. Finding and Fixing Errors

Everyone makes errors when programming and you will certainly make a few as you work to become proficient with VPython. During the course of your work with VPython you will need to find and fix two different kinds of errors:

  • Syntax Errors
  • Math, Physics, or Logic Errors

Both kinds of errors are similar to errors you may make when working pencil and paper problems using a calculator. Errors made entering expressions into your calculator are syntax errors. Using an inappropriate or incorrect equation, or trying to divide a scalar by a vector, is a physics or math error.

Common syntax errors include:

  • Typos and spelling errors
  • Missing commas, colons, equal signs, etc.
  • Unmatched parentheses

Generally you will notice syntax errors when you attempt to run the program and GlowScript reports that you have an error.  You will receive a dialog box with information about the error and you cannot dismiss it until you go back and edit your program.  If you use Chrome then the dialog box will tell you which line the error is near as shown in the figure below where the programmer typed “vect” instead of “vector” when attempting to create a sphere.

Logic errors will result in output that doesn’t match what you were expecting.  These are generally harder to catch but you will improve with practice.

4. Autoscaling and Units

VPython automatically “zooms” the camera in or out so that all objects appear in the window. This behavior is called “autoscaling.” Usually this is helpful, but occasionally we may want to turn off autoscaling.

Since VPython automatically moves the camera to try to keep all objects in the display window, it can handle any consistent set of units. We will always use SI units in our physics programs.

5. Comments

Comment lines start with a pound sign symbol #.  A comment line can be a note to yourself that comes before a block of code, such as:

# compute the new position of the ball
ball.pos = ball.pos + v*delta_t

Or, you can also put a comment at the end of a line to explain what that line does:

k = 32.0         # Define the spring constant in N/m

In addition, you can add a # symbol to the start of a line to “comment out” the line which effectively removes it temporarily, without erasing it.

# ball1 = sphere(pos=vector(1,2,3), radius=1)

6. Objects in VPython

It is often said that everything in VPython is an object. VPython objects have attributes which describe their properties. When an object is created one can set specific values for its attributes or accept default values. For example, when we created the box in our first program we used default values for all of its attributes, but could have set the position of its center with an attribute named pos and its size with attributes named length, width, and height. An additional attribute named axis controls the orientation of the box.

my_box = box(pos=vector(2,2,2), length=10, width=5, height=3)

In the above command we create a box centered at <2,2,2> with a length (along the \(x\)-axis) of 10, a width of 5 (along the \(z\) axis), and a height of 3 (along the \(y\) axis). We also gave the box a variable name of my_box which allows us to refer to either the box as a whole or in part (its attributes) later. Attributes are referenced in a form object_name.attribute_name. For example, we could move the box to a new position with a command like

# Move the center of the box to new position <1, 2, 3>
my_box.pos=vector(1,2,3)

Or we could compute the volume of the box using

# Compute and print the volume of the box
volume = my_box.length * my_box.width * my_box.height
print("The volume of the box is ", volume)