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 */
32package com.jme3.bullet.joints;
33
34import com.bulletphysics.dynamics.constraintsolver.SliderConstraint;
35import com.bulletphysics.linearmath.Transform;
36import com.jme3.bullet.objects.PhysicsRigidBody;
37import com.jme3.bullet.util.Converter;
38import com.jme3.export.InputCapsule;
39import com.jme3.export.JmeExporter;
40import com.jme3.export.JmeImporter;
41import com.jme3.export.OutputCapsule;
42import com.jme3.math.Matrix3f;
43import com.jme3.math.Vector3f;
44import java.io.IOException;
45
46/**
47 * <i>From bullet manual:</i><br>
48 * The slider constraint allows the body to rotate around one axis and translate along this axis.
49 * @author normenhansen
50 */
51public class SliderJoint extends PhysicsJoint {
52    protected Matrix3f rotA, rotB;
53    protected boolean useLinearReferenceFrameA;
54
55    public SliderJoint() {
56    }
57
58    /**
59     * @param pivotA local translation of the joint connection point in node A
60     * @param pivotB local translation of the joint connection point in node B
61     */
62    public SliderJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB, Matrix3f rotA, Matrix3f rotB, boolean useLinearReferenceFrameA) {
63        super(nodeA, nodeB, pivotA, pivotB);
64        this.rotA=rotA;
65        this.rotB=rotB;
66        this.useLinearReferenceFrameA=useLinearReferenceFrameA;
67        createJoint();
68    }
69
70    /**
71     * @param pivotA local translation of the joint connection point in node A
72     * @param pivotB local translation of the joint connection point in node B
73     */
74    public SliderJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB, boolean useLinearReferenceFrameA) {
75        super(nodeA, nodeB, pivotA, pivotB);
76        this.rotA=new Matrix3f();
77        this.rotB=new Matrix3f();
78        this.useLinearReferenceFrameA=useLinearReferenceFrameA;
79        createJoint();
80    }
81
82    public float getLowerLinLimit() {
83        return ((SliderConstraint) constraint).getLowerLinLimit();
84    }
85
86    public void setLowerLinLimit(float lowerLinLimit) {
87        ((SliderConstraint) constraint).setLowerLinLimit(lowerLinLimit);
88    }
89
90    public float getUpperLinLimit() {
91        return ((SliderConstraint) constraint).getUpperLinLimit();
92    }
93
94    public void setUpperLinLimit(float upperLinLimit) {
95        ((SliderConstraint) constraint).setUpperLinLimit(upperLinLimit);
96    }
97
98    public float getLowerAngLimit() {
99        return ((SliderConstraint) constraint).getLowerAngLimit();
100    }
101
102    public void setLowerAngLimit(float lowerAngLimit) {
103        ((SliderConstraint) constraint).setLowerAngLimit(lowerAngLimit);
104    }
105
106    public float getUpperAngLimit() {
107        return ((SliderConstraint) constraint).getUpperAngLimit();
108    }
109
110    public void setUpperAngLimit(float upperAngLimit) {
111        ((SliderConstraint) constraint).setUpperAngLimit(upperAngLimit);
112    }
113
114    public float getSoftnessDirLin() {
115        return ((SliderConstraint) constraint).getSoftnessDirLin();
116    }
117
118    public void setSoftnessDirLin(float softnessDirLin) {
119        ((SliderConstraint) constraint).setSoftnessDirLin(softnessDirLin);
120    }
121
122    public float getRestitutionDirLin() {
123        return ((SliderConstraint) constraint).getRestitutionDirLin();
124    }
125
126    public void setRestitutionDirLin(float restitutionDirLin) {
127        ((SliderConstraint) constraint).setRestitutionDirLin(restitutionDirLin);
128    }
129
130    public float getDampingDirLin() {
131        return ((SliderConstraint) constraint).getDampingDirLin();
132    }
133
134    public void setDampingDirLin(float dampingDirLin) {
135        ((SliderConstraint) constraint).setDampingDirLin(dampingDirLin);
136    }
137
138    public float getSoftnessDirAng() {
139        return ((SliderConstraint) constraint).getSoftnessDirAng();
140    }
141
142    public void setSoftnessDirAng(float softnessDirAng) {
143        ((SliderConstraint) constraint).setSoftnessDirAng(softnessDirAng);
144    }
145
146    public float getRestitutionDirAng() {
147        return ((SliderConstraint) constraint).getRestitutionDirAng();
148    }
149
150    public void setRestitutionDirAng(float restitutionDirAng) {
151        ((SliderConstraint) constraint).setRestitutionDirAng(restitutionDirAng);
152    }
153
154    public float getDampingDirAng() {
155        return ((SliderConstraint) constraint).getDampingDirAng();
156    }
157
158    public void setDampingDirAng(float dampingDirAng) {
159        ((SliderConstraint) constraint).setDampingDirAng(dampingDirAng);
160    }
161
162    public float getSoftnessLimLin() {
163        return ((SliderConstraint) constraint).getSoftnessLimLin();
164    }
165
166    public void setSoftnessLimLin(float softnessLimLin) {
167        ((SliderConstraint) constraint).setSoftnessLimLin(softnessLimLin);
168    }
169
170    public float getRestitutionLimLin() {
171        return ((SliderConstraint) constraint).getRestitutionLimLin();
172    }
173
174    public void setRestitutionLimLin(float restitutionLimLin) {
175        ((SliderConstraint) constraint).setRestitutionLimLin(restitutionLimLin);
176    }
177
178    public float getDampingLimLin() {
179        return ((SliderConstraint) constraint).getDampingLimLin();
180    }
181
182    public void setDampingLimLin(float dampingLimLin) {
183        ((SliderConstraint) constraint).setDampingLimLin(dampingLimLin);
184    }
185
186    public float getSoftnessLimAng() {
187        return ((SliderConstraint) constraint).getSoftnessLimAng();
188    }
189
190    public void setSoftnessLimAng(float softnessLimAng) {
191        ((SliderConstraint) constraint).setSoftnessLimAng(softnessLimAng);
192    }
193
194    public float getRestitutionLimAng() {
195        return ((SliderConstraint) constraint).getRestitutionLimAng();
196    }
197
198    public void setRestitutionLimAng(float restitutionLimAng) {
199        ((SliderConstraint) constraint).setRestitutionLimAng(restitutionLimAng);
200    }
201
202    public float getDampingLimAng() {
203        return ((SliderConstraint) constraint).getDampingLimAng();
204    }
205
206    public void setDampingLimAng(float dampingLimAng) {
207        ((SliderConstraint) constraint).setDampingLimAng(dampingLimAng);
208    }
209
210    public float getSoftnessOrthoLin() {
211        return ((SliderConstraint) constraint).getSoftnessOrthoLin();
212    }
213
214    public void setSoftnessOrthoLin(float softnessOrthoLin) {
215        ((SliderConstraint) constraint).setSoftnessOrthoLin(softnessOrthoLin);
216    }
217
218    public float getRestitutionOrthoLin() {
219        return ((SliderConstraint) constraint).getRestitutionOrthoLin();
220    }
221
222    public void setRestitutionOrthoLin(float restitutionOrthoLin) {
223        ((SliderConstraint) constraint).setRestitutionOrthoLin(restitutionOrthoLin);
224    }
225
226    public float getDampingOrthoLin() {
227        return ((SliderConstraint) constraint).getDampingOrthoLin();
228    }
229
230    public void setDampingOrthoLin(float dampingOrthoLin) {
231        ((SliderConstraint) constraint).setDampingOrthoLin(dampingOrthoLin);
232    }
233
234    public float getSoftnessOrthoAng() {
235        return ((SliderConstraint) constraint).getSoftnessOrthoAng();
236    }
237
238    public void setSoftnessOrthoAng(float softnessOrthoAng) {
239        ((SliderConstraint) constraint).setSoftnessOrthoAng(softnessOrthoAng);
240    }
241
242    public float getRestitutionOrthoAng() {
243        return ((SliderConstraint) constraint).getRestitutionOrthoAng();
244    }
245
246    public void setRestitutionOrthoAng(float restitutionOrthoAng) {
247        ((SliderConstraint) constraint).setRestitutionOrthoAng(restitutionOrthoAng);
248    }
249
250    public float getDampingOrthoAng() {
251        return ((SliderConstraint) constraint).getDampingOrthoAng();
252    }
253
254    public void setDampingOrthoAng(float dampingOrthoAng) {
255        ((SliderConstraint) constraint).setDampingOrthoAng(dampingOrthoAng);
256    }
257
258    public boolean isPoweredLinMotor() {
259        return ((SliderConstraint) constraint).getPoweredLinMotor();
260    }
261
262    public void setPoweredLinMotor(boolean poweredLinMotor) {
263        ((SliderConstraint) constraint).setPoweredLinMotor(poweredLinMotor);
264    }
265
266    public float getTargetLinMotorVelocity() {
267        return ((SliderConstraint) constraint).getTargetLinMotorVelocity();
268    }
269
270    public void setTargetLinMotorVelocity(float targetLinMotorVelocity) {
271        ((SliderConstraint) constraint).setTargetLinMotorVelocity(targetLinMotorVelocity);
272    }
273
274    public float getMaxLinMotorForce() {
275        return ((SliderConstraint) constraint).getMaxLinMotorForce();
276    }
277
278    public void setMaxLinMotorForce(float maxLinMotorForce) {
279        ((SliderConstraint) constraint).setMaxLinMotorForce(maxLinMotorForce);
280    }
281
282    public boolean isPoweredAngMotor() {
283        return ((SliderConstraint) constraint).getPoweredAngMotor();
284    }
285
286    public void setPoweredAngMotor(boolean poweredAngMotor) {
287        ((SliderConstraint) constraint).setPoweredAngMotor(poweredAngMotor);
288    }
289
290    public float getTargetAngMotorVelocity() {
291        return ((SliderConstraint) constraint).getTargetAngMotorVelocity();
292    }
293
294    public void setTargetAngMotorVelocity(float targetAngMotorVelocity) {
295        ((SliderConstraint) constraint).setTargetAngMotorVelocity(targetAngMotorVelocity);
296    }
297
298    public float getMaxAngMotorForce() {
299        return ((SliderConstraint) constraint).getMaxAngMotorForce();
300    }
301
302    public void setMaxAngMotorForce(float maxAngMotorForce) {
303        ((SliderConstraint) constraint).setMaxAngMotorForce(maxAngMotorForce);
304    }
305
306    @Override
307    public void write(JmeExporter ex) throws IOException {
308        super.write(ex);
309        OutputCapsule capsule = ex.getCapsule(this);
310        //TODO: standard values..
311        capsule.write(((SliderConstraint) constraint).getDampingDirAng(), "dampingDirAng", 0f);
312        capsule.write(((SliderConstraint) constraint).getDampingDirLin(), "dampingDirLin", 0f);
313        capsule.write(((SliderConstraint) constraint).getDampingLimAng(), "dampingLimAng", 0f);
314        capsule.write(((SliderConstraint) constraint).getDampingLimLin(), "dampingLimLin", 0f);
315        capsule.write(((SliderConstraint) constraint).getDampingOrthoAng(), "dampingOrthoAng", 0f);
316        capsule.write(((SliderConstraint) constraint).getDampingOrthoLin(), "dampingOrthoLin", 0f);
317        capsule.write(((SliderConstraint) constraint).getLowerAngLimit(), "lowerAngLimit", 0f);
318        capsule.write(((SliderConstraint) constraint).getLowerLinLimit(), "lowerLinLimit", 0f);
319        capsule.write(((SliderConstraint) constraint).getMaxAngMotorForce(), "maxAngMotorForce", 0f);
320        capsule.write(((SliderConstraint) constraint).getMaxLinMotorForce(), "maxLinMotorForce", 0f);
321        capsule.write(((SliderConstraint) constraint).getPoweredAngMotor(), "poweredAngMotor", false);
322        capsule.write(((SliderConstraint) constraint).getPoweredLinMotor(), "poweredLinMotor", false);
323        capsule.write(((SliderConstraint) constraint).getRestitutionDirAng(), "restitutionDirAng", 0f);
324        capsule.write(((SliderConstraint) constraint).getRestitutionDirLin(), "restitutionDirLin", 0f);
325        capsule.write(((SliderConstraint) constraint).getRestitutionLimAng(), "restitutionLimAng", 0f);
326        capsule.write(((SliderConstraint) constraint).getRestitutionLimLin(), "restitutionLimLin", 0f);
327        capsule.write(((SliderConstraint) constraint).getRestitutionOrthoAng(), "restitutionOrthoAng", 0f);
328        capsule.write(((SliderConstraint) constraint).getRestitutionOrthoLin(), "restitutionOrthoLin", 0f);
329
330        capsule.write(((SliderConstraint) constraint).getSoftnessDirAng(), "softnessDirAng", 0f);
331        capsule.write(((SliderConstraint) constraint).getSoftnessDirLin(), "softnessDirLin", 0f);
332        capsule.write(((SliderConstraint) constraint).getSoftnessLimAng(), "softnessLimAng", 0f);
333        capsule.write(((SliderConstraint) constraint).getSoftnessLimLin(), "softnessLimLin", 0f);
334        capsule.write(((SliderConstraint) constraint).getSoftnessOrthoAng(), "softnessOrthoAng", 0f);
335        capsule.write(((SliderConstraint) constraint).getSoftnessOrthoLin(), "softnessOrthoLin", 0f);
336
337        capsule.write(((SliderConstraint) constraint).getTargetAngMotorVelocity(), "targetAngMotorVelicoty", 0f);
338        capsule.write(((SliderConstraint) constraint).getTargetLinMotorVelocity(), "targetLinMotorVelicoty", 0f);
339
340        capsule.write(((SliderConstraint) constraint).getUpperAngLimit(), "upperAngLimit", 0f);
341        capsule.write(((SliderConstraint) constraint).getUpperLinLimit(), "upperLinLimit", 0f);
342
343        capsule.write(useLinearReferenceFrameA, "useLinearReferenceFrameA", false);
344    }
345
346    @Override
347    public void read(JmeImporter im) throws IOException {
348        super.read(im);
349        InputCapsule capsule = im.getCapsule(this);
350        float dampingDirAng = capsule.readFloat("dampingDirAng", 0f);
351        float dampingDirLin = capsule.readFloat("dampingDirLin", 0f);
352        float dampingLimAng = capsule.readFloat("dampingLimAng", 0f);
353        float dampingLimLin = capsule.readFloat("dampingLimLin", 0f);
354        float dampingOrthoAng = capsule.readFloat("dampingOrthoAng", 0f);
355        float dampingOrthoLin = capsule.readFloat("dampingOrthoLin", 0f);
356        float lowerAngLimit = capsule.readFloat("lowerAngLimit", 0f);
357        float lowerLinLimit = capsule.readFloat("lowerLinLimit", 0f);
358        float maxAngMotorForce = capsule.readFloat("maxAngMotorForce", 0f);
359        float maxLinMotorForce = capsule.readFloat("maxLinMotorForce", 0f);
360        boolean poweredAngMotor = capsule.readBoolean("poweredAngMotor", false);
361        boolean poweredLinMotor = capsule.readBoolean("poweredLinMotor", false);
362        float restitutionDirAng = capsule.readFloat("restitutionDirAng", 0f);
363        float restitutionDirLin = capsule.readFloat("restitutionDirLin", 0f);
364        float restitutionLimAng = capsule.readFloat("restitutionLimAng", 0f);
365        float restitutionLimLin = capsule.readFloat("restitutionLimLin", 0f);
366        float restitutionOrthoAng = capsule.readFloat("restitutionOrthoAng", 0f);
367        float restitutionOrthoLin = capsule.readFloat("restitutionOrthoLin", 0f);
368
369        float softnessDirAng = capsule.readFloat("softnessDirAng", 0f);
370        float softnessDirLin = capsule.readFloat("softnessDirLin", 0f);
371        float softnessLimAng = capsule.readFloat("softnessLimAng", 0f);
372        float softnessLimLin = capsule.readFloat("softnessLimLin", 0f);
373        float softnessOrthoAng = capsule.readFloat("softnessOrthoAng", 0f);
374        float softnessOrthoLin = capsule.readFloat("softnessOrthoLin", 0f);
375
376        float targetAngMotorVelicoty = capsule.readFloat("targetAngMotorVelicoty", 0f);
377        float targetLinMotorVelicoty = capsule.readFloat("targetLinMotorVelicoty", 0f);
378
379        float upperAngLimit = capsule.readFloat("upperAngLimit", 0f);
380        float upperLinLimit = capsule.readFloat("upperLinLimit", 0f);
381
382        useLinearReferenceFrameA = capsule.readBoolean("useLinearReferenceFrameA", false);
383
384        createJoint();
385
386        ((SliderConstraint)constraint).setDampingDirAng(dampingDirAng);
387        ((SliderConstraint)constraint).setDampingDirLin(dampingDirLin);
388        ((SliderConstraint)constraint).setDampingLimAng(dampingLimAng);
389        ((SliderConstraint)constraint).setDampingLimLin(dampingLimLin);
390        ((SliderConstraint)constraint).setDampingOrthoAng(dampingOrthoAng);
391        ((SliderConstraint)constraint).setDampingOrthoLin(dampingOrthoLin);
392        ((SliderConstraint)constraint).setLowerAngLimit(lowerAngLimit);
393        ((SliderConstraint)constraint).setLowerLinLimit(lowerLinLimit);
394        ((SliderConstraint)constraint).setMaxAngMotorForce(maxAngMotorForce);
395        ((SliderConstraint)constraint).setMaxLinMotorForce(maxLinMotorForce);
396        ((SliderConstraint)constraint).setPoweredAngMotor(poweredAngMotor);
397        ((SliderConstraint)constraint).setPoweredLinMotor(poweredLinMotor);
398        ((SliderConstraint)constraint).setRestitutionDirAng(restitutionDirAng);
399        ((SliderConstraint)constraint).setRestitutionDirLin(restitutionDirLin);
400        ((SliderConstraint)constraint).setRestitutionLimAng(restitutionLimAng);
401        ((SliderConstraint)constraint).setRestitutionLimLin(restitutionLimLin);
402        ((SliderConstraint)constraint).setRestitutionOrthoAng(restitutionOrthoAng);
403        ((SliderConstraint)constraint).setRestitutionOrthoLin(restitutionOrthoLin);
404
405        ((SliderConstraint)constraint).setSoftnessDirAng(softnessDirAng);
406        ((SliderConstraint)constraint).setSoftnessDirLin(softnessDirLin);
407        ((SliderConstraint)constraint).setSoftnessLimAng(softnessLimAng);
408        ((SliderConstraint)constraint).setSoftnessLimLin(softnessLimLin);
409        ((SliderConstraint)constraint).setSoftnessOrthoAng(softnessOrthoAng);
410        ((SliderConstraint)constraint).setSoftnessOrthoLin(softnessOrthoLin);
411
412        ((SliderConstraint)constraint).setTargetAngMotorVelocity(targetAngMotorVelicoty);
413        ((SliderConstraint)constraint).setTargetLinMotorVelocity(targetLinMotorVelicoty);
414
415        ((SliderConstraint)constraint).setUpperAngLimit(upperAngLimit);
416        ((SliderConstraint)constraint).setUpperLinLimit(upperLinLimit);
417    }
418
419    protected void createJoint(){
420        Transform transA = new Transform(Converter.convert(rotA));
421        Converter.convert(pivotA, transA.origin);
422        Converter.convert(rotA, transA.basis);
423
424        Transform transB = new Transform(Converter.convert(rotB));
425        Converter.convert(pivotB, transB.origin);
426        Converter.convert(rotB, transB.basis);
427
428        constraint = new SliderConstraint(nodeA.getObjectId(), nodeB.getObjectId(), transA, transB, useLinearReferenceFrameA);
429    }
430}
431