Skip to main content
1 of 3
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

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 &AgentHeading,
                             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.x = offset.Dot(AgentHeading);
  local.y = offset.Dot(AgentSide);

  return local;
}
DMGregory
  • 136.3k
  • 22
  • 247
  • 373