Put simply—etymologically, even—trigonometry is the study of triangles, and how their properties open the doors to a whole collection of nifty equations and solutions.
 |
Er, are we going to be tested on this? Well, no, but fundamental trigonometry is indisputably worth knowing if you're planning on coding any games or applications involving movement in more than one dimension. (Yes, that includes all those cool 3D games you want to make—three is more than one!) |
Recall that our Circle
instances can find the distance between two points by using the Pythagorean Theorem—an equation involving triangles. Trigonometry extends this a bit more, allowing us to find relative distances just knowing the angle we're concerned with.
The basics go something like this:
 |
|
 |
|
-
Find the direction we want to go (the SpacebumpObjects heading value, from 0 to 360).
-
Convert that heading from degrees to radians. Radians are merely a different unit for measuring angles, like degrees. (Just like you can measure distance in yards or meters, or temperature in Fahrenheit or Celsius.)
-
Utilizing our angle in radians, the cosine (cos ) of that angle will tell us the relative amount of movement on the x axis, and the sine (sin ) of that angle will tell us the relative amount of movement on the y axis. The "relative amount" will be from zero (no movement at all) to one (full movement).
-
Multiply our object's speed appropriately on each axis, and we'll get a precise amount of movement without falling back on our old RAD_TWO trick.
|
|
 |
|
 |
Yes, you read right: trigonometry, which a lot of perfectly well-adjusted human beings loathe and assume will never come in handy in real life, comes in quite handy in the real life of a game developer! The RAD_TWO
trick we used in Lecture Two is a simpler offshoot of the same principle, but only works on exactly diagonal paths. Using true trigonometry—taking advantage of the Math class's cos
and sin
methods—we can find correct distance ratios on any angle along the stage.