浏览代码

Sleep and wake up for physics impostor

Raanan Weber 9 年之前
父节点
当前提交
45fd1853a6

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

@@ -411,6 +411,14 @@
         public setVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
             impostor.physicsBody.velocity.copy(velocity);
         }
+        
+        public sleepBody(impostor: PhysicsImpostor) {
+            impostor.physicsBody.sleep();    
+        }
+        
+        public wakeUpBody(impostor: PhysicsImpostor) {
+            impostor.physicsBody.wakeUp(); 
+        }
 
         public dispose() {
             //nothing to do, actually.

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

@@ -309,6 +309,14 @@ module BABYLON {
         public setVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
             impostor.physicsBody.linearVelocity.init(velocity.x, velocity.y, velocity.z);
         }
+        
+        public sleepBody(impostor: PhysicsImpostor) {
+            impostor.physicsBody.sleep();    
+        }
+        
+        public wakeUpBody(impostor: PhysicsImpostor) {
+            impostor.physicsBody.awake(); 
+        }
 
         public dispose() {
             this.world.clear();

+ 6 - 4
src/Physics/babylon.physicsEngine.ts

@@ -61,7 +61,7 @@
         public addImpostor(impostor: PhysicsImpostor) {
             this._impostors.push(impostor);
             //if no parent, generate the body
-            if(!impostor.parent) {
+            if (!impostor.parent) {
                 this._physicsPlugin.generatePhysicsBody(impostor);
             }
         }
@@ -71,7 +71,7 @@
             if (index > -1) {
                 var removed = this._impostors.splice(index, 1);
                 //Is it needed?
-                if(removed.length) {
+                if (removed.length) {
                     //this will also remove it from the world.
                     removed[0].physicsBody = null;
                 }
@@ -94,7 +94,7 @@
                     && impostorJoint.joint === joint
                     && impostorJoint.mainImpostor === mainImpostor)
             });
-            if(matchingJoints.length) {
+            if (matchingJoints.length) {
                 this._physicsPlugin.removeJoint(matchingJoints[0]);
                 //TODO remove it from the list as well
                 
@@ -148,8 +148,10 @@
         removeJoint(joint: PhysicsImpostorJoint)
         isSupported(): boolean;
         setTransformationFromPhysicsBody(impostor: PhysicsImpostor);
-        setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition:Vector3, newRotation: Quaternion);
+        setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion);
         setVelocity(impostor: PhysicsImpostor, velocity: Vector3);
+        sleepBody(impostor: PhysicsImpostor);
+        wakeUpBody(impostor: PhysicsImpostor);
         dispose();
     }
 }

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

@@ -276,6 +276,20 @@ module BABYLON {
             })
             this._physicsEngine.addJoint(this, otherImpostor, joint);
         }
+        
+        /**
+         * Will keep this body still, in a sleep mode.
+         */
+        public sleep() {
+            this._physicsEngine.getPhysicsPlugin().sleepBody(this);
+        }
+        
+        /**
+         * Wake the body up.
+         */
+        public wakeUp() {
+            this._physicsEngine.getPhysicsPlugin().wakeUpBody(this);
+        }
 
         public dispose(disposeChildren: boolean = true) {
             this.physicsBody = null;