Introduction to programming with VPython

From GeoMod

Jump to: navigation, search

Everyone should have seen in class how to write a Python script and run it. This page is an introduction to programming for beginners. It will do this by analyzing the details of python program.

Contents

Code

We will start with the bouncing ball code:

from visual import *

ball = sphere(pos=(0,4,0), color=color.red)
ball.velocity = vector(0,-1,0)

floor = box(length=4, height=0.5, width=4, color=color.blue)

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


Analysis

from visual import *

from visual import *

imports modules (other python code typically) that add functions for you to use. This line adds the visual module which renders objects in 3d.

ball = sphere(pos=(0,4,0), color=color.red)

ball = sphere(pos=(0,4,0), color=color.red)

creates a 3d object (a sphere) and defines its position and color. "ball" is the name of the object defined by the user. It is given a name so we can refer to it later. "sphere(pos=(0,4,0), color=color.red)" is a function call that creates a 3d sphere, "pos=(0,4,0)" and "color=color.red" are two arguements sent to the function that tell it what the properties of the sphere are. These two are optional arguements, they do not have to be given, and instead the function call would be "sphere()", which would create a sphere with the default position (0,0,0) and color (white). "radius" is another property of the sphere object that is not defined here. Its default value is 1.

Note that some other functions will have required arguements, without which the code will fail. The sphere() function is defined in the "visual" module imported on line 1.

Python co-ordinate system and vectors

The position of the ball is set in x,y,z Python co-ordinate system, so the ball position is 4 units along the y axis. If we wanted to change the position of the sphere we would have to reference the name of the object and the property we wish to change. Thus to move the ball to the position where x=1, y=5 and z=0 we would write,

ball.pos = vector(1,5,0)

we must use the function "vector" in this line because the position is a vector variable. In line 2, where the ball is defined, the position is automatically recognised as a vector.

The three components of the vector can be refered to individually with:

ball.pos = vector(1,5,0)
print ball.pos.x
print ball.pos.y
print ball.pos.z

to print out

1
5
0

Since the position is used often, you can also refer to its vector coordinates with

ball.pos = vector(1,5,0)
print ball.x
print ball.y
print ball.z

to get the same results

Color

The color set by "color=color.red" is one of a few pre-defined colors available. Colors can also be referenced by a vector, but in this case it is a r,g,b vector for red, green and blue. The values of this vector vary between 0 and 1. To get a red sphere you could use "color=(1,0,0)". Similarly blue would be "color=(0,0,1), and yellow "color=(1,1,0)".

ball.velocity = vector(0,-1,0)

ball.velocity = vector(0,-1,0)

Velocity is not a pre-defined property of a sphere, but we can add that property to the ball object after it has been created with this line, again using x,y,z co-ordinates.

floor = box(length=4, height=0.5, width=4, color=color.blue)

floor = box(length=4, height=0.5, width=4, color=color.blue)

Here we define another object, the floor, which is a thin box

dt = 0.01

dt = 0.01

This line defines a variable. There are a number of variable types. Here we define a floating point number, the other important type of variable we will see here is the integer which is a whole number. Many programing languages require you to explicitly define the type of variables (is it a floating point or an integer) Python does not which can make things a bit confusing. In Python you can also create a variable at any point in the code, unlike FORTRAN for example which typically requires all variables to be defined at the begining of the code.

See the section on Computer Programming at the variables link for more details.

while 1:

while 1:

This creates an infinite while loop. Typically loops in programs are created to go through a limited list of numbers or things so they have a condition attached to them. For example the lines,

i=0
while i < 5:
     i = i + 1
     print i

will print out the value of i until it's value is not less than 5. ie.:

1
2
3
4
5

The line "while 1:" has no ending condition (1 is always 1) so this loop runs forever.

Any commands inside the while loop must be indented to indicate that they are part of the loop.

rate(100)

   rate(100)

This function causes the program to pause for, in this example, 100 milliseconds. This slows down the execution of the model to a useful speed.

ball.pos = ball.pos + ball.velocity*dt

    ball.pos = ball.pos + ball.velocity*dt

Here we change the position of the ball by determining how much the ball has moved "ball.velocity*dt" and adding it to the original position of the ball "ball.pos"

Vector arithmetic

Note that "ball.pos" and "ball.velocity" are vectors, while "dt" is not, so "ball.velocity*dt" multiplies a vector by a constant which results in a vector. For example let's make the variable x a vector:

from visual import *
x = vector(1,2,0)
x = x * 2
print x

prints out

vector(2,4,0)

Assignment statements

You will note that in programing the line "x = x * 2" makes sense. This is not a mathematical equation, it is an assignment statement saying that the new value of x is equal to the old value of x times 2. The "+" and "*" are operators just like in maths, as are "-" and "/".

if ball.y < 1:

    if ball.y < 1:

This if statement tells the program what to do if certain conditions apply, in this case if the y position of the ball is less than 1.


ball.velocity.y = -ball.velocity.y

        ball.velocity.y = -ball.velocity.y

This line reverses the y component of the velocity vector, so that the lines

    if ball.y < 1:
        ball.velocity.y = -ball.velocity.y

make the ball bounce on the floor by indicating that if the ball's bosition is less than (below) the top of the floor the ball must reverse direction.

else:

    else:

This is part of the if statement structure. Simply put, the line of code indented beneath the else will only execute if the previous if statement is false. In this case it means that it will execute only if the location of the ball is above y=1.

ball.velocity.y = ball.velocity.y - 9.8*dt

        ball.velocity.y = ball.velocity.y - 9.8*dt

Accelerate the ball due to gravity. The effect of gravity on a free moving object is to accelerate it downward at 9.8 m/s^2, which results in a reduction in velocity of 9.8 m/s in every second. Therefor, if the ball is bouncing up it will slow down by 9.8 m/s/s and if it is falling it will accelerate by 9.8 m/s per second.

This line is caught up in the full if statement so the lines;

    if ball.y < 1:
        ball.velocity.y = -ball.velocity.y
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt

should logically read, if the ball's vertical position is less than 1 then bounce the ball, else, accelerate the ball downward by 9.8 m/s/s. More on Logical statements.

Bells and whistles

For a much more sophisticated version of this program check out Bounce Labeled.

Personal tools