How do I convert from the global coordinate space to a local space?

Given an entity named EntityA, I want to define a local coordinate space, where the position of EntityA is the origin, its heading vector is the X axis, and the normal of the heading vector is the Y axis:

Given their global coordinates, how do I find the position of another entity in EntityA's local space?

An example: EntityA's global position is (50,50), and that of EntityB is (80,90). What is then the position of EntityB in EntityA's local space?

EDIT: Please go easy on the math.

Okay, so Assuming that you know what the World Transformation matrix for that object A is, You just need to construct the inverse of that matrix and you will have what you need.

Suppose the rotation, scaling and translation matrices of object A used to get it to Global Space are R, S and T respectively. You will multiply these together like

S * R * T = W

Now, take W and find its inverse W^-1 somehow. The inverse of a matrix is that matrix which does just the opposite. The product of the matrix with its inverse is always the identity matrix.

W * W^-1 = I

thus W^-1 = I / W;

Now Apply this inverse matrix as the world transformation to the scene and each object will be in the coordinates you wanted.

Here's another page which gives you the matrices you would need to make W.

In the question above, you should take translation in x axis as 50, translation in y axis as 50, no scaling in either axis, and a rotation you haven't specified.

• Thanks for the answer but I actually don't understand much of this. Not very good with math. With is a matrix? What do you mean by 'rotation, scaling and translation matrices'? – Aviv Cohn Jul 4 '14 at 16:16
• A Matrix is a very compact way of representing a set of interrelated numbers or objects. In Computer graphics, they are used extensively to make calculations. check the last link in the answer to know what I mean by Rotation translation and Scaling matrices.yes, you'll have to read all that. Otherwise you can use ashes999's method to solve your problem, but that won;t account for rotation and scaling of EntityA , if there is. – The Light Spark Jul 4 '14 at 16:24

I've done this with trigonometry rather than matrixes in the past (I am a matrix noob). Ashes999's answer is halfway there, get the relative vector, then rotate that by the inverse of EntityA's angle.

   relativeX = B.x - A.x
relativeY = B.y - A.y
rotatedX = Cos(-Angle) * relativeX - Sin(-Angle) * relativeY
rotatedY = Cos(-Angle) * relativeY + Sin(-Angle) * relativeX

• +1 Much more intuitive and easier to use trigonometry than matrixes. – Ramchandra Apte Jul 5 '14 at 16:32
• What about the scale? When do you apply it? – Llamageddon Jun 3 '15 at 15:35
• Sorry, meant rotation. – Llamageddon Jun 3 '15 at 18:01

Let me try to give you something somewhere between The Light Spark's answer and Elliot's answer, because from what I read, you're really looking for an algorithm to follow and not just math tossed at you.

Problem Statement: Given that you have a location A (50, 50) and a heading (since you didn't provide one, I'll assert it as y = 2 * x + 25), find where B (80, 90) is relative to A and the heading.

What you want to do is actually fairly straightforward. 1) Relocate A to the origin of your system. This is simply means that the local-to-A values are going to be the global position values minus the global position values of A. A becomes (0, 0) and B becomes (30, 40).

1.1) The heading also needs to be moved. This is a actually very easy to do, because the y-intercept in local-to-A terms is always 0, and the slope won't change, so we have y = 2 * x as the heading.

2) Now we need to align the prior heading to the X axis. So, how do we do this? The easiest way, conceptually to do this is to convert from x, y co-ordinates to a polar co-ordinate system. Polar co-ordinate system involves R, the distance to a location, and phi, an angle of rotation from the x-axis. R is defined as sqrt(x^2 + y^2) and phi is defined as atan(y / x). Most computer languages these days go ahead and define a atan2(y, x) function which does the exact same thing as atan(y/x) but does so in such a way that the output tends to be from -180 degrees to 180 degrees rather than 0 degrees to 360 degrees, but either work.

B thus becomes R = sqrt(30^2 + 40^2) = sqrt(2500) = 50, and phi = atan2(40, 30) = 53.13 in degrees.

Similarly, the heading now changes. This is a bit tricky to explain, but it because the heading, by definition, always passes through our origin A, we don't need to be worried about the R component. Headings are always going to be in the form of phi = C where C is a constant. In this case, phi = atan(2 * x / x) = atan(2) = 63.435 degrees.

Now, we can rotate the system to move the heading to the X-axis of the local-to-A system. Much like when we moved A to the origin of the system, all we have to do is subtract the phi of the heading from all phi values in the system. So the phi of B becomes 53.13 - 63.435 = -10.305 degrees.

Finally, we have to convert back out of polar co-ordinates into x, y co-ordinates. The formula to do that transformation are X = R * cos(phi) and Y = R * sin(phi). For B therefore, we get X = 50 * cos(-10.305) = 49.2 and Y = 50 * sin(-10.305) = 8.9, so B in local-to-A co-ordinates is close to (49,9).

Hopefully that helps, and is light enough on the math for you to follow.

You need to know the pose of Entity A in the global space (x1,y1,θ), where θ is the orientation relative to the x axis.

To convert the EntityB location from a global coordinate (x2,y2) to a local coordinate (x2',y2'):

1. Using expressions

Global to Local

x2' = (x2-x1)cosθ + (y2-y1)sinθ

y2' = -(x2-x1)sinθ + (y2-y1)cosθ


Local to Global

x2 = x2'cosθ - y2'sinθ + x1

y2 = x2'sinθ + y2'cosθ + y1


1. Using matrices:

R = [cosθ   -sinθ

sinθ    cosθ]

A = [x1
y1]

B_global = [x2
y2]

B_local = [x2'
y2']


Global to Local

    B_local = inv(R) x (B_global - A)


Local to Global

    B_global = R x B_local + A


To put it simply entity B would need a reference to entity A. You'd then need to get the difference between entity's A position and entity B's position.

• I don't understand how this answers the question. – Anko Jul 4 '14 at 15:10