# Is there any way to predict the relative position between two meshes?

In this example, Example of Camera | Babylon.js Playground (babylonjs.com) [Wrong example, what I need is position of mesh need to change]

The thing I need to achieve is for example, the start position of mesh A is (1, 0, 0), the position of B is (2, 1, 0), so there are some relative position between A and B, when A change its position, is it possible to know where B should be, if the relative position does not change?

1 Like

@pigga I have some trouble to understand what you are trying to achieve ?

The planes do not move in your example, nor loaded dynamically. More details would be great.

@bghgary might have an explanation basically here the planes do not move at all only the camera is, so you do not need to predict anything, just use the same position for the second plane.

Ah, you’re right, should found some better playground examples, the thing I need to achieve is for example, the start position of mesh A is (1, 0, 0), the position of B is (2, 1, 0), so there are some relative position between A and B, when A change its position, is it possible to know where B should be, if the relative position does not change?

just do B.setParent(A) on start and they will move together

The problem I have now is these two mesh does not load at the same time, so I can’t just setParent, only when A stop moving, B will start to load (need to be put on the relative position of A)

so just use A.parent = B and set the relative position you need on A.

Could you explain more what do you mean that “set the relative position you need on A”, why use A.parent = B, should that be B.parent = A?

If you know the starting positions then I think you could subtract A’s starting position from B’s starting position to calculate what B’s local position should be.

``````b.parent = a;
b.position = bStartPosition.subtract(aStartPosition);
``````
2 Likes

@pigga the other approach you can take is if you know the required offset between A and B, you can just update the position of B in `scene.onBeforeRenderObservable` to be `B.position = A.position + offset`. And if you need to keep the relative position to A when B loads but is determined by the current position of A when B finishes loading, you can then determine in the callback of the loader `offset = B.position - A.position` and then use that value in `scene.onBeforeRenderObservable`.

2 Likes

yup totally meant B.parent = A, my bad… it s friday 1 Like