VPython basics
Claimed by Liubov Nikolenko
This is a beginner guide to programming in VPython. If you have never written code before, this article is for you.
Installation
To install VPython simply follow the instructions in the links given below. After the installation is completed just open the VIDLE for Python (usually there is a shortcut created on your desktop) and start typing your code.
Windows
Install VPython here
OSX
Install VPython here
Linux
Install VPython here
Getting started with Python
Introduction to basic Python use
The first two lines of your program should be:
- from __future__ import division
from visual import *
The first line enables float division, so 3 / 2 = 1.5 (not 1) and the second line enables graphics module.
Variables
One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. It is a good practice to give your variables meaningful names, so the code will become less confusing for you and other people reading it. In order to assign some value to a variable, you should use the assignment token =
.
For example,
x = 5
This line of code assigns the variable x the value 5.
y = 0 x = y
This line of code assigns the variable x the value associated with y, so x is equal to 0.
Note: unlike equality operator you cannot interchange the variables on different sides of the equality sign. Say, our initial conditions are:
x = 5 y = 0
The statement x = y
assigns the variable x value 0, while the statement y = x
assigns the variable y value 5.
Creating VPython Objects
ball = sphere(pos=(x_coordinate,y_coordinate,z_coordinate), radius=radius_of_the_sphere, color = color.color_of_the_sphere)
myArrow = arrow(pos=(x0_coordinate,y0_coordinate,z0_coordinate), axis=(x1_coordinate,y1_coordinate,z1_coordinate), color = color.color_of_the_arrow)
myVector = vector(x_coordinate,y_coordinate,z_coordinate)
You can leave a trail behind a moving object simply by specifying make_trail=True
:
ball.make_trail = True
Each time you change ball.pos, this new position is added to a curve, thereby leaving a trail behind the moving sphere. This works with arrow, box, cone, cylinder, ellipsoid, pyramid, ring, and sphere.
Manipulating VPython values
- Accessing attributes of the variable
To access the attribute of a given variable just use the syntax object.attribute
(e.g. to access the position of the ball variable, you should do ball.pos
)
- Updating variable
To update a variable (such as time, speed, force or the position of the object) you should do variable = variable + delta
Running VPython code
To run VPython code simply press F5. While running your code you will see the visual module and a shell (a place where all print out information is displayed)
Loops
There are two types of loops that can be use in Python: for loop and while loop. Basically, loops are used to tell the computer to execute the same task multiple times. While loops are more common for modeling physics concepts.
- While loops
The body of a while loop is executed, while a certain condition is true. Make sure to indent the statements that should be done in the while loop. While loop is equivalent to integration, because it manually breaks down the physical process into small parts and adds them up (see Iterative Prediction as a good example of the physical application of a while loop). While loop has the following structure:
while (condition)
- the body of the while loop
updating the loop
For example:
while (t < 60) distance = distance + velocity * deltat t = t + deltat
Note: to see the process clearly it is common to slow down the frame rate by using rate(200) as a first line of a body of the while loop
Comments
Comments in the code have no impact on the code execution, because they are just ignored by the computer. Comments in VPython start with a pound sign (#) and end when a line ends. It is a good practice to place some useful information (like an explanation to a certain step) in the comments, so other people will find it easier to understand your code. For example:
x = x + 1 # incrementing x variable
Debugging techniques
Unfortunately, sometimes programs do not work in a way we intended them to work. Here are some tips to fixing your code.
- If you get an error try to understand the error message and fix your code accordingly. The most common errors are syntax errors, like parenthesis mismatch or wrong arguments passed into the function.
- Make sure you updating your condition for a while loop, so you don't have an infinite loop.
- Check if your are dividing by zero anywhere.
- Make sure you have correct indentation everywhere.
- Put plenty of
print()
statements, so you can know what is going on in every single stage of your code. - Check your mathematical expressions for the order of precedence (e.g.
x / (y * z)
is not equivalent tox / y * z
) - Try commenting out some steps to see which parts of your code do not work.
For further information on debugging see VPython Common Errors and Troubleshooting.
Useful built-in functions
Printing out information
print(value)
Prints out the given value in the programming shell.
Math
x**y
Raises x to the y-th power.
Vectors
cross(vectorA, vectorB)
Calculates the cross product of two vectors
mag(vector)
Calculates the magnitude of the vector
norm(vector)
Calculates the unit vector of the vector
dot(vector_1, vector_2)
Calculates the dot product of two vectors
Connectedness
How is this topic connected to something that you are interested in?
I like coding and I would love more people to share my passion, so I tried my best to make the coding part of physics more approachable
How is it connected to your major?
I am CS major and Python was the first language I have learned at Tech.
Is there an interesting industrial application?
Every simulation starting from interaction of molecules can be modeled in VPython to get the general idea of the process.
See also
GlowScript
GlowScript is an easy-to-use, powerful environment for creating 3D animations and publishing them on the web. You can write and run GlowScript programs right in your browser, store them in the cloud for free, and easily share them with others. GlowScript uses VPython as a programming language. One of the greatest strengths of GlowScript is that is does not require any pre-installed software and can be ran from any computer.
General Overview of VPython
External links
The official VPython website with the links to YouTube tutorials
References
This section contains the the references you used while writing this page
3. GlowScript