Browse Source

Merge pull request #5729 from TrevorDev/ammoFixes

init ammo correctly, applying impulse/force should be in world space
David Catuhe 6 years ago
parent
commit
d1bbe3a8f1
2 changed files with 25 additions and 4 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 24 4
      src/Physics/Plugins/ammoJSPlugin.ts

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

@@ -144,6 +144,7 @@
 - CannonJS ignores connectedPivot joint parameter ([TrevorDev](https://github.com/TrevorDev))
 - Fix case sensitive paths ([mrdunk](https://github.com))
 - Attaching a BoundingBoxGizmo on a child should not remove its parent ([TrevorDev](https://github.com/TrevorDev)))
+- AmmoJS fix include issue caused after modules update and use world contact point to be consistent with oimo and cannon ([TrevorDev](https://github.com/TrevorDev)))
 
 ### Core Engine
 - Fixed a bug with `mesh.alwaysSelectAsActiveMesh` preventing layerMask to be taken in account ([Deltakosh](https://github.com/deltakosh))

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

@@ -1,4 +1,4 @@
-import { Quaternion, Vector3 } from "../../Maths/math";
+import { Quaternion, Vector3, Matrix } from "../../Maths/math";
 import { IPhysicsEnginePlugin, PhysicsImpostorJoint } from "../../Physics/IPhysicsEngine";
 import { Logger } from "../../Misc/logger";
 import { PhysicsImpostor, IPhysicsEnabledObject } from "../../Physics/physicsImpostor";
@@ -18,7 +18,7 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
     /**
      * Reference to the Ammo library
      */
-    public bjsAMMO: any;
+    public bjsAMMO: any = {};
     /**
      * Created ammoJS world which physics bodies are added to
      */
@@ -55,9 +55,11 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
      */
     public constructor(private _useDeltaForWorldStep: boolean = true, ammoInjection: any = Ammo) {
         if (typeof ammoInjection === "function") {
-            ammoInjection();
+            ammoInjection(this.bjsAMMO);
+        }else {
+            this.bjsAMMO = ammoInjection;
         }
-        this.bjsAMMO = ammoInjection;
+
         if (!this.isSupported()) {
             Logger.Error("AmmoJS is not available. Please make sure you included the js file.");
             return;
@@ -197,6 +199,8 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
         }
     }
 
+    private _tmpVector = new Vector3();
+    private _tmpMatrix = new Matrix();
     /**
      * Applies an implulse on the imposter
      * @param impostor imposter to apply impulse
@@ -206,6 +210,14 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
     public applyImpulse(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
         var worldPoint = this._tmpAmmoVectorA;
         var impulse = this._tmpAmmoVectorB;
+
+        // Convert contactPoint into world space
+        if (impostor.object && impostor.object.getWorldMatrix) {
+            impostor.object.getWorldMatrix().invertToRef(this._tmpMatrix);
+            Vector3.TransformCoordinatesToRef(contactPoint, this._tmpMatrix, this._tmpVector);
+            contactPoint = this._tmpVector;
+        }
+
         worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
         impulse.setValue(force.x, force.y, force.z);
 
@@ -221,6 +233,14 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
     public applyForce(impostor: PhysicsImpostor, force: Vector3, contactPoint: Vector3) {
         var worldPoint = this._tmpAmmoVectorA;
         var impulse = this._tmpAmmoVectorB;
+
+        // Convert contactPoint into world space
+        if (impostor.object && impostor.object.getWorldMatrix) {
+            impostor.object.getWorldMatrix().invertToRef(this._tmpMatrix);
+            Vector3.TransformCoordinatesToRef(contactPoint, this._tmpMatrix, this._tmpVector);
+            contactPoint = this._tmpVector;
+        }
+
         worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
         impulse.setValue(force.x, force.y, force.z);