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 – 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!
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
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 ◦ Reference direction
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.
Polar => Cartesian conversion CartesianCoordinates Angle (degrees)xy ?? Initial assumption: distance is 1.0 CartesianCoordinates Angle (degrees)xy
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( θ)
“Negative Distances” Technically, a distance can’t be negative. Sometimes, it is useful, though, to indicate direction… o β ≈ 130 degrees. Cos(130) ≈ o 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
Example [Moving object, firing projectiles] – [Add a "rotate-able" object] [Pygame transform functions (used in next section]
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)
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 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 = = +350 (50,150) (200,500)
Cartesian => Polar, Problem with asin ( and acos ) θ = = +350 (50,150) (200,500) φ (-100)-50 = = +350 (50,150) (-100,500) WRONG! φ should be ≈ 113 degrees Asin ignores the sign of the horizontal direction.
Cartesian=>Polar, problem with atan.
Cartesian => Polar
Conversions finished
Example [Making object point towards a point] [Making a “rotate-able” object]