Browse Source

if mesh has no children with impostors dont create compound shape

Trevor Baron 6 years ago
parent
commit
7cbd08b2de
1 changed files with 27 additions and 21 deletions
  1. 27 21
      src/Physics/Plugins/babylon.ammoJSPlugin.ts

+ 27 - 21
src/Physics/Plugins/babylon.ammoJSPlugin.ts

@@ -379,28 +379,30 @@ module BABYLON {
 
 
             if (!ignoreChildren) {
             if (!ignoreChildren) {
                 var meshChildren = impostor.object.getChildMeshes ? impostor.object.getChildMeshes(true) : [];
                 var meshChildren = impostor.object.getChildMeshes ? impostor.object.getChildMeshes(true) : [];
-                if (meshChildren.length > 0) {
-                    returnValue = new Ammo.btCompoundShape();
-
-                    // Add shape of all children to the compound shape
-                    meshChildren.forEach((childMesh) => {
-                        var childImpostor = childMesh.getPhysicsImpostor();
-                        if (childImpostor) {
-                            var shape = this._createShape(childImpostor);
-
-                            // Position needs to be scaled based on parent's scaling
-                            var parentMat = childMesh.parent!.getWorldMatrix().clone();
-                            var s = new BABYLON.Vector3();
-                            parentMat.decompose(s);
-                            this._tmpAmmoTransform.getOrigin().setValue(childMesh.position.x * s.x, childMesh.position.y * s.y, childMesh.position.z * s.z);
-
-                            this._tmpAmmoQuaternion.setValue(childMesh.rotationQuaternion!.x, childMesh.rotationQuaternion!.y, childMesh.rotationQuaternion!.z, childMesh.rotationQuaternion!.w);
-                            this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
-                            returnValue.addChildShape(this._tmpAmmoTransform, shape);
-                            childImpostor.dispose();
-                        }
-                    });
+                returnValue = new Ammo.btCompoundShape();
+
+                // Add shape of all children to the compound shape
+                var childrenAdded = 0;
+                meshChildren.forEach((childMesh) => {
+                    var childImpostor = childMesh.getPhysicsImpostor();
+                    if (childImpostor) {
+                        var shape = this._createShape(childImpostor);
+
+                        // Position needs to be scaled based on parent's scaling
+                        var parentMat = childMesh.parent!.getWorldMatrix().clone();
+                        var s = new BABYLON.Vector3();
+                        parentMat.decompose(s);
+                        this._tmpAmmoTransform.getOrigin().setValue(childMesh.position.x * s.x, childMesh.position.y * s.y, childMesh.position.z * s.z);
+
+                        this._tmpAmmoQuaternion.setValue(childMesh.rotationQuaternion!.x, childMesh.rotationQuaternion!.y, childMesh.rotationQuaternion!.z, childMesh.rotationQuaternion!.w);
+                        this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
+                        returnValue.addChildShape(this._tmpAmmoTransform, shape);
+                        childImpostor.dispose();
+                        childrenAdded++;
+                    }
+                });
 
 
+                if(childrenAdded > 0){
                     // Add parents shape as a child if present
                     // Add parents shape as a child if present
                     var shape = this._createShape(impostor, true);
                     var shape = this._createShape(impostor, true);
                     if (shape) {
                     if (shape) {
@@ -412,6 +414,10 @@ module BABYLON {
                     }
                     }
 
 
                     return returnValue;
                     return returnValue;
+                }else{
+                    // If no children with impostors create the actual shape below instead
+                    Ammo.destroy(returnValue);
+                    returnValue = null;
                 }
                 }
             }
             }