
Lesson 5 - Connecting Objects
In this lesson we look at how to move a collection of objects as one in a simple animation.
Objectives
By the end of the lesson the student should be able to:
Understand how to move a series of objects and how to uses the properties of one object to control another.
Understand how to break out of a loop if a certain condition is met.
Timings
Teaching: 40 min
Exercises: 20 min
The program
The code below constructs four boxes and then sets them in motion together as one.
# Predefine some user variables for positions and dimensions to use for the objects.
startpos=vector(-2,0,0)
box_width=1
box_height=1
box_depth=1
box_size=vector(box_width,box_height,box_depth)
#Use the predefined variables above to create our objects
bluebox = box(pos=startpos, size=box_size, color=color.blue)
yellowbox = box(pos=vector(bluebox.pos.x+box_width,0,0), size=bluebox.size,color=color.yellow)
redbox = box(pos=vector(bluebox.pos.x+(box_width*2),0,0), size=bluebox.size, color=color.red)
greenbox = box(pos=vector(yellowbox.pos.x,yellowbox.pos.y+box_height,0,0), size=bluebox.size, color=color.green)
target=vector(2,0,0) #define target coordinates for the animation to halt at.
dx=0.01 # define a small increment in position used for animation
while True:
rate(100)
# move the boxes along a bit in the +x direction
bluebox.pos.x=bluebox.pos.x+dx
yellowbox.pos.x=bluebox.pos.x+box_width # use the positions of earlier defined boxes to move subsequent shapes
redbox.pos.x=yellowbox.pos.x+box_width
greenbox.pos.x=bluebox.pos.x+box_width
if redbox.pos.x>target.x: # see if the the red box has reached the target position.
break # here we escape the loop when the green box reaches the edge of the screen
Things to note
Objects are connected using their common properties.
When working with many objects, it is often helpful and quicker to pre-define some positions,
colours, sizes and other reference information as a user variable instead of writing out the value again
as in the program above for box sizes and positions.
In the code above, notice the final 'if' statement, which stops the animation when the box exceeds the scene range by using the 'break' statement.
This statement can be used in any loop structure.
Exercise 1
Copy the program above and do the following:
1. Add a magenta box behind the yellow box.
2. Animate the magenta box so that it travels with the others. Do this in the 'while' loop.
# Predefine some user variables for positions and dimensions to use for the objects.Solution to Exercise 1
startpos=vector(-2,0,0)
box_width=1
box_height=1
box_depth=1
box_size=vector(box_width,box_height,box_depth)
#Use the predefined variables above to create our objects
bluebox = box(pos=startpos, size=box_size, color=color.blue)
yellowbox = box(pos=vector(bluebox.pos.x+box_width,0,0), size=bluebox.size,color=color.yellow)
redbox = box(pos=vector(bluebox.pos.x+(box_width*2),0,0), size=bluebox.size, color=color.red)
greenbox = box(pos=vector(yellowbox.pos.x,yellowbox.pos.y+box_height,0,0), size=bluebox.size, color=color.green)
magentabox = box(pos=vector(yellowbox.pos.x,yellowbox.pos.y,yellowbox.pos.z-box_width), size=bluebox.size, color=color.magenta)
#solution
target=vector(2,0,0) #define target coordinates for the animation to halt at.
dx=0.01 # define a small increment in position used for animation
while True:
rate(100)
# move the boxes along a bit in the +x direction
bluebox.pos.x=bluebox.pos.x+dx
yellowbox.pos.x=bluebox.pos.x+box_width # use the positions of earlier defined boxes to move subsequent shapes
redbox.pos.x=yellowbox.pos.x+box_width
greenbox.pos.x=bluebox.pos.x+box_width
magentabox.pos.x=yellowbox.pos.x
if redbox.pos.x>target.x: # see if the the red box has reached the target position.
break # here we escape the loop when the green box reaches the edge of the screen
Conclusion
You should now be able to:
Understand how to move a collection of objects together in a simple animation and position objects together in 3-D space.
Understand how to break out of a loop when a condition is met.