GlowScript Tutorial 1: Introduction and Vectors
Let’s get started. Go to http://glowscript.org. At the upper right of the screen, click “sign in”. You should be able to use your selu account or any other google account. By signing in, you can save your programs. Next go to “your programs are here” and click create a new program. Give it a name and you should see this:
Now we are going to make three balls (ball 1, 2, and 3) all three balls have a radius of 0.1 meters and the following positions:
- Ball 1 is at <0,1,0> with a red color.
- Ball 2 is at <-2, -1, 0> with a green color.
- Ball3 is at <1,-1.5,0> with a yellow color.
Here is the code to make ball 1.
In this line (line 3) you make a sphere and assign it to the name “ball1”. The sphere() is a function built into glowscript. You can assign it a position, a radius and color.
Here is the partial code for ball 2 and 3:
Yes, some of that stuff is blank. You need to fill it in. Once you get it together, and click “run this program”, it should look like this:
This is a 3d display. You can use the mouse and right-click drag to rotate the scene and you can use the scroll wheel to zoom in or out. Of course there are multiple methods for rotating and zooming the scene. If you have a track pad instead of a mouse, try ctrl-click and drag to rotate and alt-click drag to zoom.
If your program doesn’t look like that, you have made an error somewhere.
Properties of spheres.
Once you make a sphere object, you can call back some of the properties of the object. Look at this code.
ball1.pos is the vector position of the ball1. Using the print statement, the value of this will appear in the output window. This is what it looks like when you run it.
The text output window will probably be below the 3d display window. Since this is a vector, we can do some other stuff. Try this code.
Here you can see the basic vector operations of vector addition and scalar multiplication. Also you can see that you can create new variables that are just vectors ( A = vec(2,-3,-1)) creates a vector. There are two other important vector operations. The mag function finds the magnitude of a vector and norm finds the unit vector.
You can actually calculate these two quantities manually. First, if I just want the x-component of vector A, I would do this:
Now I can use the following code to calculate the magnitude of A and the unit vector in the direction of A.
Notice that in python, “sqrt” is the square root function and **2 means “to the 2 power”. You should be able to work out on paper the magnitude and unit vector for vector A. Add two print statements to check your work.
Arrows and Vectors
How do we display a vector in the 3d environment? Here is the arrow object in Glowscript.
For the arrow object, there are two important attributes. “pos” is the location of the start of the arrow and “axis” is a vector from the starting position to the end of the arrow.
Here is an arrow pointing from ball1 to ball2 (I removed ball3).
When you run it, it should look like this.
Now go back to the three balls. Make 3 arrows pointing from each ball to the other balls. That should be good for now.