Home | History | Annotate | Download | only in joints
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 package com.jme3.bullet.joints;
     33 
     34 import com.bulletphysics.dynamics.constraintsolver.HingeConstraint;
     35 import com.jme3.bullet.objects.PhysicsRigidBody;
     36 import com.jme3.bullet.util.Converter;
     37 import com.jme3.export.InputCapsule;
     38 import com.jme3.export.JmeExporter;
     39 import com.jme3.export.JmeImporter;
     40 import com.jme3.export.OutputCapsule;
     41 import com.jme3.math.Vector3f;
     42 import java.io.IOException;
     43 
     44 /**
     45  * <i>From bullet manual:</i><br>
     46  * Hinge constraint, or revolute joint restricts two additional angular degrees of freedom,
     47  * so the body can only rotate around one axis, the hinge axis.
     48  * This can be useful to represent doors or wheels rotating around one axis.
     49  * The user can specify limits and motor for the hinge.
     50  * @author normenhansen
     51  */
     52 public class HingeJoint extends PhysicsJoint {
     53 
     54     protected Vector3f axisA;
     55     protected Vector3f axisB;
     56     protected boolean angularOnly = false;
     57     protected float biasFactor = 0.3f;
     58     protected float relaxationFactor = 1.0f;
     59     protected float limitSoftness = 0.9f;
     60 
     61     public HingeJoint() {
     62     }
     63 
     64     /**
     65      * Creates a new HingeJoint
     66      * @param pivotA local translation of the joint connection point in node A
     67      * @param pivotB local translation of the joint connection point in node B
     68      */
     69     public HingeJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB, Vector3f axisA, Vector3f axisB) {
     70         super(nodeA, nodeB, pivotA, pivotB);
     71         this.axisA = axisA;
     72         this.axisB = axisB;
     73         createJoint();
     74     }
     75 
     76     public void enableMotor(boolean enable, float targetVelocity, float maxMotorImpulse) {
     77         ((HingeConstraint) constraint).enableAngularMotor(enable, targetVelocity, maxMotorImpulse);
     78     }
     79 
     80     public void setLimit(float low, float high) {
     81         ((HingeConstraint) constraint).setLimit(low, high);
     82     }
     83 
     84     public void setLimit(float low, float high, float _softness, float _biasFactor, float _relaxationFactor) {
     85         biasFactor = _biasFactor;
     86         relaxationFactor = _relaxationFactor;
     87         limitSoftness = _softness;
     88         ((HingeConstraint) constraint).setLimit(low, high, _softness, _biasFactor, _relaxationFactor);
     89     }
     90 
     91     public float getUpperLimit(){
     92         return ((HingeConstraint) constraint).getUpperLimit();
     93     }
     94 
     95     public float getLowerLimit(){
     96         return ((HingeConstraint) constraint).getLowerLimit();
     97     }
     98 
     99     public void setAngularOnly(boolean angularOnly) {
    100         this.angularOnly = angularOnly;
    101         ((HingeConstraint) constraint).setAngularOnly(angularOnly);
    102     }
    103 
    104     public float getHingeAngle() {
    105         return ((HingeConstraint) constraint).getHingeAngle();
    106     }
    107 
    108     public void write(JmeExporter ex) throws IOException {
    109         super.write(ex);
    110         OutputCapsule capsule = ex.getCapsule(this);
    111         capsule.write(axisA, "axisA", new Vector3f());
    112         capsule.write(axisB, "axisB", new Vector3f());
    113 
    114         capsule.write(angularOnly, "angularOnly", false);
    115 
    116         capsule.write(((HingeConstraint) constraint).getLowerLimit(), "lowerLimit", 1e30f);
    117         capsule.write(((HingeConstraint) constraint).getUpperLimit(), "upperLimit", -1e30f);
    118 
    119         capsule.write(biasFactor, "biasFactor", 0.3f);
    120         capsule.write(relaxationFactor, "relaxationFactor", 1f);
    121         capsule.write(limitSoftness, "limitSoftness", 0.9f);
    122 
    123         capsule.write(((HingeConstraint) constraint).getEnableAngularMotor(), "enableAngularMotor", false);
    124         capsule.write(((HingeConstraint) constraint).getMotorTargetVelosity(), "targetVelocity", 0.0f);
    125         capsule.write(((HingeConstraint) constraint).getMaxMotorImpulse(), "maxMotorImpulse", 0.0f);
    126     }
    127 
    128     public void read(JmeImporter im) throws IOException {
    129         super.read(im);
    130         InputCapsule capsule = im.getCapsule(this);
    131         this.axisA = (Vector3f) capsule.readSavable("axisA", new Vector3f());
    132         this.axisB = (Vector3f) capsule.readSavable("axisB", new Vector3f());
    133 
    134         this.angularOnly = capsule.readBoolean("angularOnly", false);
    135         float lowerLimit = capsule.readFloat("lowerLimit", 1e30f);
    136         float upperLimit = capsule.readFloat("upperLimit", -1e30f);
    137 
    138         this.biasFactor = capsule.readFloat("biasFactor", 0.3f);
    139         this.relaxationFactor = capsule.readFloat("relaxationFactor", 1f);
    140         this.limitSoftness = capsule.readFloat("limitSoftness", 0.9f);
    141 
    142         boolean enableAngularMotor=capsule.readBoolean("enableAngularMotor", false);
    143         float targetVelocity=capsule.readFloat("targetVelocity", 0.0f);
    144         float maxMotorImpulse=capsule.readFloat("maxMotorImpulse", 0.0f);
    145 
    146         createJoint();
    147         enableMotor(enableAngularMotor, targetVelocity, maxMotorImpulse);
    148         ((HingeConstraint) constraint).setLimit(lowerLimit, upperLimit, limitSoftness, biasFactor, relaxationFactor);
    149     }
    150 
    151     protected void createJoint() {
    152         constraint = new HingeConstraint(nodeA.getObjectId(), nodeB.getObjectId(),
    153                 Converter.convert(pivotA), Converter.convert(pivotB),
    154                 Converter.convert(axisA), Converter.convert(axisB));
    155     }
    156 }
    157