The nice thing about regular posts is laying out concepts in the minimally-ordered form of paragraphs of sentences, a kind of first-order organization, and revision being the courtesy of reinterpreting the initial intuition for something clearer, falls to later vagaries of time.

As far as I know, there is no primitive representation of a vector; that is, of a concept which has both a magnitude and a direction, which can be declared in the same way as the integer or even the String. Instead, a vector is deconstructed (or constructed from) two components: dx and dy, which “JGPD” declares as float, numbers representing the horizontal or vertical quantity with which the x and y positions of an object are updated.

x += dx;
y += dy;

There’s at least one exception, which is with the paddle in the Pong clone: your cursor’s x is the immediate location of the paddle redrawn – same as the opponent’s paddle, which updates its increment of x directly by 3.0f or -3.0f; otherwise, as in the ball, its position is updated by accumulating velocity.

The vector has magnitude and direction, but these properties come out of dx and dy. Suppose a 3-4-5 triangle: the vector’s magnitude is 5 and its direction is up and to the right. But you could have determined those effects from knowing two of the three numbers, which are the 4 and the 3, or the dx and dy, respectively.

The subsequent unit vector is a division upon the dx and dy numbers by the magnitude of the vector, a hypotenuse calculated as in the Pythagorean theorem:

dx = dx / Math.sqrt(dx * dx + dy * dy);
dy = dy / Math.sqrt(dx * dx + dy * dy);

A 3-4-5 triangle has its conditions set for dx = 0.8 and dy = 0.6. The hypotenuse of a triangle with those sides has length 1. Therein is our unit vector, magnitude 1, direction pre-destined. Supposedly, multiplying the dx and dy by the desired magnitude and subtracting it from the original (5) vector lets us modify the vector without changing its direction.

“… lets us modify the vector without changing its direction.” Concretely, it lets us slow down a golf ball even as it travels along the same linear path.

### Like this:

Like Loading...

*Related*