Lab 1 Introduction To Vpython
Lab 1 Introduction To Vpython
Getting Started
Go to https://www.glowscript.org/ to get started. At the top right corner of the page, click sign in.
There, sign in with your school Google account as you would for any other school website. Once
you are signed in, it will prompt “You are signed in as your_name and your programs are here.”
on the home page. Click on the linked “here” to access your stored programs.
Create a new program and name it "Lab1". In your program editor, enter the following lines of
code:
from visual import *
Every VPython program begins with this line of code. This line tells the program to use the 3D
module called “visual” (which is what the “V” stands for in VPython!)
VPython has built-in commands to generate simple 3D objects. On the next line of your program,
type:
sphere()
Run your code. Your amazing output will open in a new browser tab or window and look very
much like this:
Error Messages
If your program contains errors, it won’t run and the Python interpreter will let you know.
Change the first line of your program to read:
An obvious mistake. Run your program now and observe the output. No sphere is created and
the error is reported something like this:
You may not understand the error messages, but it is important that you know there is an error.
You can usually find the error and fix it pretty quickly with some practice. One hint that will make
your life easier: make only a few changes to your program at a time and run it to check for errors.
If there is an error, it will be easier to track down. Correct the error now and re-run your code.
Attributes
We can change the position, radius, or color of our sphere by giving it attributes. Change the last
line of your program to read:
Run the program. Our sphere has now been given new attributes. A sphere has attributes pos,
radius, and color. The pos attribute specifies the location of the center of the sphere in the familiar
three dimensional coordinate system x-y-z. The default radius is 1, but there are no units. Feel free
to imagine what “1” is measured in. The default color is white but the other colors are blue, green,
cyan, magenta, yellow, orange, or black. Experiment with these attributes.
Create a second sphere with position (-3,-1,0) and radius = 0.15. Make this sphere green. You
should now see two spheres when you run your program. In a bit, the white sphere will represent
a baseball and the green sphere will represent a tennis ball.
Arrows
Another object we can create in our 3D graphics window is an arrow. We will use arrows to depict
quantities like velocity and force in later programs. Add the following line to your program and
run it:
To illustrate the difference between the pos and axis attributes, add another arrow to your
program:
Experiment with changing the pos and axis attributes of these arrows until you understand more
or less how they work.
Questions
It is time to pause for a moment and create the file that you will be turning in for a grade. Create a
new document and name it Lab1LastName. In the first lines of the file be sure to name the lab
and yourself appropriately. It should look like this:
In this file, you will record all of your written responses to questions, screenshots and samples of
code. You can easily spot assigned tasks because they appear just like this:
You should answer these questions in your Lab1 document now. Copy the question into your
document and follow it with your answer.
Scaling
Notice that the red arrow now points in the opposite direction because of the minus sign. It is also
half as long as the cyan arrow. Multiplying by a number changes the length of an arrow.
For the next section, you’ll only need one arrow. Instead of deleting one of the arrows from your
program, let’s “comment out” a line. Change the second to the last line of your program to the
following:
The pound sign or hash (#) at the beginning of the line causes the program to ignore this line. Run
your program to verify that the cyan arrow is no longer there. From now on, you will be asked to
place comments into your code. For example, I can do this:
# The line above used to create an arrow, but I have “commented it out”
# because I am following directions!
The interpreter will ignore all of this, but your teacher can grade it later.
Place a comment in your code that includes your name and the due date of Lab 1
Change the attributes of the red arrow so that its tail is at the position of the baseball (white
sphere) and tip is at the position of the tennis ball (green sphere). You will have to determine the
pos and axis attributes of the arrow. Remember, pos gives the position of the arrow tail, and axis
describes its length. HINT: Subtract the position of the white sphere from the position of the
green sphere and use that to set the arrow axis.
color=color.red, shaftwidth=0.1)
to the end of your arrow command. A little research will often reveal all of the fun little
improvements you can make.
We can give names to the objects in a program. This makes it easier to refer to them or their
attributes later on. Change the “sphere” lines of your program to the following:
We can now refer to the attributes of each sphere by writing, for example, tennisball.pos to
refer to the tennis ball’s position. That is, tennisball.pos is a vector with value (-3,-1,0).
Similarly, tennisball.radius is a number with value 0.15. You can check this by printing
these attributes. Add the following line to your program:
print (tennisball.pos)
The output gives us all three coordinates of the tennis ball position. If we like, we can print out
only the y coordinate. Change the last line of your code to:
print (tennisball.pos.y)
Challenges are NOT required for the assignment, but they are a big part of the fun.
Since we can refer to the attributes of objects symbolically, we want to write symbolic expressions
for the axis and pos of the arrow. The expressions should use general attribute names in symbolic
form, like tennisball.pos or baseball.pos, not specific numerical values. This way, if the
position of the tennis ball or baseball is changed, the arrow will still point from the baseball to the
tennis ball. Think about how you calculated what the arrow axis values should be and write it into
the arrow axis.
Final Tasks
Paste your final program text (not a screenshot) into your Lab1 document.
Format your code so that it looks presentable.