瀏覽代碼

Merge pull request #3016 from RaananW/impostor-no-parent

Impostors for child meshes without parent impostor
David Catuhe 7 年之前
父節點
當前提交
4a4a0b35b9
共有 1 個文件被更改,包括 26 次插入10 次删除
  1. 26 10
      src/Physics/babylon.physicsImpostor.ts

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

@@ -5,6 +5,8 @@ module BABYLON {
         friction?: number;
         restitution?: number;
         nativeOptions?: any;
+        ignoreParent?: boolean;
+        disableBidirectionalTransformation?: boolean;
     }
 
     export interface IPhysicsEnabledObject {
@@ -80,7 +82,7 @@ module BABYLON {
         set restitution(value: number) {
             if (!this._physicsEngine) {
                 return;
-            }            
+            }
             this._physicsEngine.getPhysicsPlugin().setBodyRestitution(this, value);
         }
 
@@ -126,9 +128,10 @@ module BABYLON {
                 this._options.mass = (_options.mass === void 0) ? 0 : _options.mass
                 this._options.friction = (_options.friction === void 0) ? 0.2 : _options.friction
                 this._options.restitution = (_options.restitution === void 0) ? 0.2 : _options.restitution
+
                 this._joints = [];
                 //If the mesh has a parent, don't initialize the physicsBody. Instead wait for the parent to do that.
-                if (!this.object.parent) {
+                if (!this.object.parent || this._options.ignoreParent) {
                     this._init();
                 } else if (this.object.parent.physicsImpostor) {
                     Tools.Warn("You must affect impostors to children before affecting impostor to parent.");
@@ -150,7 +153,7 @@ module BABYLON {
             this._physicsEngine.removeImpostor(this);
             this.physicsBody = null;
             this._parent = this._parent || this._getPhysicsParent();
-            if (!this.parent) {
+            if (!this.parent || this._options.ignoreParent) {
                 this._physicsEngine.addImpostor(this);
             }
         }
@@ -180,7 +183,7 @@ module BABYLON {
          */
         public forceUpdate() {
             this._init();
-            if (this.parent) {
+            if (this.parent && !this._options.ignoreParent) {
                 this.parent.forceUpdate();
             }
         }
@@ -193,11 +196,11 @@ module BABYLON {
          * Gets the body that holds this impostor. Either its own, or its parent.
          */
         public get physicsBody(): any {
-            return this._parent ? this._parent.physicsBody : this._physicsBody;
+            return (this._parent && !this._options.ignoreParent) ? this._parent.physicsBody : this._physicsBody;
         }
 
         public get parent(): PhysicsImpostor {
-            return this._parent;
+            return !this._options.ignoreParent && this._parent;
         }
 
         public set parent(value: PhysicsImpostor) {
@@ -377,7 +380,13 @@ module BABYLON {
             if (!this._physicsEngine) {
                 return;
             }
-            this.object.position.subtractToRef(this._deltaPosition, this._tmpPositionWithDelta);
+
+            if (this._options.ignoreParent && this.object.parent) {
+                this._tmpPositionWithDelta.copyFrom(this.object.getAbsolutePosition());
+                //this.object.getAbsolutePosition().subtractToRef(this._deltaPosition, this._tmpPositionWithDelta);
+            } else {
+                this.object.position.subtractToRef(this._deltaPosition, this._tmpPositionWithDelta);
+            }
             //conjugate deltaRotation
             if (this.object.rotationQuaternion) {
                 if (this._deltaRotationConjugated) {
@@ -386,8 +395,10 @@ module BABYLON {
                     this._tmpRotationWithDelta.copyFrom(this.object.rotationQuaternion);
                 }
             }
-
-            this._physicsEngine.getPhysicsPlugin().setPhysicsBodyTransformation(this, this._tmpPositionWithDelta, this._tmpRotationWithDelta);
+            // Only if not disabled
+            if (!this._options.disableBidirectionalTransformation) {
+                this._physicsEngine.getPhysicsPlugin().setPhysicsBodyTransformation(this, this._tmpPositionWithDelta, this._tmpRotationWithDelta);
+            }
 
             this._onBeforePhysicsStepCallbacks.forEach((func) => {
                 func(this);
@@ -408,7 +419,12 @@ module BABYLON {
 
             this._physicsEngine.getPhysicsPlugin().setTransformationFromPhysicsBody(this);
 
-            this.object.position.addInPlace(this._deltaPosition)
+            if (this._options.ignoreParent && this.object.parent) {
+                this.object.position.subtractInPlace(this.object.parent.getAbsolutePosition());
+            } else {
+                this.object.position.addInPlace(this._deltaPosition)
+            }
+
             if (this._deltaRotation && this.object.rotationQuaternion) {
                 this.object.rotationQuaternion.multiplyInPlace(this._deltaRotation);
             }