- Hands-On Game Development with WebAssembly
- Rick Battagline
- 612字
- 2021-06-24 13:41:12
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).
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:

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:

- BeagleBone By Example
- 精選單片機設計與制作30例(第2版)
- 計算機應用與維護基礎教程
- 嵌入式系統設計教程
- Svelte 3 Up and Running
- Hands-On Machine Learning with C#
- 計算機組裝維修與外設配置(高等職業院校教改示范教材·計算機系列)
- Creating Flat Design Websites
- BeagleBone Robotic Projects
- 無蘋果不生活:OS X Mountain Lion 隨身寶典
- 單片機原理與技能訓練
- Building Machine Learning Systems with Python
- Corona SDK Mobile Game Development:Beginner's Guide
- 計算機應用基礎案例教程(Windows 7+Office 2010)
- 現代多媒體技術及應用