Kaynağa Gözat

Fixing parenting problem with physics
Now children of children have the correct position when added to the compound

Raanan Weber 8 yıl önce
ebeveyn
işleme
8c5891c87c

+ 9 - 6
src/Physics/Plugins/babylon.cannonJSPlugin.ts

@@ -29,7 +29,7 @@
         }
         }
 
 
         public getTimeStep(): number {
         public getTimeStep(): number {
-          return this._fixedTimeStep;
+            return this._fixedTimeStep;
         }
         }
 
 
         public executeStep(delta: number, impostors: Array<PhysicsImpostor>): void {
         public executeStep(delta: number, impostors: Array<PhysicsImpostor>): void {
@@ -109,28 +109,31 @@
         }
         }
 
 
         private _processChildMeshes(mainImpostor: PhysicsImpostor) {
         private _processChildMeshes(mainImpostor: PhysicsImpostor) {
-            var meshChildren = mainImpostor.object.getChildMeshes ? mainImpostor.object.getChildMeshes() : [];
+            var meshChildren = mainImpostor.object.getChildMeshes ? mainImpostor.object.getChildMeshes(true) : [];
+            let currentRotation: Quaternion = mainImpostor.object.rotationQuaternion;
             if (meshChildren.length) {
             if (meshChildren.length) {
                 var processMesh = (localPosition: Vector3, mesh: AbstractMesh) => {
                 var processMesh = (localPosition: Vector3, mesh: AbstractMesh) => {
                     var childImpostor = mesh.getPhysicsImpostor();
                     var childImpostor = mesh.getPhysicsImpostor();
                     if (childImpostor) {
                     if (childImpostor) {
                         var parent = childImpostor.parent;
                         var parent = childImpostor.parent;
                         if (parent !== mainImpostor) {
                         if (parent !== mainImpostor) {
-                            var localPosition = mesh.position;
+                            var pPosition = mesh.getAbsolutePosition().subtract(mainImpostor.object.getAbsolutePosition());
+                            let localRotation = mesh.rotationQuaternion.multiply(Quaternion.Inverse(currentRotation));
                             if (childImpostor.physicsBody) {
                             if (childImpostor.physicsBody) {
                                 this.removePhysicsBody(childImpostor);
                                 this.removePhysicsBody(childImpostor);
                                 childImpostor.physicsBody = null;
                                 childImpostor.physicsBody = null;
                             }
                             }
                             childImpostor.parent = mainImpostor;
                             childImpostor.parent = mainImpostor;
                             childImpostor.resetUpdateFlags();
                             childImpostor.resetUpdateFlags();
-                            mainImpostor.physicsBody.addShape(this._createShape(childImpostor), new CANNON.Vec3(localPosition.x, localPosition.y, localPosition.z));
+                            mainImpostor.physicsBody.addShape(this._createShape(childImpostor), new CANNON.Vec3(pPosition.x, pPosition.y, pPosition.z), new CANNON.Quaternion(localRotation.x, localRotation.y, localRotation.z, localRotation.w));
                             //Add the mass of the children.
                             //Add the mass of the children.
                             mainImpostor.physicsBody.mass += childImpostor.getParam("mass");
                             mainImpostor.physicsBody.mass += childImpostor.getParam("mass");
                         }
                         }
                     }
                     }
-                    mesh.getChildMeshes().forEach(processMesh.bind(this, mesh.position));
+                    currentRotation.multiplyInPlace(mesh.rotationQuaternion);
+                    mesh.getChildMeshes(true).forEach(processMesh.bind(this, mesh.getAbsolutePosition()));
                 }
                 }
-                meshChildren.forEach(processMesh.bind(this, Vector3.Zero()));
+                meshChildren.forEach(processMesh.bind(this, mainImpostor.object.getAbsolutePosition()));
             }
             }
         }
         }
 
 

+ 31 - 30
src/Physics/babylon.physicsImpostor.ts

