官术网_书友最值得收藏!

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
主站蜘蛛池模板: 泰顺县| 资讯 | 鞍山市| 福泉市| 绥阳县| 南澳县| 宜黄县| 双鸭山市| 大丰市| 鄯善县| 大冶市| 临城县| 镇康县| 高密市| 渭源县| 天等县| 乌拉特前旗| 阳西县| 大石桥市| 紫阳县| 洞头县| 肥西县| 冷水江市| 瑞金市| 陆川县| 长治市| 夏邑县| 遂宁市| 和政县| 车险| 呼伦贝尔市| 凉城县| 哈密市| 光山县| 大悟县| 水城县| 龙陵县| 娄底市| 庐江县| 乡宁县| 周至县|