浏览代码

Set body mass

Now possible to set the body's mass without recreating the physics body
object.
Raanan Weber 9 年之前
父节点
当前提交
dbd1dec3c7

+ 5 - 0
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -434,6 +434,11 @@
         public setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
             impostor.physicsBody.angularVelocity.copy(velocity);
         }
+        
+        public setBodyMass(impostor: PhysicsImpostor, mass: number) {
+            impostor.physicsBody.mass = mass;
+            impostor.physicsBody.updateMassProperties();
+        }
 
         public sleepBody(impostor: PhysicsImpostor) {
             impostor.physicsBody.sleep();

+ 9 - 0
src/Physics/Plugins/babylon.oimoJSPlugin.ts

@@ -323,6 +323,15 @@ module BABYLON {
         public setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
             impostor.physicsBody.angularVelocity.init(velocity.x, velocity.y, velocity.z);
         }
+        
+        
+        public setBodyMass(impostor: PhysicsImpostor, mass: number) {
+            var staticBody: boolean = mass === 0;
+            //this will actually set the body's density and not its mass.
+            //But this is how oimo treats the mass variable.
+            impostor.physicsBody.shapes.density = staticBody ? 1 : mass ;
+            impostor.physicsBody.setupMass(staticBody ? 0x2 : 0x1);
+        }
 
         public sleepBody(impostor: PhysicsImpostor) {
             impostor.physicsBody.sleep();

+ 1 - 0
src/Physics/babylon.physicsEngine.ts

@@ -163,6 +163,7 @@
         setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion);
         setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3);
         setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3);
+        setBodyMass(impostor: PhysicsImpostor, mass: number);
         sleepBody(impostor: PhysicsImpostor);
         wakeUpBody(impostor: PhysicsImpostor);
         dispose();

+ 10 - 0
src/Physics/babylon.physicsImpostor.ts

@@ -132,6 +132,16 @@ module BABYLON {
             this._options[paramName] = value;
             this._bodyUpdateRequired = true;
         }
+        
+        /**
+         * Specifically change the body's mass option. Won't recreate the physics body object
+         */
+        public setMass(mass: number) {
+            if(this.getParam("mass") !== mass) {
+                this.setParam("mass", mass);
+            }
+            this._physicsEngine.getPhysicsPlugin().setBodyMass(this, mass);
+        }
 
         /**
          * Set the body's linear velocity.