@@ -16,10 +16,11 @@ module BABYLON {
         getBoundingInfo?(): BoundingInfo;
         getBoundingInfo?(): BoundingInfo;
         computeWorldMatrix?(force: boolean): void;
         computeWorldMatrix?(force: boolean): void;
         getWorldMatrix?(): Matrix;
         getWorldMatrix?(): Matrix;
-        getChildMeshes?(): Array<AbstractMesh>;
+        getChildMeshes?(directDecendantsOnly?: boolean): Array<AbstractMesh>;
         getVerticesData?(kind: string): Array<number> | Float32Array;
         getVerticesData?(kind: string): Array<number> | Float32Array;
         getIndices?(): IndicesArray;
         getIndices?(): IndicesArray;
         getScene?(): Scene;
         getScene?(): Scene;
+        getAbsolutePosition(): Vector3;
     }
     }
 
 
     export class PhysicsImpostor {
     export class PhysicsImpostor {
@@ -49,31 +50,31 @@ module BABYLON {
         private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
         private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
         private static _tmpQuat: Quaternion = Quaternion.Identity();
         private static _tmpQuat: Quaternion = Quaternion.Identity();
 
 
-        get isDisposed():boolean{
+        get isDisposed(): boolean {
             return this._isDisposed;
             return this._isDisposed;
         }
         }
 
 
-        get mass():number{
+        get mass(): number {
             return this._physicsEngine.getPhysicsPlugin().getBodyMass(this);
             return this._physicsEngine.getPhysicsPlugin().getBodyMass(this);
         }
         }
 
 
-        set mass(value:number){
+        set mass(value: number) {
             this.setMass(value);
             this.setMass(value);
         }
         }
 
 
-        get friction():number{
+        get friction(): number {
             return this._physicsEngine.getPhysicsPlugin().getBodyFriction(this);
             return this._physicsEngine.getPhysicsPlugin().getBodyFriction(this);
         }
         }
 
 
-        set friction(value:number){
+        set friction(value: number) {
             this._physicsEngine.getPhysicsPlugin().setBodyFriction(this, value);
             this._physicsEngine.getPhysicsPlugin().setBodyFriction(this, value);
         }
         }
 
 
-        get restitution():number {
+        get restitution(): number {
             return this._physicsEngine.getPhysicsPlugin().getBodyRestitution(this);
             return this._physicsEngine.getPhysicsPlugin().getBodyRestitution(this);
         }
         }
 
 
-        set restitution(value:number){
+        set restitution(value: number) {
             this._physicsEngine.getPhysicsPlugin().setBodyRestitution(this, value);
             this._physicsEngine.getPhysicsPlugin().setBodyRestitution(this, value);
         }
         }
 
 
@@ -485,11 +486,11 @@ module BABYLON {
             this._deltaRotationConjugated = this._deltaRotation.conjugate();
             this._deltaRotationConjugated = this._deltaRotation.conjugate();
         }
         }
 
 
-        public getBoxSizeToRef(result:Vector3){
+        public getBoxSizeToRef(result: Vector3) {
             this._physicsEngine.getPhysicsPlugin().getBoxSizeToRef(this, result);
             this._physicsEngine.getPhysicsPlugin().getBoxSizeToRef(this, result);
         }
         }
 
 
-        public getRadius():number{
+        public getRadius(): number {
             return this._physicsEngine.getPhysicsPlugin().getRadius(this);
             return this._physicsEngine.getPhysicsPlugin().getRadius(this);
         }
         }
 
 
@@ -501,16 +502,16 @@ module BABYLON {
          * @param distToJoint Optional distance from the impostor to the joint.
          * @param distToJoint Optional distance from the impostor to the joint.
          * @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
          * @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
          */
          */
-        public syncBoneWithImpostor(bone:Bone, boneMesh:AbstractMesh, jointPivot:Vector3, distToJoint?:number, adjustRotation?:Quaternion){
+        public syncBoneWithImpostor(bone: Bone, boneMesh: AbstractMesh, jointPivot: Vector3, distToJoint?: number, adjustRotation?: Quaternion) {
 
 
             var tempVec = PhysicsImpostor._tmpVecs[0];
             var tempVec = PhysicsImpostor._tmpVecs[0];
             var mesh = <AbstractMesh>this.object;
             var mesh = <AbstractMesh>this.object;
 
 
-            if(adjustRotation){
+            if (adjustRotation) {
                 var tempQuat = PhysicsImpostor._tmpQuat;
                 var tempQuat = PhysicsImpostor._tmpQuat;
                 mesh.rotationQuaternion.multiplyToRef(adjustRotation, tempQuat);
                 mesh.rotationQuaternion.multiplyToRef(adjustRotation, tempQuat);
                 bone.setRotationQuaternion(tempQuat, Space.WORLD, boneMesh);
                 bone.setRotationQuaternion(tempQuat, Space.WORLD, boneMesh);
-            }else{
+            } else {
                 bone.setRotationQuaternion(mesh.rotationQuaternion, Space.WORLD, boneMesh);
                 bone.setRotationQuaternion(mesh.rotationQuaternion, Space.WORLD, boneMesh);
             }
             }
 
 
@@ -518,27 +519,27 @@ module BABYLON {
             tempVec.y = 0;
             tempVec.y = 0;
             tempVec.z = 0;
             tempVec.z = 0;
 
 
-            if(jointPivot){
+            if (jointPivot) {
                 tempVec.x = jointPivot.x;
                 tempVec.x = jointPivot.x;
                 tempVec.y = jointPivot.y;
                 tempVec.y = jointPivot.y;
                 tempVec.z = jointPivot.z;
                 tempVec.z = jointPivot.z;
 
 
                 bone.getDirectionToRef(tempVec, boneMesh, tempVec);
                 bone.getDirectionToRef(tempVec, boneMesh, tempVec);
 
 
-                if(distToJoint === undefined || distToJoint === null){
+                if (distToJoint === undefined || distToJoint === null) {
                     distToJoint = jointPivot.length();
                     distToJoint = jointPivot.length();
                 }
                 }
-                
+
                 tempVec.x *= distToJoint;
                 tempVec.x *= distToJoint;
                 tempVec.y *= distToJoint;
                 tempVec.y *= distToJoint;
                 tempVec.z *= distToJoint;
                 tempVec.z *= distToJoint;
             }
             }
 
 
-            if(bone.getParent()){
+            if (bone.getParent()) {
                 tempVec.addInPlace(mesh.getAbsolutePosition());
                 tempVec.addInPlace(mesh.getAbsolutePosition());
-                bone.setAbsolutePosition(tempVec, boneMesh);  
-            }else{
-                boneMesh.setAbsolutePosition(mesh.getAbsolutePosition());               
+                bone.setAbsolutePosition(tempVec, boneMesh);
+            } else {
+                boneMesh.setAbsolutePosition(mesh.getAbsolutePosition());
                 boneMesh.position.x -= tempVec.x;
                 boneMesh.position.x -= tempVec.x;
                 boneMesh.position.y -= tempVec.y;
                 boneMesh.position.y -= tempVec.y;
                 boneMesh.position.z -= tempVec.z;
                 boneMesh.position.z -= tempVec.z;
@@ -555,43 +556,43 @@ module BABYLON {
          * @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
          * @param adjustRotation Optional quaternion for adjusting the local rotation of the bone.
          * @param boneAxis Optional vector3 axis the bone is aligned with
          * @param boneAxis Optional vector3 axis the bone is aligned with
          */
          */
-        public syncImpostorWithBone(bone:Bone, boneMesh:AbstractMesh, jointPivot:Vector3, distToJoint?:number, adjustRotation?:Quaternion, boneAxis?:Vector3){
+        public syncImpostorWithBone(bone: Bone, boneMesh: AbstractMesh, jointPivot: Vector3, distToJoint?: number, adjustRotation?: Quaternion, boneAxis?: Vector3) {
 
 
             var mesh = <AbstractMesh>this.object;
             var mesh = <AbstractMesh>this.object;
 
 
-            if(adjustRotation){
+            if (adjustRotation) {
                 var tempQuat = PhysicsImpostor._tmpQuat;
                 var tempQuat = PhysicsImpostor._tmpQuat;
                 bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, tempQuat);
                 bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, tempQuat);
                 tempQuat.multiplyToRef(adjustRotation, mesh.rotationQuaternion);
                 tempQuat.multiplyToRef(adjustRotation, mesh.rotationQuaternion);
-            }else{
+            } else {
                 bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, mesh.rotationQuaternion);
                 bone.getRotationQuaternionToRef(Space.WORLD, boneMesh, mesh.rotationQuaternion);
             }
             }
 
 
             var pos = PhysicsImpostor._tmpVecs[0];
             var pos = PhysicsImpostor._tmpVecs[0];
             var boneDir = PhysicsImpostor._tmpVecs[1];
             var boneDir = PhysicsImpostor._tmpVecs[1];
-            
-            if(!boneAxis){
+
+            if (!boneAxis) {
                 boneAxis = PhysicsImpostor._tmpVecs[2];
                 boneAxis = PhysicsImpostor._tmpVecs[2];
-                boneAxis.x = 0; 
-                boneAxis.y = 1; 
+                boneAxis.x = 0;
+                boneAxis.y = 1;
                 boneAxis.z = 0;
                 boneAxis.z = 0;
             }
             }
 
 
             bone.getDirectionToRef(boneAxis, boneMesh, boneDir);
             bone.getDirectionToRef(boneAxis, boneMesh, boneDir);
             bone.getAbsolutePositionToRef(boneMesh, pos);
             bone.getAbsolutePositionToRef(boneMesh, pos);
 
 
-            if((distToJoint === undefined || distToJoint === null) && jointPivot){
+            if ((distToJoint === undefined || distToJoint === null) && jointPivot) {
                 distToJoint = jointPivot.length();
                 distToJoint = jointPivot.length();
             }
             }
 
 
-            if(distToJoint !== undefined && distToJoint !== null){
+            if (distToJoint !== undefined && distToJoint !== null) {
                 pos.x += boneDir.x * distToJoint;
                 pos.x += boneDir.x * distToJoint;
                 pos.y += boneDir.y * distToJoint;
                 pos.y += boneDir.y * distToJoint;
                 pos.z += boneDir.z * distToJoint;
                 pos.z += boneDir.z * distToJoint;
             }
             }
 
 
             mesh.setAbsolutePosition(pos);
             mesh.setAbsolutePosition(pos);
-            
+
         }
         }
 
 
         //Impostor types
         //Impostor types