Glowscript Basics: Snowman

Activity Information

Learning Goals

  • Learn the basics of Glowscript

Prior Knowledge Required

  • Vectors

Code Manipulation

  • Interpret code
  • Copy / paste code
  • Edit existing code

Activity

Handout – Snowman

  1.  Follow this link to get the starter code.
  2. Click “remix” to get your own copy of this code.
  3. Name your new trinket: snowman
  4. Use your GlowScript skills to make a snowman. Use spheres for the main body, cylinders for the eyes, and a cone for the nose. This shapes link and this colors link might be helpful.
  5.   Lets see if we can get a crate to slide up to  the snowman.  Add the following code to your trinket:
#Creating the objects
floor = box(pos=vector(0,-3,0), size=vector(10,.4,1), color=color.white)      
crate = box(pos=vector(-4,0,0), size=vector(1,1,.5), color=color.red)           

#Setting the time interval
t=0                                                                             
#I've set the initial time to zero.
tf=0.940                                                                            
#I've set the final time to 0, which gives the crate enough time to slide across the floor
dt=0.01                                                                         
#I want my time interval set at 1/100th of a time unit

#Giving the objects an initial velocity
cratev=vector(75,0,0)                                                           
#I'm defining the constant velocity of my crate to be 75 in the x-direction(left to right)                                       

while crate.pos.x<35:                                                           
#I want the crate to stop at a certain position
    rate(50)                                                                    
#This rate can speed up or slow down the replay
    crate.pos=crate.pos+cratev*dt                                               
#I'm moving the crate by adding the change in position (cratev*dt) to the previous position (crate.pos)
    t = t + dt                                                                      
#I'm updating the time

6.  Move your snow man to the end of the floor.

7.  Adjust your code such that the crate stops just as it touches the snowman. Remember, a while loop runs until the condition has been met.

Notice all of the commenting – remember, this helps us understand what the programmer is thinking.  

Code

Link

GlowScript 3.1 VPython
myBall = sphere( color = vec(1,0,0), radius = 0.5, pos = vec(0,0,0 ))
secondBall = sphere( color = vec(0,0,1), pos = vec(1,0,0), radius = 0.5)

Answer Key

Handout

4. There are infinitely many different snowmen that can be made with these instructions, but they should all have the same general form. All the spheres should be white, which means they all have: color = vec(1,1,1). The spheres also should be stacked on top of each other in the y-direction, though their exact sizes will differ. Here’s an example snowman body:

myBall = sphere( color = vec(1,1,1), radius = 0.5, pos = vec(0,0,0 ))
secondBall = sphere( color = vec(1,1,1), pos = vec(0,0.6,0), radius = 0.35)
thirdBall = sphere( color = vec(1,1,1), pos = vec(0,1,0), radius = 0.2)

The sizes, colors, and positions of the eyes and nose will vary as well, here’s an example:

eye1 = cylinder( color = vec(0,0,0), pos = vec(-0.06,1.05,0.1), axis = vec(0,0,0.1), radius = 0.04)
eye2 = cylinder( color = vec(0,0,0), pos = vec(0.06,1.05,0.1), axis = vec(0,0,0.1), radius = 0.04)
nose = cone( color = color.orange, pos = vec(0,1,0.1), axis = vec(0,0,0.3), radius = 0.04)

6. To move the snowman, we could individually change the position of each object that make it up, but it is much easier to make the snowman a compound object:

snowman = compound([myBall, secondBall, thirdBall, eye1, eye2, nose])
snowman.pos = vec(4.5,-2,0)

7. The condition statement of the while loop starts out as: “crate.pos.x<35:” It tells the loop when it should run. We need to change it so that it only runs until the crate touches the snowman.

To do this, we can find the displacement between the snowman and the crate, which is given by:

snowman.pos.x - crate.pos.x

It doesn’t make much sense to leave the right side of the inequality as 35, so let’s change it to zero, so the loop will end when the snowman and the crate have the same x position.

This causes an issue, however, because now the crate stops when it looks like it is inside of the snowman. To remedy this, we can either subtract 1 from the left side of the inequality or change the zero on the right to a 1:

snowman.pos.x - crate.pos.x -1 > 0

or:

snowman.pos.x - crate.pos.x > 1

Why 1? The centers of the snowman and the crate will be 1 away from each other when they touch, because the bottom of the snowman has a radius of 0.5 and the crate has a half-width of 0.5.

Code

Link

GlowScript 3.1 VPython
myBall = sphere( color = vec(1,1,1), radius = 0.5, pos = vec(0,0,0 ))
secondBall = sphere( color = vec(1,1,1), pos = vec(0,0.6,0), radius = 0.35)
thirdBall = sphere( color = vec(1,1,1), pos = vec(0,1,0), radius = 0.2)
eye1 = cylinder( color = vec(0,0,0), pos = vec(-0.06,1.05,0.1), axis = vec(0,0,0.1), radius = 0.04)
eye2 = cylinder( color = vec(0,0,0), pos = vec(0.06,1.05,0.1), axis = vec(0,0,0.1), radius = 0.04)
nose = cone( color = color.orange, pos = vec(0,1,0.1), axis = vec(0,0,0.3), radius = 0.04)
snowman = compound([myBall, secondBall, thirdBall, eye1, eye2, nose])
snowman.pos = vec(4.5,-2,0)

#Creating the objects
floor = box(pos=vector(0,-3,0), size=vector(10,.4,1), color=color.white)      
crate = box(pos=vector(-4,-2.3,0), size=vector(1,1,.5), color=color.red)           

#Setting the time interval
t=0                                                                             
#I've set the initial time to zero.
tf=0.940                                                                            
#I've set the final time to 0, which gives the crate enough time to slide across the floor
dt=0.01                                                                         
#I want my time interval set at 1/100th of a time unit

#Giving the objects an initial velocity
cratev=vector(75,0,0)                                                           
#I'm defining the constant velocity of my crate to be 75 in the x-direction(left to right)                                       

while snowman.pos.x - crate.pos.x > 1:                                                           
#I want the crate to stop at a certain position
    rate(50)                                                                    
#This rate can speed up or slow down the replay
    crate.pos=crate.pos+cratev*dt                                               
#I'm moving the crate by adding the change in position (cratev*dt) to the previous position (crate.pos)
    t = t + dt                                                                      
#I'm updating the time