We need a getSpringConstant() and getDampingCoefficient()

At least I need it, to go along with the setSpring() in Joint. Is there a reason this was left out originally? I know it would require some math to get the values back out, but thats easy stuff. I'm writing it now so I can post the patch once its been tested, unless anybody has an argument against it.

I take it back.



Irrisor, it looks as if the setSpring() is only applied to fixed joints and ball joints. How can I set a spring for a single translational axis (slider joint in ode)?

After reading some on the ODE Wiki I re-learned some information I once knew. Can we implement in jME Physics the parameters for StopERP, StopCFM, bounce… and anything else thats missing in joint axis?

The spring stuff should be applied to stops (I wrote that some some months ago, I think). Using springs for joints with a single translational axis do work in a demo of mine… hold on… I didn't commit that changes to CVS - try it now

It works great!



I exposed setERP and setCFM by making them abstract methods in Joint, and also added getters. In addition, I added these two values to the binary capsule along with the boolean for collision between nodes.



For Joint:

removed code



and for OdeJoint:

removed code



I think that pretty much wraps up the binary stuff, unless I stumble onto somthing else.  8)

Alright Irrisor, give this a look over…



For Joint.java

Index: Joint.java
===================================================================
RCS file: /cvs/jmephysics/src/com/jmex/physics/Joint.java,v
retrieving revision 1.14
diff -u -r1.14 Joint.java
--- Joint.java   25 Feb 2008 09:54:47 -0000   1.14
+++ Joint.java   14 Mar 2008 04:00:02 -0000
@@ -260,6 +260,25 @@
      */
     public abstract void setSpring( float springConstant, float dampingCoefficient );
    
+    /**
+     * Returns the value of the spring constant.
+     *
+     * @return spring constant
+     */
+    public abstract float getSpringConstant();
+   
+    /**
+     * Returns the value of the damping coefficient.
+     *
+     * @return damping coefficient
+     */
+    public abstract float getDampingCoefficient();
+   
+    /**
+     * Returns the class tag.
+     *
+     * @return class tag
+     */
     public Class getClassTag() {
           return Joint.class;
     }
@@ -269,6 +288,9 @@
     public static final String AXES_PROPERTY = "axes";
     public static final String NAME_PROPERTY = "name";
     public static final String NODES_PROPERTY = "nodes";
+    public static final String SPRING_CONSTANT_PROPERTY = "springConstant";
+    public static final String DAMPING_COEFFICIENT_PROPERTY = "dampingCoefficient";
+    public static final String COLLISION_ENABLED_PROPERTY = "collisionEnabled";
 
    public void read(JMEImporter im) throws IOException {
         InputCapsule capsule = im.getCapsule( this );
@@ -295,6 +317,12 @@
         Vector3f anchor = (Vector3f) capsule.readSavable(ANCHOR_PROPERTY, Vector3f.ZERO );
         if (!Vector3f.ZERO.equals(anchor))
            setAnchor(anchor);
+       
+        float spring = capsule.readFloat(SPRING_CONSTANT_PROPERTY, Float.NaN);
+        float damping = capsule.readFloat(DAMPING_COEFFICIENT_PROPERTY, Float.NaN);
+        setSpring(spring, damping);
+       
+        setCollisionEnabled(capsule.readBoolean(COLLISION_ENABLED_PROPERTY, false));
    }
 
    public void write(JMEExporter ex) throws IOException {
@@ -304,6 +332,9 @@
         capsule.writeSavableArrayList( new ArrayList<JointAxis>( axes ), AXES_PROPERTY, null );
         capsule.writeSavableArrayList( new ArrayList<DynamicPhysicsNode>( getNodes() ), NODES_PROPERTY, null );
         capsule.write(getAnchor( null ), ANCHOR_PROPERTY, Vector3f.ZERO );
+        capsule.write(getSpringConstant(), SPRING_CONSTANT_PROPERTY, Float.NaN);
+        capsule.write(getDampingCoefficient(), DAMPING_COEFFICIENT_PROPERTY, Float.NaN);
+        capsule.write(isCollisionEnabled(), COLLISION_ENABLED_PROPERTY, false);
    }
 
     /**




And for OdeJoint.java

Index: OdeJoint.java
===================================================================
RCS file: /cvs/jmephysics/impl/ode/src/com/jmex/physics/impl/ode/joints/OdeJoint.java,v
retrieving revision 1.17
diff -u -r1.17 OdeJoint.java
--- OdeJoint.java   3 Mar 2008 09:40:40 -0000   1.17
+++ OdeJoint.java   14 Mar 2008 04:00:30 -0000
@@ -124,7 +124,7 @@
     }
 
     public void setSpring( float springConstant, float dampingCoefficient ) {
-        if ( odeJoint == null || odeJoint instanceof JointBall || odeJoint instanceof JointFixed ) {
+        if ( odeJoint == null || odeJoint instanceof JointBall || odeJoint instanceof JointFixed || odeJoint instanceof JointSlider ) {
             //        float r = 2.0f * dampingCoefficient *
             //                FastMath.sqrt( sprungMass * springConstant );
             if ( !Float.isNaN( springConstant ) && !Float.isNaN( dampingCoefficient ) ) {
@@ -138,9 +138,33 @@
                 setCFM( Float.NaN );
             }
         } else {
-            throw new UnsupportedOperationException( "spring only supported for 0 axes or 3 rotational axes" );
+            throw new UnsupportedOperationException( "spring only supported for 0 axes, 1 translational axis or 3 rotational axes" );
         }
     }
+   
+    public float getSpringConstant() {
+       
+       float springConstant = Float.NaN;
+       
+       if(!Float.isNaN( erp ) && !Float.isNaN( cfm )) {
+
+          springConstant = (erp / cfm) / getSpace().getODEJavaWorld().getStepSize();
+       }
+       
+       return springConstant;
+    }
+   
+    public float getDampingCoefficient() {
+       
+       float dampingCeofficient = Float.NaN;
+       
+       if(!Float.isNaN( erp ) && !Float.isNaN( cfm )) {
+          
+          dampingCeofficient = (1 / cfm) - (erp / cfm);
+       }
+       
+       return dampingCeofficient;
+    }
 
     private float erp = Float.NaN;
     private float cfm = Float.NaN;



Since the spring settings work on joint stops of a slider joint, I added that to the list of supported setSpring() joints. Works great.

Hmm, the problem with this is, that other physics engines (e.g. PhysX) do not allow to set erp/cfm… I think a real getter for the spring stuff is needed and those values should be written to file.

So, are you saying kill ERP/CFM accessors in Joint, but add getters for the two values passed into setSpring()? I can work with that, as long as the erp and cfm are still applied to joint stops in the ODE implementation.

nymon said:

So, are you saying kill ERP/CFM accessors in Joint, but add getters for the two values passed into setSpring()?

Yep

Good work. I will commit it to CVS when I get the chance (but most probably not before Monday).

Applied. Thanks nymon.



Little notes for creating patches: 1. please include the path for the file names (at least package path) 2. single file/box is more convenient

roger.