• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.jme3.bullet.objects.PhysicsRigidBody;
35 import com.jme3.export.InputCapsule;
36 import com.jme3.export.JmeExporter;
37 import com.jme3.export.JmeImporter;
38 import com.jme3.export.OutputCapsule;
39 import com.jme3.math.Vector3f;
40 import java.io.IOException;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
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 
HingeJoint()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      */
HingeJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB, Vector3f axisA, Vector3f axisB)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 
enableMotor(boolean enable, float targetVelocity, float maxMotorImpulse)76     public void enableMotor(boolean enable, float targetVelocity, float maxMotorImpulse) {
77         enableMotor(objectId, enable, targetVelocity, maxMotorImpulse);
78     }
79 
enableMotor(long objectId, boolean enable, float targetVelocity, float maxMotorImpulse)80     private native void enableMotor(long objectId, boolean enable, float targetVelocity, float maxMotorImpulse);
81 
getEnableMotor()82     public boolean getEnableMotor() {
83         return getEnableAngularMotor(objectId);
84     }
85 
getEnableAngularMotor(long objectId)86     private native boolean getEnableAngularMotor(long objectId);
87 
getMotorTargetVelocity()88     public float getMotorTargetVelocity() {
89         return getMotorTargetVelocity(objectId);
90     }
91 
getMotorTargetVelocity(long objectId)92     private native float getMotorTargetVelocity(long objectId);
93 
getMaxMotorImpulse()94     public float getMaxMotorImpulse() {
95         return getMaxMotorImpulse(objectId);
96     }
97 
getMaxMotorImpulse(long objectId)98     private native float getMaxMotorImpulse(long objectId);
99 
setLimit(float low, float high)100     public void setLimit(float low, float high) {
101         setLimit(objectId, low, high);
102     }
103 
setLimit(long objectId, float low, float high)104     private native void setLimit(long objectId, float low, float high);
105 
setLimit(float low, float high, float _softness, float _biasFactor, float _relaxationFactor)106     public void setLimit(float low, float high, float _softness, float _biasFactor, float _relaxationFactor) {
107         biasFactor = _biasFactor;
108         relaxationFactor = _relaxationFactor;
109         limitSoftness = _softness;
110         setLimit(objectId, low, high, _softness, _biasFactor, _relaxationFactor);
111     }
112 
setLimit(long objectId, float low, float high, float _softness, float _biasFactor, float _relaxationFactor)113     private native void setLimit(long objectId, float low, float high, float _softness, float _biasFactor, float _relaxationFactor);
114 
getUpperLimit()115     public float getUpperLimit() {
116         return getUpperLimit(objectId);
117     }
118 
getUpperLimit(long objectId)119     private native float getUpperLimit(long objectId);
120 
getLowerLimit()121     public float getLowerLimit() {
122         return getLowerLimit(objectId);
123     }
124 
getLowerLimit(long objectId)125     private native float getLowerLimit(long objectId);
126 
setAngularOnly(boolean angularOnly)127     public void setAngularOnly(boolean angularOnly) {
128         this.angularOnly = angularOnly;
129         setAngularOnly(objectId, angularOnly);
130     }
131 
setAngularOnly(long objectId, boolean angularOnly)132     private native void setAngularOnly(long objectId, boolean angularOnly);
133 
getHingeAngle()134     public float getHingeAngle() {
135         return getHingeAngle(objectId);
136     }
137 
getHingeAngle(long objectId)138     private native float getHingeAngle(long objectId);
139 
write(JmeExporter ex)140     public void write(JmeExporter ex) throws IOException {
141         super.write(ex);
142         OutputCapsule capsule = ex.getCapsule(this);
143         capsule.write(axisA, "axisA", new Vector3f());
144         capsule.write(axisB, "axisB", new Vector3f());
145 
146         capsule.write(angularOnly, "angularOnly", false);
147 
148         capsule.write(getLowerLimit(), "lowerLimit", 1e30f);
149         capsule.write(getUpperLimit(), "upperLimit", -1e30f);
150 
151         capsule.write(biasFactor, "biasFactor", 0.3f);
152         capsule.write(relaxationFactor, "relaxationFactor", 1f);
153         capsule.write(limitSoftness, "limitSoftness", 0.9f);
154 
155         capsule.write(getEnableMotor(), "enableAngularMotor", false);
156         capsule.write(getMotorTargetVelocity(), "targetVelocity", 0.0f);
157         capsule.write(getMaxMotorImpulse(), "maxMotorImpulse", 0.0f);
158     }
159 
read(JmeImporter im)160     public void read(JmeImporter im) throws IOException {
161         super.read(im);
162         InputCapsule capsule = im.getCapsule(this);
163         this.axisA = (Vector3f) capsule.readSavable("axisA", new Vector3f());
164         this.axisB = (Vector3f) capsule.readSavable("axisB", new Vector3f());
165 
166         this.angularOnly = capsule.readBoolean("angularOnly", false);
167         float lowerLimit = capsule.readFloat("lowerLimit", 1e30f);
168         float upperLimit = capsule.readFloat("upperLimit", -1e30f);
169 
170         this.biasFactor = capsule.readFloat("biasFactor", 0.3f);
171         this.relaxationFactor = capsule.readFloat("relaxationFactor", 1f);
172         this.limitSoftness = capsule.readFloat("limitSoftness", 0.9f);
173 
174         boolean enableAngularMotor = capsule.readBoolean("enableAngularMotor", false);
175         float targetVelocity = capsule.readFloat("targetVelocity", 0.0f);
176         float maxMotorImpulse = capsule.readFloat("maxMotorImpulse", 0.0f);
177 
178         createJoint();
179         enableMotor(enableAngularMotor, targetVelocity, maxMotorImpulse);
180         setLimit(lowerLimit, upperLimit, limitSoftness, biasFactor, relaxationFactor);
181     }
182 
createJoint()183     protected void createJoint() {
184         objectId = createJoint(nodeA.getObjectId(), nodeB.getObjectId(), pivotA, axisA, pivotB, axisB);
185         Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Joint {0}", Long.toHexString(objectId));
186     }
187 
createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Vector3f axisA, Vector3f pivotB, Vector3f axisB)188     private native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Vector3f axisA, Vector3f pivotB, Vector3f axisB);
189 }
190