O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

//returns the length of the vector
inline double Length()const;
//returns the squared length of the vector (thereby avoiding the sqrt)
inline double LengthSq()const;
inline void Normalize();
//returns the dot product of this and v2
inline double Dot(const Vector2D& v2)const;
//returns positive if v2 is clockwise of this vector,
//negative if counterclockwise (assuming the Y axis is pointing down,
//X axis to right like a Window app)
inline int Sign(const Vector2D& v2)const;
//returns the vector that is perpendicular to this one
inline Vector2D Perp()const;
//adjusts x and y so that the length of the vector does not exceed max
inline void Truncate(double max);
//returns the distance between this vector and the one passed as a parameter
inline double Distance(const Vector2D &v2)const;
//squared version of above
inline double DistanceSq(const Vector2D &v2)const;
//returns the vector that is the reverse of this vector
inline Vector2D GetReverse()const;
//we need some operators
const Vector2D& operator+=(const Vector2D &rhs);
const Vector2D& operator-=(const Vector2D &rhs);
const Vector2D& operator*=(const double& rhs);
const Vector2D& operator/=(const double& rhs;
bool operator==(const Vector2D& rhs)const;
bool operator!=(const Vector2D& rhs)const;
};
Local Space and World Space
It’s important you understand the difference between local space and world
space. The world space representation is normally what you see rendered
to your screen. Every object is defined by a position and orientation rela
-
tive to the origin of the world coordinate system (see Figure 1.23). A
soldier is using world space when he describes the position of a tank with a
grid reference, for instance.
26 | Chapter 1
Mathematics
Local space, however, describes the position and orientation of objects rel-
ative to a specific entity’s local coordinate system. In two dimensions, an
entity’s local coordinate system can be defined by a facing vector and a
side vector (representing the local x- and y-axis, respectively), with the ori-
gin positioned at the center of the entity (for three dimensions an additional
up vector is required). Figure 1.24 shows the axis describing the local coor-
dinate system of the dart-shaped object.
Using this local coordinate system we can transform the world so that all
the objects in it describe their position and orientation relative to it (see
Figure 1.25). This is just like viewing the world through the eyes of the
entity. Soldiers are using local space when they say stuff like “Target 50m
A Math and Physics Primer | 27
Mathematics
Figure 1.23. Some obstacles and a vehicle shown in world space
Figure 1.24. The vehicle’s local coordinate system

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required