Set global position of a mesh

After countless hours of trying to find a solution, I gave up and decided to ask for help here.

What I’m trying to do is simply set the absolute coordinates of a mesh, but right now I’m only able set relative coordinates. I import my meshes from Blender, where I have them placed next to each other as seen in the image below.

One potential solution I can think is setting the position of all the meshes to 0/0/0 in Blender, but I’d really rather not have them overlapping, but rather have them next to each other as they are right now.

Here’s a simplified version of the code I’m using right now. Here’s the part that loads all the meshes, probably irrelevant, but just in case:

function loadMeshes(scene, settings) {
    return new Promise(function(resolve, reject) {
        BABYLON.SceneLoader.LoadAssetContainer(`${settings.url}/models/tiles/babylon/`, 'tiles.babylon', scene, function(container) {
            resolve(container.meshes);
        });
    });
}

And here is the code that clones those meshes and places them into the scene:

let desertTiles = meshes.filter(mesh => mesh.matchesTagsQuery('desertTile'));

for (let i = 0; i < desertTiles.length; i++) {
    let generatedTile = desertTiles[i];
    generatedTile.clone(`${generatedTile.name}-i${i}`);

    generatedTile.position = new BABYLON.Vector3(0, 0, 0); // this line doesn't work
}

I’ve went through dozens of different solutions, the closest question to my problem I could find is this one (“Convert global coordinates to local coordinates”). But when I get the local coordinates and set them to be the meshes’ position, it doesn’t do anything either:

let matrix = new BABYLON.Matrix();
generatedTile.getWorldMatrix().invertToRef(matrix);
let pos = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(0, 0, 0), matrix);
generatedTile.position = pos;

I also looked at the pivot documentation but had no success with it either. Here is how my meshes look when imported right now:

https://i.imgur.com/i9tJASd.png (can’t embed due to one image per post limit)

But what I want to happen is that all the meshes are moved to the world’s 0/0/0 coordinates and overlap each other. If you need any more of the code, a playground example, or the models to figure it out, just tell me and I’ll provide. Also sorry if my question was a bit confusingly worded; if something isn’t clear I’ll elaborate. Thank you in advance!

Hello and welcome!

does this work for you?

mesh.setAbsolutePosition(BABYLON.Vector3.Zero())
1 Like

Damn. I remember trying to use that method before, but it didn’t work. The problem was that I called setAbsolutePosition() on a wrong variable (I didn’t assign the return value of generatedTile.clone()).

Thank you very much, this took way too long to solve for such a simple solution.

No worries at all. This is how we all learn :slight_smile: