
Lesson 2 - Creating Objects
A range of common shapes is available as constructors within VPython.
In this lesson we will create and modify some of these.
Objectives
By the end of the lesson the student should be able to:.
Understand how to create a variety of shapes and position them on the screen.
Understand how to specify a colour to an object using a vector.
Understand how to use the axis attribute to change the orientation of a non-symmetrical object.
Timings
Teaching: 40 min
Exercises: 20 min
Non symmetric objects
In earlier lessons we looked at boxes and spheres which are symmetrical objects.
VPython offers other shapes which are not symmetrical and have an axis which determines which way the object points.
Here we look some of them, and in the figure opposite we have a cylinder, a cone,an arrow and a pyramid.
As you can see they all point in an upwards direction ie their axis is in the positive Y direction.
From the code below this axis is set using the axis attribute, which again is set by a vector
To point them downwards in the Y axis one simply makes the value of Y negative.
To point in the X axis the vector would be vector(1,0,0), and in the Z direction simply vector(0,0,1).
For other angles use a mix of values such as vector(1,1,0) to make the object point at 45 degrees in the XY plane.
The magnitude of the axis vecor effectively determines the length/height of the object as seen in the example.
acylinder = cylinder(pos=vector(-4,2,0), radius=1.1, axis=vector(0,1.5,0), color=color.yellow)
acone = cone(pos=vector(4,2,0), axis=vector(0,3,0), radius=1, color=color.green)
anarrow = arrow(pos=vector(-4,-4,0), axis=vector(0,4,0), shaftwidth=0.5, color=color.cyan)
apyramid = pyramid(pos=vector(4,-4,0), size=vector(4,2,2), axis=vector(0,4,0), color=color.magenta)
Exercise 1
Copy the program above and do the following:
Make the cylinder lay on its side in the X direction
Make the cone point downwards
Make the arrow point to the left at 45 degrees
And finally set the pyramid to point into the screen.
Hint: Use the same values as the examples to maintain the sizes.
Solution to Exercise 1
acylinder = cylinder(pos=vector(-4,2,0), radius=1.1, axis=vector(1.5,0,0), color=color.yellow)
acone = cone(pos=vector(4,2,0), axis=vector(0,-3,0), radius=1, color=color.green)
anarrow = arrow(pos=vector(-4,-4,0), axis=vector(-4,4,0), shaftwidth=0.5, color=color.cyan)
apyramid = pyramid(pos=vector(4,-4,0), size=vector(4,2,2), axis=vector(0,0,-4), color=color.magenta)
Things to note
All shapes are constructed by default along the X-axis. If you wish the orientation to be different, as with the pyramid from the example, the axis along which the shape follows needs to be specified.
One can also specify length. width, and height separately for some shapes instead of using the size vector. For example cylinder(length=1,height=2,width=3)
One more asymmetric object
The 3D version of an ellipse is called an ellipsoid and uses the constructor ellipsoid().
This object follows the same format as the earlier objects, which you should should now be familiar with.
Exercise 2
As a final test of understanding try to place an orange ellipsoid in the center of the objects in your program from exercise 1
oriented in the Y axis with a length of 2, a height of 1, and a width of 0.5.
hint: the dimensions of an ellipsoid are defined as size=vector(length, height, width)
Solution to Exercise 2
anellipsoid = ellipsoid(pos=vector(0,0,0), size=vector(2,1,0.5), axis=vector(0,1,0), color=color.orange)
Conclusion
You should now be able to:
Create a variety of shapes and position them on the screen in the correct orientation.