# Coordinates inception

Updated: Jan 18, 2022

In AnyLogic, getting the coordinates of an agent (in discrete or continuous space) is rather simple:

`myAgent.getX() or myAgent.getY()`

But what happens if the agent is embedded in another agent?

Imagine we have a warehouse (agent), with three different storage areas each with the following dimensions 100x100, and a box in each area exactly in the center:

This warehouse is positioned at coordinates x=10 and y=10 in our environment Main.

You want to determine the X coordinate of the box within the warehouse so that you can do some calculations about where to go and pick up the box or determine what space is occupied or any other similar calculations.

Let's say we want to get the location of the box in area 3. From your mental calculation, it should be exactly 260, (2 storage areas of 100 and the box is in the middle of the third, so 100/2 = 50, the box is 10 wide, thus 100 + 100 + 50 + 10)

But when you get the X coordinate of that box the result is 50?

**This is because that box lives inside storage area 3, and its coordinates are w.r.t. that area's origin**. So it will return the X coordinate inside the storage area. In order to get the X coordinate, w.r.t. Main's origin, you need to sum the X coordinates of all the parent agents:

**box X w.r.t. Main = box X + parent storage area X + warehouse X**

Using this calculation you will get 260 as expected, the same applies for Y and Z coordinates, or any other coordinate system. But this only works if all agents have **exactly the same scale**, so please first make sure that is the case ;-), else you need to do some more mathematical acrobatics to get the right answer. Feel free to add the answer to using scales in the comments.

### Getting coordinates - the next level

That was easy and simple, but what happens if you don't know how many levels of nested agents you have? E.g. you have a box inside a box, inside a box, inside a box...inside a storage area inside a warehouse....

The same result can be achieved by recursively adding the parent agent's X coordinate:

```
Agent agent = myBox;
double x = myBox.getX();
while (agent.getOwner != null) {
agent = agent.getOwner();
double += agent.getX();
}
```

Confusing? Check the example model here or comment if you need more information:

You can also play or download the model from the cloud __here__

This post was originally sourced from a Stack Overflow question __here__

### What next?

If you liked this post, you are welcome to read more posts by following the links above to similar posts. Remember to __subscribe__, so that you can get notifications via email or you can also join the mobile app __here____! O__r follow us on any of the social media accounts for future updates. The links are in the Menu bar at the top or the footer at the bottom.

If you want to contact us for some advice, maybe a potential partnership or project or just to say "Hi!", feel free to get in touch __here__ and we will get back to you soon!