Game Development Reference
In-Depth Information
This formula lets us define the additive inverse of a vector:
x
a
=−
1
×
a
=
y
z
We can overload the multiplication operator *= in C++ to support these operations,
with the following code in the Vector3 class.
Excerpt from include/cyclone/core.h
class Vector3
{
// ... Other Vector3 code as before ...
/** Multiplies this vector by the given scalar. */
void operator*=(const real value)
{
x *= value;
y *= value;
z *= value;
}
/** Returns a copy of this vector scaled to the given value. */
Vector3 operator*(const real value) const
{
return Vector3(x*value, y*value, z*value);
}
};
Geometrically this operation scales the vector, changing its length by the scalar. This
isshowninfigure2.4.
2.1.4
V ECTOR A DDITION AND S UBTRACTION
Geometrically, adding two vectors together is equivalent to placing them end to end.
The result is the vector from the origin of the first to the end of the second, shown
in figure 2.5. Similarly, subtracting one vector from another places the vectors end to
end, but the vector being subtracted is placed so that its end touches the end of the
first.Inotherwords,tosubtractvector b from vector a we first go forward along a ,
and then go backward along b .