Getting 'Visual mesh geometry or vertices not initialized' error when adding soft body physics to object in Three.js scene

28 views Asked by At

I'm currently working on a project where I'm trying to add soft body physics to objects in a Three.js scene using ammo.js. However, I'm encountering an error that says "Visual mesh geometry or vertices not initialized" when I attempt to update the physics. I've checked my code, but I'm having trouble pinpointing the exact cause of the issue.

Here's a brief overview of my setup:

I have a Three.js scene with various objects, and I'm trying to add soft body physics to one of these objects. I'm using the ammo.js physics engine to handle the physics simulations. The error occurs when I'm updating the physics in the updatePhysics function, specifically within the section where I update the positions of the soft body nodes. I've already checked the following:

Ensured that the geometry and vertices of the visual mesh are properly initialized. Verified that the soft body mesh has the same number of nodes as the visual mesh geometry. Checked for any errors during the creation of the soft body mesh. Despite these checks, I'm still unable to resolve the issue. I suspect there might be something I'm overlooking or misunderstanding in my implementation.

Could anyone provide insights into what might be causing this error and how I can fix it? Any suggestions or guidance would be greatly appreciated. Thank you!

I get this error: city-stage.js:372 Visual mesh geometry or vertices not initialized.

this is where I am updating physics and the error occures within here:

/* U P D A T E   P H Y S I C S */
    
    function updatePhysics(deltaTime) {
        physicsWorld.stepSimulation(deltaTime, 10);
    
        for (let i = 0, il = rigidBodies.length; i < il; i++) {
            let objThree = rigidBodies[i];
            let objPhys = objThree.userData.physicsBody;
    
            if (objPhys.getMotionState) {
                let ms = objPhys.getMotionState();
                if (ms) {
                    ms.getWorldTransform(transformAux1);
                    let p = transformAux1.getOrigin();
                    let q = transformAux1.getRotation();
                    objThree.position.set(p.x(), p.y(), p.z());
                    objThree.quaternion.set(q.x(), q.y(), q.z(), q.w());
    
                    if (p.y() < -(boundaryDepth / 2)) {
                        objPhys.setLinearVelocity(new Ammo.btVector3(0, 0, 0));
                        objPhys.setAngularVelocity(new Ammo.btVector3(0, 0, 0));
    
                        transformAux1.setIdentity();
                        transformAux1.setOrigin(new Ammo.btVector3(0, 0, -10));
                        transformAux1.setRotation(new Ammo.btQuaternion(0, 0, 0, 1)); // Reset rotation if necessary
                        ms.setWorldTransform(transformAux1);
                        objPhys.setWorldTransform(transformAux1);
                    }
                }
            } else {
                // Update soft body positions
                let softBody = objPhys;
                let softBodyNodes = softBody.get_m_nodes();
                let numNodes = softBodyNodes.size();
    
                if (!objThree.geometry || !objThree.geometry.vertices || numNodes !== objThree.geometry.vertices.length) {
                    console.error("Mismatch in number of soft body nodes and vertices in visual mesh.");
                    continue; // Skip updating this soft body
                }
    
                for (let j = 0; j < numNodes; j++) {
                    let node = softBodyNodes.at(j);
                    if (!node) {
                        console.error("Undefined soft body node at index:", j);
                        continue; // Skip updating this node
                    }
                    let position = node.get_m_x();
                    objThree.geometry.vertices[j].set(position.x(), position.y(), position.z());
                }
                objThree.geometry.verticesNeedUpdate = true;
            }
        }
    }

here is my softbody code for reference.

/* S O F T B O D Y*/

function createSoftBody(mesh, geometry, mass, volumeStiffness, material) {
    let volumeSoftBody = new Ammo.btSoftBodyHelpers();
    let vertices = mesh.geometry.attributes.position.array;
    let indices = mesh.geometry.index.array;

    let softBody = volumeSoftBody.CreateFromTriMesh(
        physicsWorld.getWorldInfo(),
        vertices,
        indices,
        indices.length / 3,
        true
    );

    softBody.setTotalMass(mass, false);

    let sbConfig = softBody.get_m_cfg();
    sbConfig.set_viterations(40);
    sbConfig.set_piterations(40);

    let sbMaterial = softBody.get_m_materials().at(0);
    sbMaterial.set_m_kLST(volumeStiffness);
    sbMaterial.set_m_kAST(volumeStiffness);

    physicsWorld.addSoftBody(softBody, 1, -1);
    mesh.userData.physicsBody = softBody;

    scene.add(mesh);
}   

here is my object.

// Box 01
    let box = new THREE.Mesh(
        new THREE.BoxBufferGeometry(cubeSize, cubeSize, cubeSize),
        new THREE.MeshPhongMaterial({ color: 0xff0000 })
    );
    let boxShape = new Ammo.btBoxShape(new Ammo.btVector3(cubeSize / 2, cubeSize / 2, cubeSize / 2));
    boxShape.setMargin(margin); 
    pos.set(0, 0, 0);
    quat.set(3, 6, 1, 1);
    createRigidBody(box, boxShape, .4, pos, quat);
    box.userData.physicsBody.setFriction(0.5);
    box.userData.physicsBody.setRestitution(bounceRestitution);
    const boxMass = 0.5;
    const boxVolumeStiffness = 0.5;
    createSoftBody(box, box.geometry, boxMass, boxVolumeStiffness, box.material);   
0

There are 0 answers