Game Development Reference

In-Depth Information

Vector Subtraction: The −= Operator

This subtraction operator is used to subtract the passed vector from the current one,

which is performed on a component-by-component basis. Vector subtraction is very

similar to vector addition except that you take the reverse of the second vector and add

it to the first, as illustrated in
Figure A-5
.

Figure A-5. Vector subtraction

Here's the code that subtracts vector
u
from our
Vector
class vector:

inline Vector& Vector::operator-=(Vector u)

{

x -= u.x;

y -= u.y;

z -= u.z;

return *this;

}

Scalar Multiplication: The *= Operator

This is the scalar multiplication operator that's used to multiply a vector by a scalar,

effectively scaling the vector's length. When you multiply a vector by a scalar, you simply

multiply each vector component by the scalar quantity to obtain the new vector. The

new vector points in the same direction as the unscaled one, but its length will be dif‐

ferent (unless the scale factor is 1). This is illustrated in
Figure A-6
.

Figure A-6. Scalar multiplication

Here's the code that scales our
Vector
class vector:

inline Vector& Vector::operator*=(float s)

{

x *= s;