Applying Trigonometry
The resources on this page were originally created by Dr. Aaron Bradley of Summit Middle School. I've done some reformatting to add clarity but that is about it. Enjoy!
|
In previous explorations, the user-controlled graphic object moved in accordance with the Cartesian plane: up, down, left, right. In this exploration, we apply trigonometry to control the object in a fashion similar to a remote-controlled car.
A Rotating Object
The simple graphics module provides one object type that can be rotated: a triangle:
from sgfx import *
import math
w = Window()
ship = w.triangle(250, 250, 10)
def left():
w.setAngle(ship, w.angle(ship) + math.pi/30)
def right():
w.setAngle(ship, w.angle(ship) - math.pi/30)
w.on('j', left)
w.on('l', right)
w.run()
The simple graphics module, like most graphics modules, uses radians for angle measurements. Recall that 0 is at 3 o'clock, π/2 is at 12 o'clock, π is at 9 o'clock, and 3π/2 is at 6 o'clock. Rotating by π/30 is the same as rotating by 180/30 = 6 degrees.
The math module provides a definition of π. Thus, the line
w.setAngle(ship, w.angle(ship) + math.pi/30)
sets the angle of the ship to π/30 beyond its current angle.
The simple graphics module provides one object type that can be rotated: a triangle:
from sgfx import *
import math
w = Window()
ship = w.triangle(250, 250, 10)
def left():
w.setAngle(ship, w.angle(ship) + math.pi/30)
def right():
w.setAngle(ship, w.angle(ship) - math.pi/30)
w.on('j', left)
w.on('l', right)
w.run()
The simple graphics module, like most graphics modules, uses radians for angle measurements. Recall that 0 is at 3 o'clock, π/2 is at 12 o'clock, π is at 9 o'clock, and 3π/2 is at 6 o'clock. Rotating by π/30 is the same as rotating by 180/30 = 6 degrees.
The math module provides a definition of π. Thus, the line
w.setAngle(ship, w.angle(ship) + math.pi/30)
sets the angle of the ship to π/30 beyond its current angle.
Moving Forward
To move the ship forward---that is, along the direction of its orientation---requires trigonometry. I'll assume that you've seen some trigonometry from math class. What we need to know for motion is relatively straightforward. If the ship is oriented at angle A and its center is at (x, y), then one unit in direction A takes the ship to (x + cos(A), y + sin(A)). The following code moves the ship 5 units. (The code should of course be inserted before w.run().)
def forward():
x, y = w.center(ship)
deltaX = 5 * math.cos(w.angle(ship))
deltaY = 5 * math.sin(w.angle(ship))
w.move(ship, deltaX, deltaY)
w.on('i', forward)
Now implement a function to move the ship backward 1 unit, and bind that function to key 'k'.
To move the ship forward---that is, along the direction of its orientation---requires trigonometry. I'll assume that you've seen some trigonometry from math class. What we need to know for motion is relatively straightforward. If the ship is oriented at angle A and its center is at (x, y), then one unit in direction A takes the ship to (x + cos(A), y + sin(A)). The following code moves the ship 5 units. (The code should of course be inserted before w.run().)
def forward():
x, y = w.center(ship)
deltaX = 5 * math.cos(w.angle(ship))
deltaY = 5 * math.sin(w.angle(ship))
w.move(ship, deltaX, deltaY)
w.on('i', forward)
Now implement a function to move the ship backward 1 unit, and bind that function to key 'k'.
Extensions
- Like in Accelerator, implement control where 'k' and 'i' slow down and speed up the ship, respectively, rather than moving the ship by -1 and 5 units, respectively.
- Make an animated object that randomly changes its orientation or speed.
- Using the animated object of Extension 2, make a game that ends when the user-controlled object overlaps the animated object (say, when their centers are within some units of each other).