Hands-On Game Development with WebAssembly
上QQ阅读APP看书,第一时间看更新

A short refresher on trigonometry

At this point, our collision detection algorithms start to get a lot more complicated. You may remember some of the concepts from your high school trigonometry class, but some basic trigonometry is very important for many collision detection algorithms. Even our circle collision detection that we discussed earlier relies on the Pythagorean theorem, so, in reality, unless you are doing simple non-oriented rectangle collision detection, at least a tiny amount of trigonometry is required. Trigonometry is the study of triangles in mathematics. Most games use what's called a Cartesian coordinate system. If you're not familiar with that phrase, Cartesian coordinate system means we have a grid with an x and a y coordinate (for a 2D game).

The word Cartesian means Rene Descartes invented it—the " I think; therefore, I am" guy who had a lot of great ideas in mathematics and a lot of stupid ideas in philosophy (ghost in the machine…yuck!).

There are a few key concepts we have to remember from our trigonometry classes in high school, and they all have to do with right triangles. A right triangle is a triangle with a 90-degree angle in it. That is a handy thing when you're working with a Cartesian coordinate system because your x and y axes happen to form a right angle so any line between two points that do not share either an x or a y coordinate could be considered the hypotenuse (long side) of a right triangle. There are a few ratios we also need to remember; they are as follows:

  • Sine - Y / Hypotenuse
  • Cosine - X / Hypotenuse
  • Tangent - Y / X

Do you remember SOHCAHTOA? (Pronounced "Sock-Ah-Toe-Ah")

That was meant to remind you of the following versions of the trigonometry ratios:

  • Sine - Opposite / Hypotenuse
  • Cosine - Adjacent / Hypotenuse
  • Tangent - Opposite / Adjacent

In this formulation, the opposite side of the triangle is the y axis, and the adjacent side of the triangle is the x axis. If you remember SOHCAHTOA, you may have an easier time remembering these ratios. If not, just open this book back up or use Google:

SOHCAHTOA
Some people have been taught the phrase " Some Old Horse Came A-Hoppin' Through Our Alley." I'm not sure if that is helpful. I find it more difficult to remember than SOHCAHTOA, but that's a matter of opinion. So, if imagining a horse that hops like a rabbit around some city's back alley is your bag, then, by all means, use that instead.

You may remember earlier in this book we used the angle the ship was rotated with the sin and cos math library functions to figure out how fast our ship was moving on the x axis and the y axis. Those functions return the ratio for a given angle.

Another concept we need to know is the dot product between two unit vectors. A unit vector is a vector with a length of 1. The dot product between two unit vectors is just the cosine of the angle between those two unit vectors. The closer the dot products are to 1, the closer the angle between the two vectors is to 0 degrees. If the dot product is close to 0, the angles between the two vectors are close to 90 degrees, and if the dot product between the two angles is close to -1, the angle between the two vectors is near 180 degrees. Dot products between different vectors are very useful in both collision detection and in-game physics. Refer to the following diagram:

The dot product of two normalized vectors