How To: World To Local Matrix. Making sense of it!

IntroductioN

This post is an attempt to elucidate and demystify the “World to Local Matrix”. This is one concept that you would have to face some time or the other during your game development adventures. Typically game engines expose a helper function like “InverseTransformMatrix” or something else to make this easy. BUT! it’s important to understand what’s going on behind the scenes to fully appreciate it.
I will not be going into the actual calculation of the inverse matrices in this post, but rather how to visualize them when trying to transform a point from world to local space.
A short recap helps to establish a few things:

  • Basis vectors are a set of vectors that define a coordinate space. (1, 0) and (0, 1) represent X and Y axes for 2D coordinate space.
  • A point in a coordinate space is a linear combination of basis vectors.
  • World space has basis vectors that sit in the Identity matrix.

This diagram should demo the idea of points being combination of basis vectors. It’s vital that this piece of information is completely understood.
If we want to represent a point, no matter the basis, it’s going to be a linear combination of the basis vectors under consideration.

Point as Linear Combination of Basis vectors
Point as Linear Combination of Basis vectors – World Space
A New Basis

Now that we have established that a point is a linear combination of basis vectors, let’s setup a new 2D basis that’s rotated from World space basis by 45 degrees and scaled by 4.
This is how our new basis would look and it’s matrix representation:

New Basis
New Basis Vectors represented in World-Space Coordinates

Going by the rules set above that a point is a linear combination of basis vectors, (1, 0) in local space of A’ would be (4, 4) in the world space (space of A). This is basically the X-Axis of our new basis.

  • The vector (4, 4) is one unit in our new basis along X’
  • The vector (-4, 4) is one unit in out new basis along Y’
  • X’ and Y’ and effectively scaled up. Their magnitude is no longer 1.
  • X’ and Y’ are rotated by 45 degrees from the default basis vectors.

I’ve intentionally chosen slightly weird vectors to act as our new basis. Following properties can be observed in out new basis.
Let’s take another example and try to work out the local position.
Note: The bracket notation is used to represent column matrices/vectors

  • Let P = (4, 0) be a point in world-space and we want to represent that in local space of A’.
  • Let P’ = (a, b) be the point we require in local-space of A’.
  • Since P’ should be a linear combination of vectors of A’, we can represent it as follows
  • a (4, 4) + b (-4, 4) = (4, 0)
  • On solving the above equation and back-substitution, we get a = 0.5 and b = -0.5.
Point in Local Space - Manual calculation
Point in Local Space – Manual calculation

It’s that simple and those are the values that we want. (a, b) represent a point in the local-space of A’, whose world-space counterpart is (4, 0) !!
When represented in matrix notation, we get a familiar local-to-world matrix representation. Except in this case, the unknown is on the left hand side and we are solving 2 equations to obtain unknowns.

Local to world - Inverse matrix representation
World to Local – Inverse matrix representation

On moving things around a little bit, we land in a more convenient format for calculating (a, b) as shown in picture above. This is basically multiplying the World-Space point with the inverse matrix. Let’s make sense of the inverse matrix next!

The Inverse

What does it mean to represent a point in Local-Space?
The new basis that I chose is quite odd. It’s X-Axis is (4, 4) and Y-Axis is (-4 ,4) when represented in World-Space coordinates.
We want some way to represent that (4, 4) as (1, 0) in the new basis and (-4 ,4) as (0, 1).
This shouldn’t be magical, but rather should happen with what we have. As it happens, we only have the basis vectors to work with.

  • By linear combination of basis vectors of A’, we must transform X’ (4, 4) to (1, 0)
  • By linear combination of basis vectors of A’, we must transform Y'(-4, 4) to (0, 1)
  • In doing this, we would have our target representation where (4, 4) is transformed to (1, 0) and (-4, 4) is transformed to (0, 1).

Expanding on the example in the paragraph above, I’ve worked a part of it by hand. This is just to demo the idea of the inverse matrix.
NOTE that, I’ve done it using a hacky approach as we know both the Local and World spaces of couple of points.
There are obviously better approaches to calculating the inverse. This is basically our World to Local Matrix!!

Simple Inverse Matrix Calculation
Simple Inverse Matrix Calculation

This is a bit mind-bending, but try to think of Inverse matrix representing a different coordinate space with the basis vectors as the ones we’ve obtained after calculation above. Let us represent this new basis by T.

  • In this new basis, (4, 4) in local space of T is (1, 0) in world space.
  • In this new basis, (-4, 4) in local space of T is (0, 1) in world space.

This transformation essentially scales and rotates the world-space basis vectors. They are scaled and rotated such that (4, 4) is transformed to (1, 0) and (-4, 4) is transformed to (0, 1).
AND, This is the transformation we needed and we reduced it to a simple local-to-world matrix calculation.
BUT! in this case, the point being transformed is in world-space rather than in local-space of T!!
If you can swallow this, you’ve pretty much mastered inverse! Whack your head around this a little bit. Draw some picture. Make a mess!
If you think about it, transforming (4, 4) with T will give (1, 0), which represents a single unit it X-Direction inside our basis A’.

Visualizing all basis in play
All Basis vectors we have worked with in a single picture

Therefore, inverse is basically a set of basis vectors that have the offset required to transform A to Identity.
Once we have the matrix that maintains this offset, any point in world-space when transformed with T(inverse of A) will represent local-space of A’.

I hope this post helped someone and check back later to see what’s new.
That’s all for now 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *