Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trigonometry and Applications References: xyz. Motivation Our character movements so far: Other types of movement:

Similar presentations


Presentation on theme: "Trigonometry and Applications References: xyz. Motivation Our character movements so far: Other types of movement:"— Presentation transcript:

1 Trigonometry and Applications References: xyz

2 Motivation Our character movements so far: Other types of movement:

3 The "new" type of movement Move n pixels (a distance) in this direction How do we represent a direction? – In 2d…an angle.

4 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

5 Angles (2D) Degrees 0 90 180 270 45 135 360720 -90 -180

6 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…

7 Angles (2D) radians What is π? – Common answer: 3.14159… – But what does it represent??? Definition of π… Circumference = 6.283" Diameter = 2" Circumference = 1.57" Diameter = 0.5"

8 Angles (2D) radians, cont.

9 Radian angles are the (partial) distance, d, around a circle divided by the radius, r. r d

10 Angles (2D) radians, cont. 0 π

11 Conversions

12 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!

13 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.

14 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

15 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 5.3852 21.8 ◦ Reference direction

16 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.

17 Polar => Cartesian conversion CartesianCoordinates Angle (degrees)xy 010 9001 1800 2700 15.4?? Initial assumption: distance is 1.0

18 Trig to the rescue! θ A O H

19 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( θ)

20 Polar => Cartesian conversion CartesianCoordinates Angle (degrees)xy 010 9001 1800 2700 15.4 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) = 0.9641 1.0 ?? 0.9641 The opposite side's length this time O = H * sin(angle) = 1.0 * sin(15.4) = 0.2656 0.2656 15.4⁰

21 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

22 “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 -0.64. Your calculator, pygame.math, etc, already handle this. cos(130) ≈ -0.64 Interpret this as a distance of 0.64, but to the left of the origin. β 0.64 α

23 Example [Moving object, firing projectiles] – [Add a "rotate-able" object]

24 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)

25 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

26 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 200-50 = +150 150-500 = -350 (50,500) (200,150)

27 Cartesian => Polar, Problem with asin ( and acos ) θ 200-50 = +150 150-500 = -350 (50,500) (200,150) φ (-100)-50 = - 150 150-500 = -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 – ψ).

28 Cartesian=>Polar, problem with atan.

29 Cartesian => Polar

30 Conversions finished

31 Example [Making object point towards a point]


Download ppt "Trigonometry and Applications References: xyz. Motivation Our character movements so far: Other types of movement:"

Similar presentations


Ads by Google