Get world coordinates

Suppose I set an object relative to screen right, on position [0, 0, 0]. Is it possible to retrieve the object’s position now, as if it was relative to screen? The absolute position of the object, ignoring the relativeTo.

1 Like

It would report it’s position relative to right screen. because it’s absolute position is now relative to right screen.

If you can give more info on what your looking to get maybe we can help.

Steve

Hi @marcus

Screen right is just at [(aspect_ratio), 0, 0]. You can find the aspect ratio (and be notified of any changes caused by window resizes) by adding a handler for the “resize” event to Z.screen.

Z.screen.on("resize", (width, height) => {
  let new_aspect = width/height;
  // screenRight is now at position [new_aspect, 0, 0]
});

In general, on the Javascript side of Zappar scenes there isn’t a single “absolute” world space that everything is defined in. TriggerRegions can help to map between different spaces. More discussion of that in this thread:

Hope that helps!

2 Likes

I was able to do it. I’ll leave it here if someone needs it:

let toRelativeToScreen = node => {
        const [curX, curY, curZ] = [node.position()[0], node.position()[1], node.position()[2]];
        node.relativeTo()[0] === Z.screenRight ? node.position([curX + screenWidth/screenHeight, curY, curZ]) :
        node.relativeTo()[0] === Z.screenLeft && node.position([curX - screenWidth/screenHeight, curY, curZ]);
        node.relativeTo(Z.screen);
    }

where screenWidth and screenHeight are x and y from

Z.screen.on("resize", (x, y) => {
     screenWidth = x;
     screenHeight = y;
});
1 Like