# What is the signifigance of the Dot Product in World to Local Transformations?

Looking for help understanding why this World to Local Space function works.

I'm working my way through Buckland's Programming Game AI By Example The following function is used in the book to convert a point from world space to an agent's local coordinate space (its i hat and j hat are AgentHeading and AgentSide respectively)

    inline Vector2D PointToLocalSpace(const Vector2D &point,
Vector2D &AgentSide,
Vector2D &AgentPosition)
{

//make a copy of the point
Vector2D TransPoint = point;

//create a transformation matrix
C2DMatrix matTransform;

double Ty = -AgentPosition.Dot(AgentSide);

//create the transformation matrix
matTransform._31(Tx);           matTransform._32(Ty);

//now transform the vertices
matTransform.TransformVector2Ds(TransPoint);

return TransPoint;
}


I've been learning about vectors and matrices for the last three weeks and still I'm having a lot of trouble understanding why this works in terms of vectors and matrices. What is the reason for using a a dot product in the translation coordinates? Why is it the dot product of the agent's position and its heading? What does this dot product correspond to geometrically?

Why not just translate by the Agent's position?

Are there any simpler ways of representing this algorithm?

This is a vector projection. Dotting a vector like AgentPosition with a unit direction vector like AgentHeading or AgentSide gives you the signed length of the component of that vector parallel to that direction.

Doing it with each perpendicular axis direction lets you express a point from one coordinate system in a new coordinate system with those direction vectors as its basis.

So AgentPosition.Dot(AgentHeading) answers "how far is the agent from the origin, along the heading direction?" and AgentPosition.Dot(AgentSide) does the same for the side direction. Negating these flips the question: "how far is the origin from the agent, along this direction?"

Combined, Tx and Ty now hold the position of the world origin, from the perspective of the agent's local coordinate system, where +x is its heading, and +y is its side.

A simpler version of this algorithm (assuming the heading and side vectors are perpendicular unit vectors, so no scaling or shearing) would be:

inline Vector2D PointToLocalSpace(const Vector2D &point,
Vector2D &AgentSide,
Vector2D &AgentPosition)
{
// Get the world space vector from the agent to the point.
Vector2D offset = point.Subtract(AgentPosition);

// Project this offset onto our heading & side directions to put it in local space.
Vector2D local;
local.y = offset.Dot(AgentSide);

return local;
}


If you expand out the math, you'll find this is equivalent to the original function:

local.x = offset.x * AgentHeading.x + offset.y * AgentHeading.y
= (point.x - AgentPosition.x) * AgentHeading.x + (point.y - AgentPosition.y) * AgentHeading.y