소스 검색

Merge pull request #7431 from RaananW/physics-compound

Extend size and position should be in local space for the physics engin(s)
David Catuhe 5 년 전
부모
커밋
f35c36befb
4개의 변경된 파일28개의 추가작업 그리고 25개의 파일을 삭제
  1. 1 0
      dist/preview release/what's new.md
  2. 1 13
      src/Physics/Plugins/cannonJSPlugin.ts
  3. 25 11
      src/Physics/Plugins/oimoJSPlugin.ts
  4. 1 1
      src/Physics/physicsImpostor.ts

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

@@ -292,6 +292,7 @@
 - Fix shadow bound calculation in CSM shadow technique ([Popov72](https://github.com/Popov72))
 - Disposing of the depthReducer used in CSM ([Popov72](https://github.com/Popov72))
 - Fixed an issue with teleportation detach and attach ([#7419](https://github.com/BabylonJS/Babylon.js/issues/7419)) ([RaananW](https://github.com/RaananW/))
+- Physics compound calculations were incorrect ([#7407](https://github.com/BabylonJS/Babylon.js/issues/7407)) ([RaananW](https://github.com/RaananW/))
 
 ## Breaking changes
 

+ 1 - 13
src/Physics/Plugins/cannonJSPlugin.ts

@@ -155,7 +155,7 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
                 if (childImpostor) {
                     var parent = childImpostor.parent;
                     if (parent !== mainImpostor) {
-                        var pPosition = mesh.getAbsolutePosition().subtract(mainImpostor.object.getAbsolutePosition());
+                        var pPosition = mesh.position.clone();
                         let localRotation = mesh.rotationQuaternion.multiply(Quaternion.Inverse(currentRotation));
                         if (childImpostor.physicsBody) {
                             this.removePhysicsBody(childImpostor);
@@ -599,18 +599,6 @@ export class CannonJSPlugin implements IPhysicsEnginePlugin {
         joint.physicsJoint.distance = maxDistance;
     }
 
-    // private enableMotor(joint: IMotorEnabledJoint, motorIndex?: number) {
-    //     if (!motorIndex) {
-    //         joint.physicsJoint.enableMotor();
-    //     }
-    // }
-
-    // private disableMotor(joint: IMotorEnabledJoint, motorIndex?: number) {
-    //     if (!motorIndex) {
-    //         joint.physicsJoint.disableMotor();
-    //     }
-    // }
-
     public setMotor(joint: IMotorEnabledJoint, speed?: number, maxForce?: number, motorIndex?: number) {
         if (!motorIndex) {
             joint.physicsJoint.enableMotor();

+ 25 - 11
src/Physics/Plugins/oimoJSPlugin.ts

@@ -102,7 +102,7 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
             var bodyConfig: any = {
                 name: impostor.uniqueId,
                 //Oimo must have mass, also for static objects.
-                config: [impostor.getParam("mass") || 1, impostor.getParam("friction"), impostor.getParam("restitution")],
+                config: [impostor.getParam("mass") || 0.001, impostor.getParam("friction"), impostor.getParam("restitution")],
                 size: [],
                 type: [],
                 pos: [],
@@ -143,11 +143,16 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
                 var oldQuaternion = i.object.rotationQuaternion;
                 globalQuaternion = oldQuaternion.clone();
 
+                i.object.rotationQuaternion.set(0, 0, 0, 1);
+                i.object.computeWorldMatrix(true);
+
                 var rot = oldQuaternion.toEulerAngles();
                 var extendSize = i.getObjectExtendSize();
 
                 const radToDeg = 57.295779513082320876;
 
+                console.log(i.object.position, extendSize);
+
                 if (i === impostor) {
                     var center = impostor.getObjectCenter();
 
@@ -162,17 +167,20 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
 
                     bodyConfig.rotShape.push(0, 0, 0);
                 } else {
-                    let localPosition = i.object.getAbsolutePosition().subtract(impostor.object.getAbsolutePosition());
+                    let localPosition = i.object.position.clone();
                     bodyConfig.posShape.push(localPosition.x);
                     bodyConfig.posShape.push(localPosition.y);
                     bodyConfig.posShape.push(localPosition.z);
-                    bodyConfig.pos.push(0, 0, 0);
+
+                    // bodyConfig.pos.push(0, 0, 0);
 
                     bodyConfig.rotShape.push(rot.x * radToDeg);
                     bodyConfig.rotShape.push(rot.y * radToDeg);
                     bodyConfig.rotShape.push(rot.z * radToDeg);
                 }
 
+                i.object.rotationQuaternion.copyFrom(globalQuaternion);
+
                 // register mesh
                 switch (i.type) {
                     case PhysicsImpostor.ParticleImpostor:
@@ -225,7 +233,7 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
                 //actually not needed, but hey...
                 i.object.rotationQuaternion = oldQuaternion;
             });
-
+            console.log(bodyConfig);
             impostor.physicsBody = this.world.add(bodyConfig);
             // set the quaternion, ignoring the previously defined (euler) rotation
             impostor.physicsBody.resetQuaternion(globalQuaternion);
@@ -328,13 +336,15 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
 
     public setTransformationFromPhysicsBody(impostor: PhysicsImpostor) {
         if (!impostor.physicsBody.sleeping) {
-            //TODO check that
-            /*if (impostor.physicsBody.shapes.next) {
-                var parentShape = this._getLastShape(impostor.physicsBody);
-                impostor.object.position.copyFrom(parentShape.position);
-                console.log(parentShape.position);
-            } else {*/
-            impostor.object.position.copyFrom(impostor.physicsBody.getPosition());
+            if (impostor.physicsBody.shapes.next) {
+                let parent = impostor.physicsBody.shapes;
+                while (parent.next) {
+                    parent = parent.next;
+                }
+                impostor.object.position.copyFrom(parent.position);
+            } else {
+                impostor.object.position.copyFrom(impostor.physicsBody.getPosition());
+            }
             //}
 
             if (impostor.object.rotationQuaternion) {
@@ -345,6 +355,10 @@ export class OimoJSPlugin implements IPhysicsEnginePlugin {
 
     public setPhysicsBodyTransformation(impostor: PhysicsImpostor, newPosition: Vector3, newRotation: Quaternion) {
         var body = impostor.physicsBody;
+        // disable bidirectional for compound meshes
+        if (impostor.physicsBody.shapes.next) {
+            return;
+        }
         body.position.copy(newPosition);
         body.orientation.copy(newRotation);
         body.syncShapes();

+ 1 - 1
src/Physics/physicsImpostor.ts

@@ -636,7 +636,7 @@ export class PhysicsImpostor {
     }
 
     /**
-     * Get a specific parametes from the options parameter
+     * Get a specific parameter from the options parameters
      * @param paramName The object parameter name
      * @returns The object parameter
      */