What are vectors?
Vectors are sets of coordinates of varying dimension. Each coordinate in a vector represents some absolute position in that direction of the space the vector is in.
- A 1-D vector would be {1} . This could be, for example, a position at X = 1. Or a time t = 1.
- A 2-D vector would be {-4,3}. This could be, for example, a position at -4 on the X-axis, and 3 on the Y-axis. It could also be the temperature (3 degrees) at a position (-4 meters) back on the X-axis.
- A 3-D vector would be {1,2,3}. This could be a position in space 1 along the X-axis, 2 back on the Y-axis, and 3 up on the Z-axis. Or it could be 1 red, 2 green, and 3 blue in a color. Or, it could be an XY position ({1,2}) at some time T ({3}).
Note that in all cases, we've assigned meaning to the vectors for our problem. While you will commonly find vectors being used for geometry in games, there is no reason you can't do something else with them.
Why do I use vectors?
First, you never have to use vectors. As long as you are keeping track of x and y, or whatever coordinates you care about, in some way you are fine.
However, the advantage to using vectors is that they neatly represent things such as direction and position, and also have several mathematical operations defined on them that make your life easier.
For a simple example of these, consider the dot product.
Suppose you have a radar system in a top-down style game. Every enemy that appears in the sector of the radar (some pie-shaped wedge in 2D) should get a little red dot in your screen. So, you need to figure out what enemies are in your radar section.
You could test if the enemies are inside a triangle. You could also test if the enemies are contained in the intersection of the two half-spaces of the planes/lines defining the two side of the radar sector.
Or, you could just use a dot product to do the check. Here's how:
- Create a vector going from the center of the radar out towards the "front of the radar". Normalize it.
- Create a vector going from the center of the radar out towards the object we want to check the radar visibility of. Normalize it.
- Take the dot product of the two normalized vectors.
- Take the arccosine of that product, and check if it is less than half the angle of the width of the radar. If it is, draw a blip.
This is very handy, and also now lets you easily have radars that point in different directions (just change the forward vector) and have different widths (just change the radar width angle)--and you can reuse the same code for those cases too!
Why else do I use vectors?
If you are in 2D, perhaps the best way of achieving complex effects and motions (spinning, scaling, etc.) is to use a scene graph. A planet has an orbiting ship, the ship has an orbiting drone. The calculation for this without using vector math is really, really ugly.
With vector math, we represent each as having a point and a 3x3 transform matrix. The planet uses its transform, the ship uses its transform and the planet's transform, and the drone uses its transform and the ship's transform and the planet's transform.
When the planet moves, you change its transform, and the ship and drone automatically get positioned "for free". Much cleaner code.
Still not convinced.
Vectors are also the native representation for position, geometry, and motion used by nearly all graphics libraries--and certainly OpenGL and DirectX. You aren't likely to get away without having to use them.
Conclusion
Vectors are a powerful tool for writing clear code that solves geometrical problems cleanly and elegantly.
class Star
here \$\endgroup\$Point2D
used inclass ResizeRectangle
\$\endgroup\$