瀏覽代碼

linear and angular velocities added to soft bodies

Guide 6 年之前
父節點
當前提交
3a23c0b34d
共有 3 個文件被更改,包括 40 次插入33 次删除
  1. 0 2
      src/Meshes/mesh.ts
  2. 16 14
      src/Physics/Plugins/ammoJSPlugin.ts
  3. 24 17
      src/Physics/physicsImpostor.ts

+ 0 - 2
src/Meshes/mesh.ts

@@ -2400,7 +2400,6 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
 
     /**
      * Increase the number of facets and hence vertices in a mesh
-     * Introduced for use with Ammo.JSPlugin soft bodies
      * Warning : the mesh is really modified even if not set originally as updatable. A new VertexBuffer is created under the hood each call.
      * @param numberPerEdge the number of new vertices to add to each edge of a facet, optional default 1
      */
@@ -2545,7 +2544,6 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
     /**
      * Force adjacent facets to share vertices and remove any facets that have all vertices in a line
      * This will undo any application of covertToFlatShadedMesh
-     * Introduced for use with Ammo.JSPlugin soft bodies
      * Warning : the mesh is really modified even if not set originally as updatable. A new VertexBuffer is created under the hood each call.
      */
     public forceSharedVertices(): void {

+ 16 - 14
src/Physics/Plugins/ammoJSPlugin.ts

@@ -769,12 +769,12 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
      * @param velocity velocity to set
      */
     public setLinearVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
-        if (!impostor.soft) {
-            this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
-            impostor.physicsBody.setLinearVelocity(this._tmpAmmoVectorA);
+        this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
+        if (impostor.soft) {
+            impostor.physicsBody.linearVelocity(this._tmpAmmoVectorA);
         }
         else {
-            Logger.Warn("Linear velocity cannot be applied to a soft body");
+            impostor.physicsBody.setLinearVelocity(this._tmpAmmoVectorA);
         }
     }
 
@@ -784,12 +784,12 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
      * @param velocity velocity to set
      */
     public setAngularVelocity(impostor: PhysicsImpostor, velocity: Vector3) {
-        if (!impostor.soft) {
-            this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
-            impostor.physicsBody.setAngularVelocity(this._tmpAmmoVectorA);
+        this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
+        if (impostor.soft) {
+            impostor.physicsBody.angularVelocity(this._tmpAmmoVectorA);
         }
         else {
-            Logger.Warn("Angular velocity cannot be applied to a soft body");
+            impostor.physicsBody.setAngularVelocity(this._tmpAmmoVectorA);
         }
     }
 
@@ -800,10 +800,11 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
      */
     public getLinearVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
         if (impostor.soft) {
-            Logger.Warn("Linear velocity is not a property of a soft body");
-            return null;
+            var v = impostor.physicsBody.linearVelocity();
+        }
+        else {
+            var v = impostor.physicsBody.getLinearVelocity();
         }
-        var v = impostor.physicsBody.getLinearVelocity();
         if (!v) {
             return null;
         }
@@ -817,10 +818,11 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
      */
     public getAngularVelocity(impostor: PhysicsImpostor): Nullable<Vector3> {
         if (impostor.soft) {
-            Logger.Warn("Angular velocity is not a property a soft body");
-            return null;
+            var v = impostor.physicsBody.angularVelocity();
+        }
+        else {
+            var v = impostor.physicsBody.getAngularVelocity();
         }
-        var v = impostor.physicsBody.getAngularVelocity();
         if (!v) {
             return null;
         }

+ 24 - 17
src/Physics/physicsImpostor.ts

@@ -269,10 +269,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return 0;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().getBodyPressure) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin .setBodyPressure) {
             return 0;
         }
-        return this._physicsEngine.getPhysicsPlugin().getBodyPressure!(this);
+        return plugin.getBodyPressure!(this);
     }
 
     /**
@@ -282,10 +283,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().setBodyPressure) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.setBodyPressure) {
             return;
         }
-        this._physicsEngine.getPhysicsPlugin().setBodyPressure!(this, value);
+        plugin.setBodyPressure!(this, value);
     }
 
     /**
@@ -295,10 +297,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return 0;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().getBodyStiffness) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.getBodyStiffness) {
             return 0;
         }
-        return this._physicsEngine.getPhysicsPlugin().getBodyStiffness!(this);
+        return plugin.getBodyStiffness!(this);
     }
 
     /**
@@ -308,10 +311,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().setBodyStiffness) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.setBodyStiffness) {
             return;
         }
-        this._physicsEngine.getPhysicsPlugin().setBodyStiffness!(this, value);
+        plugin.setBodyStiffness!(this, value);
     }
 
     /**
@@ -321,10 +325,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return 0;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().getBodyVelocityIterations) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.getBodyVelocityIterations) {
             return 0;
         }
-        return this._physicsEngine.getPhysicsPlugin().getBodyVelocityIterations!(this);
+        return plugin.getBodyVelocityIterations!(this);
     }
 
     /**
@@ -334,10 +339,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().setBodyVelocityIterations) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.setBodyVelocityIterations) {
             return;
         }
-        this._physicsEngine.getPhysicsPlugin().setBodyVelocityIterations!(this, value);
+        plugin.setBodyVelocityIterations!(this, value);
     }
 
     /**
@@ -347,10 +353,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return 0;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().getBodyPositionIterations) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.getBodyPositionIterations) {
             return 0;
         }
-        return this._physicsEngine.getPhysicsPlugin().getBodyPositionIterations!(this);
+        return plugin.getBodyPositionIterations!(this);
     }
 
     /**
@@ -360,10 +367,11 @@ export class PhysicsImpostor {
         if (!this._physicsEngine) {
             return;
         }
-        if (!this._physicsEngine.getPhysicsPlugin().setBodyPositionIterations) {
+        const plugin = this._physicsEngine.getPhysicsPlugin();
+        if (!plugin.setBodyPositionIterations) {
             return;
         }
-        this._physicsEngine.getPhysicsPlugin().setBodyPositionIterations!(this, value);
+        plugin.setBodyPositionIterations!(this, value);
     }
 
     /**
@@ -435,7 +443,6 @@ export class PhysicsImpostor {
             this._options.mass = (_options.mass === void 0) ? 0 : _options.mass;
             this._options.friction = (_options.friction === void 0) ? 0.2 : _options.friction;
             this._options.restitution = (_options.restitution === void 0) ? 0.2 : _options.restitution;
-            this._options.pressure = (_options.pressure === void 0) ? 0 : _options.pressure;
             if (this.soft) {
                 //softbody mass must be above 0;
                 this._options.mass = this._options.mass > 0 ? this._options.mass : 1;