Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

pictorial description of the same

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.

share|improve this question

5 Answers 5

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.

For matrix multiplication, see This page. For Identity matrix, see this.

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.

share|improve this answer
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. – Anurup Dey 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
share|improve this answer
+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 at 15:35
Sorry, meant rotation. – Llamageddon Jun 3 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.

share|improve this answer

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
    B_global = [x2
    B_local = [x2' 

Global to Local

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

Local to Global

    B_global = R x B_local + A
share|improve this answer

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.

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.