Problem with rotation of clones

Hello folks,

I didn’ t want to create a thread for this issue but I can’t solve it. I don’t understand why I am not able to rotate the cloned meshes. In playground it works just fine but in my code it doesn’t.

My model consists of several identical wood beams which are build up to a box. I created a function to clone these wood beams dynamically if the size of the box changes. To achieve a more realistic appearance I try to rotate the wood beams randomly around the own axis. Unfortunately rotation isn’t working at all. Nothing happens.

function adjustWidth(type, desiredSize) {
    if (desiredSize >= min && desiredSize <= max) {
        const preAdjustBeams_2Length = getObjectSize(Beams_2).x
        const currentParentSize = getObjectSize(parent).x
        const Beams_1Scaling = Beams_1.absoluteScaling.x

        if (desiredSize !== Math.round(currentParentSize)) {
            const beam = type === "length" ? Scene3d.c.lengthBeamBase : Scene3d.c.widthBeamBase
            const currentBeamLength = getObjectSize(beam).x
            const currentBeamCount = Math.floor(currentParentSize / currentBeamLength)
            var nextBeamCount = currentBeamCount + 1
            const desiredBeamLength = 45
            const desiredBeamCount = Math.ceil(desiredSize / desiredBeamLength)

            for (nextBeamCount; nextBeamCount < desiredBeamCount + 1; nextBeamCount++) {
                Beams_4.map(Beam => {
                    const beamClone = Beam.clone()
                    beamClone.position.x -= currentBeamLength * (nextBeamCount - 1) / Beams_1Scaling
                    beamClone.rotation.x = Math.PI/2 * Math.round(Math.random() * 4)
                    return true
                })
            }
            
            const lengthBeams_1Length = getObjectSize(Beams_1).x
            Beams_1.scaling.x = (desiredSize - beamWidth) / lengthBeams_1Length * Beams_1Scaling
            Beams_2.scaling.x = (desiredSize - beamWidth) / lengthBeams_1Length * Beams_1Scaling

            Scene3d.c.lengthBeams_3.map(Beam => {
                Beam.position.x -= getObjectSize(Beams_2).x - preAdjustBeams_2Length
                return true
            })

            const bottomLength = getObjectSize(bottom).x
            bottom.scaling.x = (desiredSize / bottomLength) * bottom.absoluteScaling.x
        }
    }
}

if you have any idea how I can solve this, I would appreciate it very much.

Thank you!

Hi samevision,

If the beam has its rotationQuaternion set, the rotation property gets ignored, so to make it work then you can set it to null.

Hi Gijs,

just setting rotationQuaternion to null broke everything. I had to set “beamClone.rotation.y = Math.PI” as well to get the previous state of my model.

Unfortunately …
beamClone.rotation.z = Math.PI/2 //* Math.round(Math.random() * 4)
… doesn’t work as expected. The beams are not rotating around their local axis. I will try to fix that. If you can give me a hint please let me know.

@samevision
What I said would just discard the quaternion rotation, so what I think you want is:

beamClone.rotate(BABYLON.Axis.X, Math.PI/2 * Math.round(Math.random() * 4), BABYLON.Space.LOCAL);

Thank you. After readjusting the local origins of each mesh it works perfectly.

1 Like