From Visual Import : Aballinabox
From Visual Import : Aballinabox
available but is no longer supported. See vpython.org for information on A Ball in a Box 1
installing VPython 7 or using GlowScript VPython. Documentation is available
1 Overview
at glowscript.org by clicking Help.
VPython is a programming language that is easy to learn and is well suited to creating 3D interactive models of physical
systems. VPython has three components that you will deal with directly:
• Python, a programming language invented in 1990 by Guido van Rossem, a Dutch computer scientist. Python is
a modern, object-oriented language which is easy to learn.
• Visual, a 3D graphics module for Python created in 2000 by David Scherer while he was an undergraduate stu-
dent at Carnegie Mellon University. Visual allows you to create and animate 3D objects, and to navigate around
in a 3D scene by spinning and zooming, using the mouse.
• IDLE, an interactive editing environment, written by van Rossem and modified by Scherer, and later by others,
which allows you to enter computer code, try your program, and get information about your program.
This tutorial assumes that Python and Visual are installed on the computer you are using.
you run your program, IDLE will save your code before running. You can undo changes to the program by pressing
CTRL-z.
6 A ball in a box
The goal of the following activity is to give you experience in using the velocity to update the position of an object, to
create a 3D animation. You will write a program to make a ball bounce around in a box, in 3D.
To position objects in the display window we use their 3D coordinates. The origin of the coor- y
dinate system is at the center of the display window. The positive x axis runs to the right, the pos-
itive y axis runs up, and the positive z axis comes out of the screen, toward you.
Start a new program like the following, which displays a ball and a wall (we’ve called it wallR x
because it is on the right side of the scene). Read the program carefully and make sure you understand
the relationship between these statements, the coordinate system, and the display generated by the program.
z
from visual import *
ball = sphere(pos=(-5,0,0), radius=0.5, color=color.cyan)
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
As with the sphere object, the pos attribute of the VPython box object positions
the center of the box. The size attribute specifies the width (in x), height (in y),
and depth (in z). Since the width has been specified to be only 0.2, the box displays
as a thin wall. There are 9 colors easily accessible by color.xxx: red, green, blue,
yellow, magenta, cyan, orange, black, and white. It is possible to design your own
colors.
signment, not equality. That is, the line assigns the vector ball.pos a new value, which is the current value of
ball.pos plus the displacement ball.velocity*deltat ( vt ), the change in the position.
to see whether 1/100 second of real time has elapsed since the previous loop. If not, VPython waits until that much
time has gone by. This ensures that there are no more than 100 loops performed in one second.)
> Run your program.
You should see the ball move to the right more slowly. However, it keeps on going right through the wall, off into
empty space, because this is what it was told to do. VPython doesn’t know any physics! You have to tell it what to do.
> Add another wall at the left side of the display, and give it the name wallL.
Make the ball bounce off that wall also.
You need to create the left wall near the beginning of the program, before the while
loop. If you put the statement inside the loop, a new wall would be created every
time the loop was executed. A very large number of walls would be created, all at
the same location. While we wouldn’t be able to see them, the computer would try
to draw them, and this would slow the program down considerably.
> Next, before the while loop, change the initial velocity to have a nonzero y
component:
ball.velocity = vector(25,5,0)
When you run the program, the ball bounces even where there is no wall! Again, the issue is that VPython doesn’t
know any physics. If we tell it to make the ball change direction when the ball’s position is to the right of the right
wall’s position, VPython goes ahead and does that, whether that makes physics sense or not. We’ll fix this later.
10 Visualizing velocity
We will often want to visualize vector quantities, such as the ball’s velocity. We can use an arrow object to visualize
the velocity of the ball.
> Before the while loop, but after the program statement that sets the ball’s velocity, create an arrow, which
you will use to visualize the velocity vector for the ball. The tail of the arrow is at the location given by pos, and
we choose that to be the location of the ball. The tip of the arrow is at the location that is a vector displacement
axis away from the tip (in this case, the location of the ball plus the velocity of the ball).
varr = arrow(pos=ball.pos, axis=ball.velocity, color=color.yellow)
It is important to create the arrow before the while loop. If we put this statement in the indented code after the
while, we would create a new arrow in every iteration. We would soon have a large number of arrows, all at the same
location. This would make the program run very slowly.
> Run your program.
You should see a yellow arrow with its tail located at the ball’s initial position, pointing in the direction of the ball’s
initial velocity. However, the arrow is huge, and it completely dominates the scene. The problem is that velocity in
meters per second and position in meters are basically different kinds of quantities, and we need to scale the velocity
in order to fit it into the diagram.
Let’s scale down the size of the arrow, by multiplying by a “scalar”, a single number. Multiplying a scalar times a
vector changes the magnitude of a vector but not its direction, since all components are scaled equally.
> Change your arrow statement to use a scale factor to scale the axis, like this, then run the program:
vscale = 0.1
varr = arrow(pos=ball.pos, axis=vscale*ball.velocity, color=color.yellow)
Run the program. Now the arrow has a reasonable size, but it doesn’t change when
the ball moves. We need to update the position and axis of the arrow every time we
move the ball.
> Inside the while loop, update the position and axis attributes of the arrow
named varr, so that the tail of the arrow is always on the ball, and the axis of the
arrow represents the current vector velocity. Remember to use the scale factor
vscale to scale the axis of the arrow. Run the program.
The arrow representing the ball’s velocity should move with the ball, and should
change direction every time the ball collides with a wall.
11 Autoscaling
By default, VPython “autoscales” the scene, by continually moving the camera forwards or backwards so that all of the
scene is in view. If desired, you can turn off autoscaling after creating the initial scene.
6 A Ball in a Box
> Just before the while loop, after drawing the initial scene, turn off further autoscaling:
scene.autoscale = False
12 Leaving a trail
Often we are interested in the trajectory of a moving object, and would like to have it leave a trail. We can make a trail
out of a curve object. A curve is an ordered list of points, which are connected by a line (actually a thin tube). We’ll
create the curve object before the loop, and append a point to it every time we move the ball.
> After creating the ball, but before the while loop, insert the following statement to create a curve object
ball.trail = curve(color=ball.color)
This creates a curve object, associated with the ball, whose color is the same as the color of the ball, but without any
points in the curve as yet.
> Inside the while loop, after updating the ball’s position, add this statement:
ball.trail.append(pos=ball.pos)
This statement appends a point to the trail. The position of the added point was
chosen to be the same as the current position of the ball. Run your program. You
should see a cyan trail behind the ball.
15 Playing around
Here are some suggestions of things you might like to play with after completing your program.
• You can make the program run much longer by changing to while t < 3e4: or you can make it run “forever”
by changing to while True:, since the condition is always True.
A Ball in a Box 7
• You might like to add some more balls, with different initial positions and initial velocities. Inside the loop you
will need position updates for each ball.
• You could make a “Pong” game by moving walls around under mouse or keyboard control. See the sections on
Mouse Interactions and Keyboard Interactions in the on-line Visual reference manual, accessible from the Help
menu in IDLE.
• You can design your own colors using fractional values for the amount of red, green, and blue. For example, if
you specify color=(1, 0.7, 0.7) you get a kind of pink (100% red, 70% green, 70% blue). The example program
colorsliders.py lets you experiment with colors.
• You could change the color of the ball or the wall whenever there is a collision.
• After the statement from visual import *, add the statement scene.stereo = ‘redcyan’ (or use
‘redblue’) and your scene will be in stereo for use with red-cyan (or red-blue) glasses (red lens on left eye).
You can also specify ‘passive’ or ‘crosseyed’ for walleyed or crosseyed stereo.
16 Example programs
If you quit VPython and restart it, you should find that choosing “Open” on the File menu gives you access to a large
number of example programs written in VPython, which may suggest useful ideas.