浏览代码

Merge pull request #5978 from TrevorDev/ammoConvexMeshRotation

fix rotation for meshes and fallback to convexMesh when mesh impostor…
David Catuhe 6 年之前
父节点
当前提交
09fb58af5f
共有 1 个文件被更改,包括 23 次插入12 次删除
  1. 23 12
      src/Physics/Plugins/ammoJSPlugin.ts

+ 23 - 12
src/Physics/Plugins/ammoJSPlugin.ts

@@ -497,8 +497,11 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
                 var triPoints = [];
                 var triPoints = [];
                 for (var point = 0; point < 3; point++) {
                 for (var point = 0; point < 3; point++) {
                     var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
                     var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
-                    v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
-                    v.subtractInPlace(topLevelObject.position);
+
+                    // Adjust for initial scaling
+                    Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
+                    v = Vector3.TransformCoordinates(v, this._tmpMatrix);
+
                     var vec: any;
                     var vec: any;
                     if (point == 0) {
                     if (point == 0) {
                         vec = this._tmpAmmoVectorA;
                         vec = this._tmpAmmoVectorA;
@@ -693,8 +696,11 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
                 var triPoints = [];
                 var triPoints = [];
                 for (var point = 0; point < 3; point++) {
                 for (var point = 0; point < 3; point++) {
                     var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
                     var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
-                    v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
-                    v.subtractInPlace(topLevelObject.position);
+
+                    // Adjust for initial scaling
+                    Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
+                    v = Vector3.TransformCoordinates(v, this._tmpMatrix);
+
                     var vec: any;
                     var vec: any;
                     if (point == 0) {
                     if (point == 0) {
                         vec = this._tmpAmmoVectorA;
                         vec = this._tmpAmmoVectorA;
@@ -785,15 +791,20 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
                 returnValue = new Ammo.btBoxShape(this._tmpAmmoVectorA);
                 returnValue = new Ammo.btBoxShape(this._tmpAmmoVectorA);
                 break;
                 break;
             case PhysicsImpostor.MeshImpostor:
             case PhysicsImpostor.MeshImpostor:
-                var tetraMesh = new Ammo.btTriangleMesh();
-                impostor._pluginData.toDispose.concat([tetraMesh]);
-                var triangeCount = this._addMeshVerts(tetraMesh, object, object);
-                if (triangeCount == 0) {
-                    returnValue = new Ammo.btCompoundShape();
-                } else {
-                    returnValue = new Ammo.btBvhTriangleMeshShape(tetraMesh);
+                if (impostor.getParam("mass") == 0) {
+                    // Only create btBvhTriangleMeshShape impostor is static
+                    // See https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=7283
+                    var tetraMesh = new Ammo.btTriangleMesh();
+                    impostor._pluginData.toDispose.concat([tetraMesh]);
+                    var triangeCount = this._addMeshVerts(tetraMesh, object, object);
+                    if (triangeCount == 0) {
+                        returnValue = new Ammo.btCompoundShape();
+                    } else {
+                        returnValue = new Ammo.btBvhTriangleMeshShape(tetraMesh);
+                    }
+                    break;
                 }
                 }
-                break;
+                // Otherwise create convexHullImpostor
             case PhysicsImpostor.ConvexHullImpostor:
             case PhysicsImpostor.ConvexHullImpostor:
                 var convexMesh = new Ammo.btConvexHullShape();
                 var convexMesh = new Ammo.btConvexHullShape();
                 var triangeCount = this._addHullVerts(convexMesh, object, object);
                 var triangeCount = this._addHullVerts(convexMesh, object, object);