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 – 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 Cartesian coordinates What we've been using so far. – Origin – Axes (for 2d, 2 of them: conventionally x and y) – Coordinates define an offset (e.g. (5,2)) Origin +x +y 5 2

14 Polar coordinates A new way to specify an offset. – Origin – A "reference" direction (conventionally right) – Coordinates define an offset. Now, they are: A direction (an angle) A distance to move in that direction Example: (5.3852, 21.8 ◦ ) – Note: This offset and the Cartesian offset look (are) the same, but they are totally different coordinates Origin 5.3852 21.8 ◦ Reference direction

15 Polar / Cartesian coordinates So… – (5,2) in Cartesian coordinates and – (5.3852, 21.8 ◦ ) in Polar coordinates Represent the same offset! – The first is useful for "Pacman-style" offsets – The second is useful for "Asteroids-style" offsets Problem: Pygame only uses Cartesian coordinates Solution: We need a way to convert – Initially from Polar=>Cartesian – We'll see later that Cartesian=>Polar has uses too.

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

17 Trig to the rescue! θ A O H

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

19 “Negative Distances” Technically, a distance can’t be negative. Sometimes, it is useful, though, to indicate direction… o β ≈ 130 degrees. Cos(130) ≈ -0.64 o -0.64 represents a distance of 0.64 to the left of the origin. o Internally, math.cos calculates cos(50) (which is +0.64) and negates it because 130 degrees is pointing “left” β 0.64

20 Example [Moving object, firing projectiles] – [Add a "rotate-able" object] [Pygame transform functions (used in next section]

21 Cartesian => Polar coordinates Motivation: – Control orientation with the mouse. Previously we knew H and θ; We want 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 θ X rotSurf = pygame.transform.rotate( origSurf, degrees)

22 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

23 Example Cartesian => Polar  Givens:  Plane at (50, 150)  Target at (200, 500)  (using "normal" y-axis)  Step1: Calculate offsets towards target  Step2: Calculate hypotenuse length  Step3: Calculate theta 200-50 = +150 500-150 = +350 (50,150) (200,500)

24 Cartesian => Polar, Problem with asin ( and acos ) θ 200-50 = +150 500-150 = +350 (50,150) (200,500) φ (-100)-50 = - 150 500-150 = +350 (50,150) (-100,500) WRONG! φ should be ≈ 113 degrees Asin ignores the sign of the horizontal direction.

25 Cartesian=>Polar, problem with atan.

26 Cartesian => Polar

27 Conversions finished

28 Example [Making object point towards a point] [Making a “rotate-able” object]


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

Similar presentations


Ads by Google