Table of Contents

Common Commands and Tips for Glowscript

In this workshop, we are often going to use Glowscript to create computational models, which will serve as a powerful tool to help us create visualizations and apply the ideas in this course to more real-world contexts. Below are some of the common Python commands that we will use and some coding tips compiled by previous students. (Note: we do not expect you to have any coding experience prior to this course, and we will not expect you to write a program from scratch. We will primarily be asking you to interpret chunks of code with your group members and adjust/modify pieces of code that you will be given).

Common Commands

cloud = sphere(pos=vec(0,3500,0), color=color.white, radius=100)
MyBox = box(pos=vec(0,0,0), length=2, width=2, height=2, color=color.white, opacity=0.1)
object1=box(pos=vec(0,0,0), length=2, width=2,color=color.green)
object1.pos=vec(3,4,5)
basketball=sphere(pos=vec(0,0,0), color=color.white, radius=10)
basketball.velocity=vec(5,0,0)
IceCreamCone=cone(pos=vec(0,0,0), axis=vec(0,-5,0), size=vec(3,1,1))
print("Ice cream cone length:", IceCreamCone.size.x)
print("Two scoops of butter pecan please!")

Graphing

  1. A line above the while loop which creates the axes and titles of the graph (line 3 in the picture below).
  2. A line above the while loop which tells the program what you are about to graph (line 4 in the picture below).
  3. A line inside of the while loop which plots the respective x-y data point for that iteration of the while loop (line 19 in the picture below).

MyGraph1 = graph(title='Height vs Time', xtitle='Time (s)', ytitle='Height (m)', fast=False) #Name our graph and set some features
HeightGraph = gcurve(color=color.red, label='Height') # Specifies a name for the data-set that we will be plotting

Cart=box(pos=vec(0,0,0), size=vec(1,1,1),color=color.magenta)
t=0
dt=1
while True:
    rate(100)
    Cart.pos=Cart.pos+vec(0,1,0)*dt
        
    # This line plots data points as the program iterates
    HeightGraph.plot(t,Cart.pos.y) # t will be on the x-axis and Cart.pos.y will be on the y-axis
    
    t=t+dt

Programmer Logic

* If-then statements - these act as triggers to start some new event once a condition in met. For example, it we have an object moving to the right with particle.velocity=vec(1,0,0) and we want it to move in the opposite direction after 10 seconds, then you might use the following code.

Particle = sphere(pos=vec(0,0,0))
Particle.velocity=vec(1,0,0)

t=0
dt=1
tf=20

while t >= tf:
  rate(100)
  Particle.pos=Particle.pos+Particle.velocity*dt

  if t >= 10:
    Particle.velocity=vec(-1,0,0)

  t=t+dt

Mathematics

MyAnswer = 123 * 44 + 566 / 33 - 935
print(MyAnswer)
Number=5
AbsoluteNumber = abs(Number)
A=5.42*10**4
A=5.24E4
qwer=sqrt(625)
position1=vec(3,4,5)
position2=vec(2,1,0)
separation=position2-position1  #This will give separation=vec(-1,-3,-5)
testing=separation-4 #This will give an error
vector1=vec(1,2,3)
mag1=mag(vector1) #This will calculate sqrt(1^2+2^2+3^2)

Tips for Coding

A=4+4/2
B=(4+4)/2
## Set the radius (this line would be ignored by the program)
R=20 #cm (Everything in this line before the # would run in the program, everything after the # is ignored)

Math Review

Check out this link for a useful math review.