New in VPython 6

Change log

User forum

Contributed programs

For developers

Python web site


Simple Example


This is documentation for Classic VPython (VPython 6), which continues to be available but is no longer supported. See vpython.org for information on installing VPython 7 or using GlowScript VPython. Documentation is available at glowscript.org by clicking Help.

Here is a complete VPython program that produces a 3D animation of a red ball bouncing on a blue floor as shown above. Note that in the "while" loop there are no graphics commands, just computations to update the position of the ball and check whether it hits the floor. The 3D animation is a side effect of these computations.

from visual import *

floor = box (pos=(0,0,0), length=4, height=0.5, width=4, color=color.blue)
ball = sphere (pos=(0,4,0), radius=1, color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.01

while 1:
    rate (100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < ball.radius:
        ball.velocity.y = abs(ball.velocity.y)
        ball.velocity.y = ball.velocity.y - 9.8*dt

The program starts by importing the module "visual" which enables 3D graphics.

A box and a sphere are created and given names "floor" and "ball" so one can refer to these objects.

The ball is given a vector velocity.

In the while loop, "rate(100" has the effect "no more than 100 loops per second on fast computers."

The ball's velocity is used to update its position, in a single vector statement that updates x, y, and z.

Visual periodically use the current values of the objects' attributes, including ball.pos, to paint a 3D picture.

The program checks for the ball touching the floor and if necessary reverses the y-component of velocity, otherwise the velocity is updated due to the gravitational force.