Trigonometry and Applications References: xyz
Motivation Our character movements so far: Other types of movement:
The "new" type of movement Move n pixels (a distance) in this direction How do we represent a direction? – In 2d…an angle.
Angles (2D) Two common systems: Degrees Radians By convention, 0 (degrees / radians) is to the right. A measure of rotation: Negative is clockwise (by convention) Positive is counter-clockwise (by convention) Also a description of orientation: How much we've rotate from the 0 (right) position
Angles (2D) Degrees
Angles (2D) degrees, cont. The number 360 is sort-of arbitrary – Evenly divisible by a lot of numbers (2, 4, 8, …) – Loosely based on #days/yr In the radians system, the number has a physical meaning…
Angles (2D) radians What is π? – Common answer: … – But what does it represent??? Definition of π… Circumference = 6.283" Diameter = 2" Circumference = 1.57" Diameter = 0.5"
Angles (2D) radians, cont.
Radian angles are the (partial) distance, d, around a circle divided by the radius, r. r d
Angles (2D) radians, cont. 0 π
Conversions
Conversions, cont. …Or just use the math functions. – math.radians(num) Converts num (assumed to be in degrees) to radians – math.degrees(num) Converts num (assumed to be in radians) to degrees Caution: – If you see the number 45.0 in your code, is it in radians or degrees? You can't tell – neither can python. – Comments are very important!
Complementay Angles A pair of complementary angles add up to 180 (degrees) If Θ and Φ are complementary… Θ + Φ = 180 The complement of 34 degrees is 146 degrees.
Cartesian coordinates What we've been using so far. – Coordinate System Origin Axes (for 2d, 2 of them: conventionally named x and y) – With an associated scale (indicated by the gray dotted lines) – Coordinates define an offset (e.g. (5,2)) Origin +x +y 5 2
Polar coordinates A new way to specify an offset. – Coordinate System Origin A "reference" direction (conventionally right) – Scale in any direction is indicated by the gray dotted lines. – Coordinates define an offset. Now, they are: A direction (an angle, relative to the "reference" direction) A distance to move in that direction Example: (5.3852, 21.8 ◦ ) Origin ◦ Reference direction
Polar / Cartesian coordinates So… o (5,2) in Cartesian coordinates and o (5.3852, 21.8 ◦ ) in Polar coordinates …Represent the same offset! o (Flip back / forth on the previous slides – look at the purple arrow) o The first is useful for "Pacman-style" offsets o The second is useful for "Asteroids-style" offsets Problem: Pygame only uses Cartesian coordinates Solution: We need a way to convert o Initially from Polar=>Cartesian o We'll see later that Cartesian=>Polar has uses too.
Polar => Cartesian conversion CartesianCoordinates Angle (degrees)xy ?? Initial assumption: distance is 1.0
Trig to the rescue! θ A O H
Trig functions θ A O H H is the distance we want to move forward A is the amount to add to our x-position O is the amount to add to our y-position (note pygame's y axis) (A,O) is the Cartesian equivalent of (H, θ) in polar coordinate. A=H*cos( θ) O=H*sin( θ)
Polar => Cartesian conversion CartesianCoordinates Angle (degrees)xy Back to our original problem… Initial assumption: distance is 1.0 This is the hypotenuse's length The length of the adjacent side's length (which we don't know)… …but we can calculate A = H * cos(angle) = 1.0 * cos(15.4) = ?? The opposite side's length this time O = H * sin(angle) = 1.0 * sin(15.4) = ⁰
Quadrants and Sign of trig functions Let Θ be any angle in the range –infininty…+infinity. Θ will be in one of 4 quadrants. The following trig functions are positive in each quadrant: – Q1: Sin(Θ), Cos(Θ), Tan(Θ) – Q2: Sin(Θ) – Q3: Tan(Θ) – Q4: Cos(Θ) Menmonic: "All Students Take Calculus" Quadrant IQuadrant II Quadrant III Quadrant IV
“Negative Distances” Let's say our angle β is 130 degrees. – This would put it in quadrant II – Sin is positive, Cos and Tan are negative. Problem: We can't draw a right triangle with an (obtuse) angle β We can, however, compute a complementary angle, α – And then a right-trangle using that angle. Notice how the adjacent side (if hyptonuse is 1) is cos(50) ≈ 0.64 This is the correct horizontal offset, but it is to the left of the origin. – So…it really should be Your calculator, pygame.math, etc, already handle this. cos(130) ≈ Interpret this as a distance of 0.64, but to the left of the origin. β 0.64 α
Example [Moving object, firing projectiles] – [Add a "rotate-able" object]
Cartesian => Polar coordinates Motivation: – Control orientation with the mouse. Previously we knew H and θ; We wanted to calculate Δx and Δy – Δx is the amount to change our x-position. – Δy is the amount to change our y-position. Now, however, want to calculate θ (from Δx and Δy) X rotSurf = pygame.transform.rotate( origSurf, degrees)
Cartesian => Polar, cont. θ A O H X We know initially: The position of the plane (x, y) The position of the mouse (mx, my) We want to calculate: The angle θ First step: We can calculate A and O below using our givens: A = mx – x O = my - y Note: it does make a difference which order you do the sub. Think of both as directions negative = left. positive = right. Similar for O
Example Cartesian => Polar (here using arcsin) Givens: Plane at (50, 500) Target at (200, 150) Step1: Calculate offsets towards target Note how we take destination minus source Step2: Calculate hypotenuse length Step3: Calculate theta = = -350 (50,500) (200,150)
Cartesian => Polar, Problem with asin ( and acos ) θ = = -350 (50,500) (200,150) φ (-100)-50 = = -350 (50,500) (-100,150) WRONG! ψ should be ≈ 113 degrees Asin ignores the sign of the horizontal direction. δ If we're in quadrant II or III, we need the complement of ψ (180 – ψ).
Cartesian=>Polar, problem with atan.
Cartesian => Polar
Conversions finished
Example [Making object point towards a point]