javascriptthree.jsphysijs

Physijs update object mass


I am trying to work with a three.js environment with multiple objects that change mass when they are clicked on. Unfortunately the documentation hasn't been helpful and I haven't been able to find any examples of the best way to do this.

Here is the portion in which I am adding a random number of objects to the scene.

    var random = getRandomArbitrary(4, 50);
    for (var i = 0; i <= random; i++) {
      var geometry = new THREE.IcosahedronGeometry( 5, 0 );

      var physijsMaterial = Physijs.createMaterial(
        new THREE.MeshLambertMaterial( { color: 0xffffff, emissive: 0x333333, shading: THREE.FlatShading } ),
        0, // high friction
        0 // medium restitution /bouciness
      );
      var smallSphere = new Physijs.SphereMesh(
        geometry,
        physijsMaterial,
        0
      );

      smallSphere.position.y = getRandomArbitrary(-50, 50);
      smallSphere.position.z = getRandomArbitrary(-50, 50);
      smallSphere.position.x = getRandomArbitrary(-50, 50);
      smallSphere.name = "Crystals";
      scene.add(smallSphere); 
    }

Here is the portion in which I am trying to update the mass of an object.

function generateGravity(event)
  {
    event.preventDefault();

    var vector = new THREE.Vector3();
    vector.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
    vector.unproject( camera );

    raycaster.ray.set( camera.position, vector.sub( camera.position ).normalize() );

    var intersects = raycaster.intersectObjects( scene.children, true );

    console.log(intersects);

    // intersects.object

    length = intersects.length;
    for (var x = 0; x < length; x++) {
      if (intersects[x].object.name == "Crystals") {
        console.log(intersects[x].object.mass);
        intersects[x].object._physijs.mass = 50;
        // intersects[x].object.remove();
      }
    }
  }

What I'm curious to know is the best approach to updating the mass of an object during an event. Right now this function generateGravity is being called in the "render" method.


Solution

  • I realized this was happening because I placed scene.simulate() after render().