Browse Source

Merge pull request #7196 from MackeyK24/master

AmmoJS - Custom Impostor Shapes
David Catuhe 5 years ago
parent
commit
312562cc72

+ 1 - 0
dist/preview release/what's new.md

@@ -102,6 +102,7 @@
 - Update Ammo.js library to support global collision contact callbacks ([MackeyK24](https://github.com/MackeyK24/))
 - Update Ammo.js library to allow native capsule shape impostors ([MackeyK24](https://github.com/MackeyK24/))
 - Update Ammo.js library to allow your own broadphase overlapping pair cache ([MackeyK24](https://github.com/MackeyK24/))
+- Update Ammo.js library for custom impostor shapes. PhysicsImpostor.CustomImposter type and AmmoJSPlugin.OnCreateCustomShape factoty function ([MackeyK24](https://github.com/MackeyK24/))
 - Update Ammo.js library and AmmoJS plugin to support ellipsoid ([CedricGuillemet](https://github.com/CedricGuillemet/))
 
 ### Loaders

+ 24 - 0
src/Physics/Plugins/ammoJSPlugin.ts

@@ -143,6 +143,11 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
         return this._timeStep;
     }
 
+    /**
+     * The create custom shape handler function to be called when using BABYLON.PhysicsImposter.CustomImpostor
+     */
+    public onCreateCustomShape: (impostor: PhysicsImpostor) => any;
+
     // Ammo's contactTest and contactPairTest take a callback that runs synchronously, wrap them so that they are easier to consume
     private _isImpostorInContact(impostor: PhysicsImpostor) {
         this._tmpContactCallbackResult = false;
@@ -802,6 +807,21 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
         return ropeBody;
     }
 
+    /**
+     * Create a custom physics impostor shape using the plugin's onCreateCustomShape handler
+     * @param impostor to create the custom physics shape for
+     */
+    private _createCustom(impostor: PhysicsImpostor): any {
+        let returnValue: any = null;
+        if (this.onCreateCustomShape) {
+            returnValue = this.onCreateCustomShape(impostor);
+        }
+        if (returnValue == null) {
+            returnValue = new Ammo.btCompoundShape();
+        }
+        return returnValue;
+    }
+
     // adds all verticies (including child verticies) to the convex hull shape
     private _addHullVerts(btConvexHullShape: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
         var triangleCount = 0;
@@ -959,6 +979,10 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
                 // Fill with sphere but collision is disabled on the rigid body in generatePhysicsBody, using an empty shape caused unexpected movement with joints
                 returnValue = new Ammo.btSphereShape(extendSize.x / 2);
                 break;
+            case PhysicsImpostor.CustomImpostor:
+                // Only usable when the plugin's onCreateCustomShape is set
+                returnValue = this._createCustom(impostor);
+                break;
             case PhysicsImpostor.SoftbodyImpostor:
                 // Only usable with a mesh that has sufficient and shared vertices
                 returnValue = this._createSoftbody(impostor);

+ 4 - 0
src/Physics/physicsImpostor.ts

@@ -1263,6 +1263,10 @@ export class PhysicsImpostor {
      */
     public static ConvexHullImpostor = 10;
     /**
+     * Custom-Imposter type (Ammo.js plugin only)
+     */
+    public static CustomImpostor = 100;
+    /**
      * Rope-Imposter type
      */
     public static RopeImpostor = 101;