
Lesson 4 - Orbital Motion
Understand how to rotate an object about a reference point and its own axis.
Objectives
By the end of the lesson the student should be able to:
Understand how to move an object in an orbital motion.
Understand how to rotate an object about its own axis.
Timings
Teaching: 20 min
Exercises: 20 min
The program
In this example we will introduce orbital motion by making an object rotate about a fixed reference point.
center=vector(0,0,0) # center of screen
ballpos=vector(4,0,0) # ball offset 4 units in the x direction from center
orbit_radius=ballpos.x # the orbit radius is thus ball_pos.x
ballangle = radians(1) # converts angle in degrees to angle in radians through which to increment
# the opposite function degrees(x), converts radians to degrees.
# Boxes are used here so one can see any rotation
center_box = box(pos=center, size=vector(0.1, 0.1, 0.1), color=color.red)
ball = box(pos=ballpos, size=vector(1.5, 1, 1), color=color.yellow,
make_trail=True, interval=10, retain=5) # note you can break long lines into 2 or more
while True:
rate(100)
ball.rotate(angle=ballangle, axis=vector(0,1,0), origin=center) # code for orbit
Things to note
In the code above, orbital motion is achieved by using the built in method 'rotate'.
The 'angle' attribute of 'rotate' must be in radians.
Positive angles increase anti clockwise from 0. Negative angles increase clockwise from 0.
Introduction of the 'degrees()' function for converting radians to degrees
The complimentary function to this is 'radians()', which converts degrees to radians.
Exercise 1
Copy the program from above and do the following:
1. Change the code so that the orbiting yellow cube spins on its own axis as well as orbiting the red cube.
2. Change the code so that the orbiting cube changes between cyan and yellow after every 10 degrees of swept angle.
center=vector(0,0,0) Solution to Exercise 1
ballpos=vector(4,0,0)
orbit_radius=ballpos.x
ballangle = 0.005*pi
swept_angle=0 # a user variable to measure how far the orbit has rotated
center_box = box(pos=center, size=vector(0.1, 0.1, 0.1), color=color.red)
ball = box(pos=ballpos, size=vector(1.5, 1, 1), color=color.yellow)
while True:
rate(100)
ball.rotate(angle=ballangle, axis=vector(0,1,0), origin=center)# code for orbit
ball.rotate(angle=ballangle, axis=vector(0,0,1), origin=ball.pos) #spin cube through z axis at its current position
# If ball moves over 10 degrees make it cyan
if degrees(swept_angle) >10:
ball.color=color.cyan
else:
# otherwise make it yellow
ball.color=color.yellow
swept_angle=swept_angle+ballangle # update the swept angle
if degrees(swept_angle)>20: swept_angle=0 # reset swept angle every 20 degrees
Conclusion
You should now be able to:
Understand how to move an object in an orbital motion about a reference point.
Make an object spin on its own axis by setting the rotation originate at the center of that object.
Understand how to make decisions based on the object's angle.