IntroductionIntroduction. Lesson 1 A guide around the Glowscript VPython interface, and introduction to some basic programming concepts. Lesson 2 Create code to produce simple 3-D objects Lesson 3 Make an object move in a linear fashion Lesson 4 Make an object rotate around a point Lesson 5 Connect objects together using common attributes Lesson 6 Create a new object by amalgamating other objects Lesson 7 Apply textures and images to the surface of shapes Lesson 8 Demonstration of how keyboard and mouse events are handled by VPython Lesson 9 Allow the user to interact with the scene and objects within Lesson 10 Take a shape and extrude it along a path to create novel objects Lesson 11 The scene window and its properties Lesson 12 A task to use all the lesson skills to create some digital art! Glossary Terms and language used in programming for VPython

Lesson 6 - Compound Objects

In this lesson we look at another way to connect objects so that they become a new object.
These objects are called compound objects.

Objectives

By the end of the lesson the student should be able to:
  Understand how create a collection of objects and bind them together into a compound object, which can be manipulated as a single object.
  Rotate the compound object and make something happen when a certain angle is reached.
  Use the 'sleep' function to momentarily pause program execution.

Timings
Teaching: 40 min
Exercises: 20 min

The program

This program constructs several objects which are combined to form a single object - a hammer, which is later used to 'strike' the ball.

hammer_origin=vector(0,0,0) # define the center of hammer
hammer_base=vector(-2.5,0,0) # define the base of the hammer
handle_size=vector(5,1,1)

head_size=vector(1,3,1)

# set the initial position of hammer head at the end of the handle
head_pos=vector( hammer_base.x + handle_size.x+head_size.x/2, hammer_base.y, hammer_base.z)

anvil_radius=1

# set the position of the anvil relative to hammer head
anvil_pos=vector( head_pos.x,head_pos.y - head_size.y/2 - anvil_radius,head_pos.z)

# make handle and head
handle = box(pos=hammer_origin, size=handle_size, color=vector(0.72,0.42,0))
head = box( size=head_size, pos=head_pos, color=color.gray(.6) )

# join the handle and head and call it a hammer
hammer = compound( [handle, head] ) # notice the list of componants within a list[]

anvil=sphere(pos=anvil_pos,radius=1 )

Things to note

In the above program, the code which joins the head and handle is the line hammer = compound([handle, head])
This combines the two objects 'handle' and 'head' into one. The objects are placed in a list, i.e. between the square brackets [ ].
Predefining the positions and sizes of the head and handle assists in the positioning of the objects.

 

 

Exercise 1

Copy the program above and do the following:
1. Rotate the hammer so that it 'strikes' the white sphere.
2. When it 'strikes' the white sphere, let the sphere flash red momentarily.
Hint: VPython has a 'sleep()' function which mometarily pauses the program. E.g. 'sleep(1)' will pause the program for one second. Use the following to rotate the hammer:
hammer.rotate(angle=direction*da, axis=vector(0,0,1), origin=hammer_base)
Remember that angles are measured anti-clockwise from zero degrees in the +x direction and you will need to remember the direction in a variable.

Solution to Exercise 1

hammer_origin=vector(0,0,0) # define the center of hammer
hammer_base=vector(-2.5,0,0) # define the base of the hammer
handle_size=vector(5,1,1)

head_size=vector(1,3,1)
# set the initial position of hammer head at the end of the handle
head_pos=vector( hammer_base.x + handle_size.x+head_size.x/2, hammer_base.y, hammer_base.z)

anvil_radius=1
anvil_pos=vector(head_pos.x,head_pos.y-head_size.y/2-anvil_radius,head_pos.z) # set the initial angle of hammer

# make handle and head
handle = box(pos=hammer_origin, size=handle_size, color=vector(0.72,0.42,0))
head = box( size=head_size, pos=head_pos, color=color.gray(.6) )

# join the handle and head and call it a hammer
hammer = compound([handle, head])

anvil=sphere(pos=anvil_pos,radius=1 )

direction=-1 # a variable to indicate directon of rotation
da=pi/180

while True:
 rate(100)
 if hammer.pos.y<0 or hammer.pos.y>hammer.size.y:
 # when the hammer is vertical or horizontal change direction of rotation
  direction=-1*direction

 if hammer.pos.y<0 and direction>0:
  # when hammer is horizontal and going clockwise it has 'hit' sphere so change colour
  anvil.color=color.red
  # the program goes so fast we momentarily pause the program to see the colour change
  sleep(0.1)
 else:
  anvil.color=color.white

 hammer.rotate(angle=direction*da, axis=vector(0,0,1), origin=hammer_base) # The hammer is rotated around the 'z' axis

Things to note

From the solution to the exercise, we create a global variable 'direction' to indicate if the hammer is travelling clockwise or anti-clockwise.
The hammer is rotated around the 'z' axis
Each time the hammer's y coordinates reach zero, the direction is switched.
Then, the statement 'if hammer.pos.y<0 and direction>0:' is true and we know we have hit the sphere.
The colour of the sphere is changed to red and the program then 'sleeps' for 1/10 of a second to allow the user to see the 'strike'.

-

Conclusion

You should now be able to:
  Understand how create a compound object and manipulate it as a single entity.
  Rotate the compound object and let something happen when a certain angle is reached.
  Use the 'sleep' function to momentarily pause program execution.