Kenney's Crate and Pivot Point

I try to translate the next Godot tutorial example to Babylon.js: Importing 3D Objects The author uses kenney_platformerkit2 But there is a problem:

However, there’s one small problem: it seems Kenney exported this model with an offset:

It would much better if the crate were centered relative to the parent node, so that when the RigidBody rotates about its center, so will the mesh. To fix this, select the MeshInstance node and set its Translation property to (-0.5, -0.25, 0.5) .

I want to make the same with Babylon.js. I found in the documentation: Pivots

I can set a pivot point like this:

crate.setPivotPoint(new BABYLON.Vector3(-0.5, -0.5, -0.5));

or like this:

crate.setPivotMatrix(BABYLON.Matrix.Translation(-0.5, -0.5, -0.5))

But both set only X and ignore Y and Z

image

The gray box is in the center.

This is my sandbox example: Crate and Pivot Point

        const box = BABYLON.MeshBuilder.CreateBox("box", { width: 0.5, height: 0.5, depth: 0.5 }, scene);

        async function load()
        {
            const result = await BABYLON.SceneLoader.ImportMeshAsync(null, "models/", "crate.glb", scene);
            const crate = result.meshes[0];
            crate.setPivotPoint(new BABYLON.Vector3(-0.5, -0.5, -0.5));
            // crate.setPivotMatrix(BABYLON.Matrix.Translation(-0.5, -0.5, -0.5))
            console.log(crate.getAbsolutePivotPoint());
        }

        load();

It is actually moving it along all axis as you can see if you try without pivot:

2 Likes
crate.setPivotPoint(new BABYLON.Vector3(0, 1, 0));

Why does nothing change?

I want to set the crate to the center. How to make it?

Looking more into it, in your case why not simply changing the position of the crate cause you already have a hierachy ???

image

I want to use the crate with physics engine. I think that a collider will be on another position.

You can move it like this

const crate = scene.getTransformNodeByID(“crate”);
crate.position.x = 0;
crate.position.y = -0.25;
crate.position.z = 0;

and then create the physicImpostor on the root, as I think you can not create an impostor outside root objects. Adding @Cedric and @RaananW to confirm ?

It is easier to change the origin of an object in Blender.

When I’m having trouble using setPivotPoint, I usually end up setting the position and calling mesh.bakeCurrentTransformIntoVertices() instead, to change its center point… :slight_smile:

2 Likes

I do it like @Blake when I’m having issues :slight_smile:
Note, I don’t think you’ll need to center the box around (0,0,0) before using a physics engine. I did changes for that some weeks ago and it should work out of the box (pun intended).

4 Likes

Hi @8Observer8 just checking in to see if you still have questions about this :slight_smile:

2 Likes

Thank you. This problem was solved by setting “Origin Point” in Blender.

2 Likes