Tutorial - Introduction to VPython

Visual Python (VPython) is a remarkably easy to learn and use programming language that has much to offer! It is:

  • a 3-dimensional, vector language
  • uses relatively simple instructions and syntax
  • has good documentation
  • is free!

get VPython for "your very own!!!"

When you are finished the following tutorial you should be able to:

  • create a simple, 3D animated simulation
  • know how to use VPython as a "calculator" and get text output

Critical References:

  • On-line reference manual: You can access this at the URL given here or directly from the help menu selection in the VPython brower. Use this reference lots!
  • New User's tutorial: browse through this to get a feel for the structure of VPython programs and how to make and run them.
  • sample VPython program simple_vector.py

 

Tutorial Part One:

Spend 15 minutes or so skiming the New User's Tutorial. When ready try the following:

Simple Questions...

  1. What does the code fragment "from visual import *" do in any VPython program? What would happen if you left this out? (Try it if you aren't sure?)
  2. How do you run a VPython program?
  3. What is the correct way to create the object "Ball" that is a blue sphere of radius 10 units and centered at the point (30,20,-10)?
  4. What statement would assign a property "mass" to Ball and give it a value of 12.3?
  5. Is Vpython "case sensitive"? Is it "space sensitive"?
  6. What extension do VPython files use?

 

Tutorial Part Two: Vectors and Vector Math in VPython

VPython really "shines" when working with vectors! Look-up "Vector Computations" in the online-reference manual and do the following:

  1. create a VPython program (you may model this after simple_vector.py)

  2. create v1 = (-2,4,2) and draw it. (Hint: read about "arrow objects")
  3. create v2 = (3,1,7) and draw it.
  4. Create and two new vectors: v3 = v1+v2 and v4 = v2-v1
  5. Find the lengths of each of the vectors v3 and v4 using the correct call in VPython and by using methods that you learned in C2 of the text.
  6. Use VPython to determine the angle between v1 and v2. If the angle seems strange think about the "units" that VPython may be using.
  7. Give your results for parts 5 and 6 above - how do they compare?

 

Tutorial Part Three: Creating a Ball Bounce Program

In this part of the tutorial you will create a very simple program that shows you how to create an animated simulation of a ball bouncing. Do the following:

Open the VPython shell editor (press the icon). Before you begin typing, save your with a name like bounce.py - make sure to include the ".py" extension. This tells the editor that you are creating a VPython file. Now type the following code:

Sample Program Ball Bounce

from visual import*
1
#draw some axes
x_axis = arrow(pos=(0,0,0), axis=(15,0,0), shaftwidth=0.1);
y_axis = arrow(pos=(0,0,0), axis=(0,15,0), shaftwidth=0.1);
z_axis = arrow(pos=(0,0,0), axis=(0,0,15), shaftwidth=0.1);
2
h = 15;
ball = sphere(radius=2, pos=(0,h,0), color = color.blue);
ball.v = vector(0,0,0);
g = vector(0,-9.81,0);
ts = 0.01;
3
while 1:
4

rate(100);
ball.v = ball.v + g*ts;
ball.pos = ball.pos + ball.v*ts;
if ball.y <ball.radius:

5

ball.v = -0.9*ball.v;

6

Discussion with your lab partners....

  1. Discuss what each section of the code (number 1-6) does.
  2. What should the program do when it runs?
  3. Try running the program by pressing the F5 key.
  4. Explain how the concepts of "momentum change and impulse" are used in the line of code ball.v = ball.v + g*ts.

Tutorial Part Four: Extending the Program

Modify the program in the following way:

  1. permit the user to change the speed of the ball when it rebounds - we call this the coefficient of restitution. The coefficient is usually a number between 0 and 1. If, for example the coefficient of restitution was 0.90 then the ball would bounce with a speed that was 90% of its velocity on impact.
  2. give the ball an inital x-velocity
  3. provide text output that indicates the position of the ball and the time.
  4. Where is the ball 1 second after motion begins if it was given an initial velocity of (2.5, 0,0) m/s for a coefficient of restitution of 0.90?

It would be a good idea to save the modified program under a new name before running it.

What to Hand In

At the end of the lab period you should hand in all answers to questions posed in sections 1 - 4 of the tutorial and provide evidence that you created succesful VPython programs. You may do this by printing copies of your "source code" or e-mailing me copies of your VP programs.

If you worked in a group and this is a group submission be sure that each person in the group submits a greensheet.

 

Looking Ahead....

In preparation for next week's lab you may wish to investigate how to:

  • add text labels in VPython
  • graph data in VPython