14
\$\begingroup\$

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.

\$\endgroup\$

5 Answers 5

21
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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'? \$\endgroup\$
    – Aviv Cohn
    Commented Jul 4, 2014 at 16:16
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2014 at 16:24
9
\$\begingroup\$

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
\$\endgroup\$
3
  • 2
    \$\begingroup\$ +1 Much more intuitive and easier to use trigonometry than matrixes. \$\endgroup\$ Commented Jul 5, 2014 at 16:32
  • \$\begingroup\$ What about the scale? When do you apply it? \$\endgroup\$ Commented Jun 3, 2015 at 15:35
  • \$\begingroup\$ Sorry, meant rotation. \$\endgroup\$ Commented Jun 3, 2015 at 18:01
7
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

You need to know the pose of Entity A in the global space \$(x_1, y_1, \theta)\$, where \$\theta\$ is the orientation relative to the x axis.

To convert the EntityB location from a global coordinate \$(x_2, y_2)\$ to a local coordinate \$(x_2',y_2')\$:

1. Using expressions

Global to Local

$$ \begin{align} x_2' &= (x_2 - x_1) \cos \theta + (y_2 - y_1) \sin \theta \\ y_2' &= -(x_2 - x_1) \sin \theta + (y_2 - y_1) \cos \theta \end{align} $$

Local to Global

$$ \begin{align} x_2 &= x_2' \cos \theta - y_2' \sin \theta + x_1 \\ y_2 &= x_2' \sin \theta + y_2' \cos \theta + y_1 \end{align} $$

2. Using matrices

$$ \begin{align} R &= \begin{bmatrix} \cos \theta & - \sin \theta \\ \sin \theta & \cos \theta \end{bmatrix} \\ \vec A &= \begin{pmatrix} x_1 & y_1 \end{pmatrix} \\ \vec B_{global} &= \begin{pmatrix} x_2 & y_2 \end{pmatrix} \\ \vec B_{local} &= \begin{pmatrix} x_2' & y_2' \end{pmatrix} \end{align} $$

Global to Local

$$ \vec B_{local} = R^{-1} \times (\vec B_{global} - \vec A) $$

(where \$R^{-1}\$ is the inverse of the matrix R)

Local to Global

$$ \vec B_{global} = R \times \vec B_{local} + \vec A $$

\$\endgroup\$
-2
\$\begingroup\$

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.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ I don't understand how this answers the question. \$\endgroup\$
    – Anko
    Commented Jul 4, 2014 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .