Friday, June 29, 2007
If Only My Math Teacher Could See Me Now
I don't remember what grade I was in when I first learned about things like points, lines, and rays. It might have been third grade. I remember seeing rays represented as arrows made out of pieces of yarn in the textbook. I remember anticipating the lesson because we were going to learn about RAYS.
Like... laser rays? Ray guns?
While those sci-fi weapons were named after behavior that approximated that of a mathematical ray, I was too young to have a clue. When we learned about rays, the word that came to my mind was:
"Huh?"
Something which has an origin - a point - but no width, no length, only a direction? It goes out to infinity? What kind of useless made-up bull-hockey is this? Why are we learning this? What POSSIBLE use could this ever have in my life?
Little did I realize just how much use it would be. Even in ordinary business application programming, some of this stuff comes in handy (particularly if you are doing any GUI programming). But as a game programmer? I live in a world of rays, vectors, lines, line segments, normals, dot products, cross products, sines, cosines, arctangents, the Pythagorean theorem, matrix transformations, and quaternions.
Can the AI see the hero? Do a vector subtract of the AI's point to the hero's point. Measure its length using the Pythagorean theorum. Is it within visible range? If so, turn it into a ray using the AI's start location, and normalize that line segment by dividing it by its just-calculated length. Now use a ray test against the environment. If no collision occurs, or if the collision distance (calculated using good ol' Pythagoras's trick) is further away than the original range, then the AI can see the hero.
(Oh, yeah, and the old game programmer's trick... don't find the square root in the Pythagorean theorum if you are just comparing distances --- compare the squares. You don't need to perform the costly square root operation until you absolutely need to know the exact distance... such as when you are normalizing the vector).
You never know when this stuff could come in handy.
So there's something to tell your kids when they complain about their math class.
(Vaguely) related randomness:
* Losing Your Limits Without Losing Your Mind
* On Game Engines and Swarm Missiles
* My Worst Bug Ever (that ray can bite you if you aren't careful...)
* Jet Moto Memories
.
Labels: programming
