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.jme3.bullet.objects.PhysicsRigidBody;
35import com.jme3.export.InputCapsule;
36import com.jme3.export.JmeExporter;
37import com.jme3.export.JmeImporter;
38import com.jme3.export.OutputCapsule;
39import com.jme3.math.Matrix3f;
40import com.jme3.math.Vector3f;
41import java.io.IOException;
42import java.util.logging.Level;
43import java.util.logging.Logger;
44
45/**
46 * <i>From bullet manual:</i><br>
47 * The slider constraint allows the body to rotate around one axis and translate along this axis.
48 * @author normenhansen
49 */
50public class SliderJoint extends PhysicsJoint {
51
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 getLowerLinLimit(objectId);
84    }
85
86    private native float getLowerLinLimit(long objectId);
87
88    public void setLowerLinLimit(float lowerLinLimit) {
89        setLowerLinLimit(objectId, lowerLinLimit);
90    }
91
92    private native void setLowerLinLimit(long objectId, float value);
93
94    public float getUpperLinLimit() {
95        return getUpperLinLimit(objectId);
96    }
97
98    private native float getUpperLinLimit(long objectId);
99
100    public void setUpperLinLimit(float upperLinLimit) {
101        setUpperLinLimit(objectId, upperLinLimit);
102    }
103
104    private native void setUpperLinLimit(long objectId, float value);
105
106    public float getLowerAngLimit() {
107        return getLowerAngLimit(objectId);
108    }
109
110    private native float getLowerAngLimit(long objectId);
111
112    public void setLowerAngLimit(float lowerAngLimit) {
113        setLowerAngLimit(objectId, lowerAngLimit);
114    }
115
116    private native void setLowerAngLimit(long objectId, float value);
117
118    public float getUpperAngLimit() {
119        return getUpperAngLimit(objectId);
120    }
121
122    private native float getUpperAngLimit(long objectId);
123
124    public void setUpperAngLimit(float upperAngLimit) {
125        setUpperAngLimit(objectId, upperAngLimit);
126    }
127
128    private native void setUpperAngLimit(long objectId, float value);
129
130    public float getSoftnessDirLin() {
131        return getSoftnessDirLin(objectId);
132    }
133
134    private native float getSoftnessDirLin(long objectId);
135
136    public void setSoftnessDirLin(float softnessDirLin) {
137        setSoftnessDirLin(objectId, softnessDirLin);
138    }
139
140    private native void setSoftnessDirLin(long objectId, float value);
141
142    public float getRestitutionDirLin() {
143        return getRestitutionDirLin(objectId);
144    }
145
146    private native float getRestitutionDirLin(long objectId);
147
148    public void setRestitutionDirLin(float restitutionDirLin) {
149        setRestitutionDirLin(objectId, restitutionDirLin);
150    }
151
152    private native void setRestitutionDirLin(long objectId, float value);
153
154    public float getDampingDirLin() {
155        return getDampingDirLin(objectId);
156    }
157
158    private native float getDampingDirLin(long objectId);
159
160    public void setDampingDirLin(float dampingDirLin) {
161        setDampingDirLin(objectId, dampingDirLin);
162    }
163
164    private native void setDampingDirLin(long objectId, float value);
165
166    public float getSoftnessDirAng() {
167        return getSoftnessDirAng(objectId);
168    }
169
170    private native float getSoftnessDirAng(long objectId);
171
172    public void setSoftnessDirAng(float softnessDirAng) {
173        setSoftnessDirAng(objectId, softnessDirAng);
174    }
175
176    private native void setSoftnessDirAng(long objectId, float value);
177
178    public float getRestitutionDirAng() {
179        return getRestitutionDirAng(objectId);
180    }
181
182    private native float getRestitutionDirAng(long objectId);
183
184    public void setRestitutionDirAng(float restitutionDirAng) {
185        setRestitutionDirAng(objectId, restitutionDirAng);
186    }
187
188    private native void setRestitutionDirAng(long objectId, float value);
189
190    public float getDampingDirAng() {
191        return getDampingDirAng(objectId);
192    }
193
194    private native float getDampingDirAng(long objectId);
195
196    public void setDampingDirAng(float dampingDirAng) {
197        setDampingDirAng(objectId, dampingDirAng);
198    }
199
200    private native void setDampingDirAng(long objectId, float value);
201
202    public float getSoftnessLimLin() {
203        return getSoftnessLimLin(objectId);
204    }
205
206    private native float getSoftnessLimLin(long objectId);
207
208    public void setSoftnessLimLin(float softnessLimLin) {
209        setSoftnessLimLin(objectId, softnessLimLin);
210    }
211
212    private native void setSoftnessLimLin(long objectId, float value);
213
214    public float getRestitutionLimLin() {
215        return getRestitutionLimLin(objectId);
216    }
217
218    private native float getRestitutionLimLin(long objectId);
219
220    public void setRestitutionLimLin(float restitutionLimLin) {
221        setRestitutionLimLin(objectId, restitutionLimLin);
222    }
223
224    private native void setRestitutionLimLin(long objectId, float value);
225
226    public float getDampingLimLin() {
227        return getDampingLimLin(objectId);
228    }
229
230    private native float getDampingLimLin(long objectId);
231
232    public void setDampingLimLin(float dampingLimLin) {
233        setDampingLimLin(objectId, dampingLimLin);
234    }
235
236    private native void setDampingLimLin(long objectId, float value);
237
238    public float getSoftnessLimAng() {
239        return getSoftnessLimAng(objectId);
240    }
241
242    private native float getSoftnessLimAng(long objectId);
243
244    public void setSoftnessLimAng(float softnessLimAng) {
245        setSoftnessLimAng(objectId, softnessLimAng);
246    }
247
248    private native void setSoftnessLimAng(long objectId, float value);
249
250    public float getRestitutionLimAng() {
251        return getRestitutionLimAng(objectId);
252    }
253
254    private native float getRestitutionLimAng(long objectId);
255
256    public void setRestitutionLimAng(float restitutionLimAng) {
257        setRestitutionLimAng(objectId, restitutionLimAng);
258    }
259
260    private native void setRestitutionLimAng(long objectId, float value);
261
262    public float getDampingLimAng() {
263        return getDampingLimAng(objectId);
264    }
265
266    private native float getDampingLimAng(long objectId);
267
268    public void setDampingLimAng(float dampingLimAng) {
269        setDampingLimAng(objectId, dampingLimAng);
270    }
271
272    private native void setDampingLimAng(long objectId, float value);
273
274    public float getSoftnessOrthoLin() {
275        return getSoftnessOrthoLin(objectId);
276    }
277
278    private native float getSoftnessOrthoLin(long objectId);
279
280    public void setSoftnessOrthoLin(float softnessOrthoLin) {
281        setSoftnessOrthoLin(objectId, softnessOrthoLin);
282    }
283
284    private native void setSoftnessOrthoLin(long objectId, float value);
285
286    public float getRestitutionOrthoLin() {
287        return getRestitutionOrthoLin(objectId);
288    }
289
290    private native float getRestitutionOrthoLin(long objectId);
291
292    public void setRestitutionOrthoLin(float restitutionOrthoLin) {
293        setDampingOrthoLin(objectId, restitutionOrthoLin);
294    }
295
296    private native void setRestitutionOrthoLin(long objectId, float value);
297
298    public float getDampingOrthoLin() {
299        return getDampingOrthoLin(objectId);
300    }
301
302    private native float getDampingOrthoLin(long objectId);
303
304    public void setDampingOrthoLin(float dampingOrthoLin) {
305        setDampingOrthoLin(objectId, dampingOrthoLin);
306    }
307
308    private native void setDampingOrthoLin(long objectId, float value);
309
310    public float getSoftnessOrthoAng() {
311        return getSoftnessOrthoAng(objectId);
312    }
313
314    private native float getSoftnessOrthoAng(long objectId);
315
316    public void setSoftnessOrthoAng(float softnessOrthoAng) {
317        setSoftnessOrthoAng(objectId, softnessOrthoAng);
318    }
319
320    private native void setSoftnessOrthoAng(long objectId, float value);
321
322    public float getRestitutionOrthoAng() {
323        return getRestitutionOrthoAng(objectId);
324    }
325
326    private native float getRestitutionOrthoAng(long objectId);
327
328    public void setRestitutionOrthoAng(float restitutionOrthoAng) {
329        setRestitutionOrthoAng(objectId, restitutionOrthoAng);
330    }
331
332    private native void setRestitutionOrthoAng(long objectId, float value);
333
334    public float getDampingOrthoAng() {
335        return getDampingOrthoAng(objectId);
336    }
337
338    private native float getDampingOrthoAng(long objectId);
339
340    public void setDampingOrthoAng(float dampingOrthoAng) {
341        setDampingOrthoAng(objectId, dampingOrthoAng);
342    }
343
344    private native void setDampingOrthoAng(long objectId, float value);
345
346    public boolean isPoweredLinMotor() {
347        return isPoweredLinMotor(objectId);
348    }
349
350    private native boolean isPoweredLinMotor(long objectId);
351
352    public void setPoweredLinMotor(boolean poweredLinMotor) {
353        setPoweredLinMotor(objectId, poweredLinMotor);
354    }
355
356    private native void setPoweredLinMotor(long objectId, boolean value);
357
358    public float getTargetLinMotorVelocity() {
359        return getTargetLinMotorVelocity(objectId);
360    }
361
362    private native float getTargetLinMotorVelocity(long objectId);
363
364    public void setTargetLinMotorVelocity(float targetLinMotorVelocity) {
365        setTargetLinMotorVelocity(objectId, targetLinMotorVelocity);
366    }
367
368    private native void setTargetLinMotorVelocity(long objectId, float value);
369
370    public float getMaxLinMotorForce() {
371        return getMaxLinMotorForce(objectId);
372    }
373
374    private native float getMaxLinMotorForce(long objectId);
375
376    public void setMaxLinMotorForce(float maxLinMotorForce) {
377        setMaxLinMotorForce(objectId, maxLinMotorForce);
378    }
379
380    private native void setMaxLinMotorForce(long objectId, float value);
381
382    public boolean isPoweredAngMotor() {
383        return isPoweredAngMotor(objectId);
384    }
385
386    private native boolean isPoweredAngMotor(long objectId);
387
388    public void setPoweredAngMotor(boolean poweredAngMotor) {
389        setPoweredAngMotor(objectId, poweredAngMotor);
390    }
391
392    private native void setPoweredAngMotor(long objectId, boolean value);
393
394    public float getTargetAngMotorVelocity() {
395        return getTargetAngMotorVelocity(objectId);
396    }
397
398    private native float getTargetAngMotorVelocity(long objectId);
399
400    public void setTargetAngMotorVelocity(float targetAngMotorVelocity) {
401        setTargetAngMotorVelocity(objectId, targetAngMotorVelocity);
402    }
403
404    private native void setTargetAngMotorVelocity(long objectId, float value);
405
406    public float getMaxAngMotorForce() {
407        return getMaxAngMotorForce(objectId);
408    }
409
410    private native float getMaxAngMotorForce(long objectId);
411
412    public void setMaxAngMotorForce(float maxAngMotorForce) {
413        setMaxAngMotorForce(objectId, maxAngMotorForce);
414    }
415
416    private native void setMaxAngMotorForce(long objectId, float value);
417
418    @Override
419    public void write(JmeExporter ex) throws IOException {
420        super.write(ex);
421        OutputCapsule capsule = ex.getCapsule(this);
422        //TODO: standard values..
423        capsule.write(getDampingDirAng(), "dampingDirAng", 0f);
424        capsule.write(getDampingDirLin(), "dampingDirLin", 0f);
425        capsule.write(getDampingLimAng(), "dampingLimAng", 0f);
426        capsule.write(getDampingLimLin(), "dampingLimLin", 0f);
427        capsule.write(getDampingOrthoAng(), "dampingOrthoAng", 0f);
428        capsule.write(getDampingOrthoLin(), "dampingOrthoLin", 0f);
429        capsule.write(getLowerAngLimit(), "lowerAngLimit", 0f);
430        capsule.write(getLowerLinLimit(), "lowerLinLimit", 0f);
431        capsule.write(getMaxAngMotorForce(), "maxAngMotorForce", 0f);
432        capsule.write(getMaxLinMotorForce(), "maxLinMotorForce", 0f);
433        capsule.write(isPoweredAngMotor(), "poweredAngMotor", false);
434        capsule.write(isPoweredLinMotor(), "poweredLinMotor", false);
435        capsule.write(getRestitutionDirAng(), "restitutionDirAng", 0f);
436        capsule.write(getRestitutionDirLin(), "restitutionDirLin", 0f);
437        capsule.write(getRestitutionLimAng(), "restitutionLimAng", 0f);
438        capsule.write(getRestitutionLimLin(), "restitutionLimLin", 0f);
439        capsule.write(getRestitutionOrthoAng(), "restitutionOrthoAng", 0f);
440        capsule.write(getRestitutionOrthoLin(), "restitutionOrthoLin", 0f);
441
442        capsule.write(getSoftnessDirAng(), "softnessDirAng", 0f);
443        capsule.write(getSoftnessDirLin(), "softnessDirLin", 0f);
444        capsule.write(getSoftnessLimAng(), "softnessLimAng", 0f);
445        capsule.write(getSoftnessLimLin(), "softnessLimLin", 0f);
446        capsule.write(getSoftnessOrthoAng(), "softnessOrthoAng", 0f);
447        capsule.write(getSoftnessOrthoLin(), "softnessOrthoLin", 0f);
448
449        capsule.write(getTargetAngMotorVelocity(), "targetAngMotorVelicoty", 0f);
450        capsule.write(getTargetLinMotorVelocity(), "targetLinMotorVelicoty", 0f);
451
452        capsule.write(getUpperAngLimit(), "upperAngLimit", 0f);
453        capsule.write(getUpperLinLimit(), "upperLinLimit", 0f);
454
455        capsule.write(useLinearReferenceFrameA, "useLinearReferenceFrameA", false);
456    }
457
458    @Override
459    public void read(JmeImporter im) throws IOException {
460        super.read(im);
461        InputCapsule capsule = im.getCapsule(this);
462        float dampingDirAng = capsule.readFloat("dampingDirAng", 0f);
463        float dampingDirLin = capsule.readFloat("dampingDirLin", 0f);
464        float dampingLimAng = capsule.readFloat("dampingLimAng", 0f);
465        float dampingLimLin = capsule.readFloat("dampingLimLin", 0f);
466        float dampingOrthoAng = capsule.readFloat("dampingOrthoAng", 0f);
467        float dampingOrthoLin = capsule.readFloat("dampingOrthoLin", 0f);
468        float lowerAngLimit = capsule.readFloat("lowerAngLimit", 0f);
469        float lowerLinLimit = capsule.readFloat("lowerLinLimit", 0f);
470        float maxAngMotorForce = capsule.readFloat("maxAngMotorForce", 0f);
471        float maxLinMotorForce = capsule.readFloat("maxLinMotorForce", 0f);
472        boolean poweredAngMotor = capsule.readBoolean("poweredAngMotor", false);
473        boolean poweredLinMotor = capsule.readBoolean("poweredLinMotor", false);
474        float restitutionDirAng = capsule.readFloat("restitutionDirAng", 0f);
475        float restitutionDirLin = capsule.readFloat("restitutionDirLin", 0f);
476        float restitutionLimAng = capsule.readFloat("restitutionLimAng", 0f);
477        float restitutionLimLin = capsule.readFloat("restitutionLimLin", 0f);
478        float restitutionOrthoAng = capsule.readFloat("restitutionOrthoAng", 0f);
479        float restitutionOrthoLin = capsule.readFloat("restitutionOrthoLin", 0f);
480
481        float softnessDirAng = capsule.readFloat("softnessDirAng", 0f);
482        float softnessDirLin = capsule.readFloat("softnessDirLin", 0f);
483        float softnessLimAng = capsule.readFloat("softnessLimAng", 0f);
484        float softnessLimLin = capsule.readFloat("softnessLimLin", 0f);
485        float softnessOrthoAng = capsule.readFloat("softnessOrthoAng", 0f);
486        float softnessOrthoLin = capsule.readFloat("softnessOrthoLin", 0f);
487
488        float targetAngMotorVelicoty = capsule.readFloat("targetAngMotorVelicoty", 0f);
489        float targetLinMotorVelicoty = capsule.readFloat("targetLinMotorVelicoty", 0f);
490
491        float upperAngLimit = capsule.readFloat("upperAngLimit", 0f);
492        float upperLinLimit = capsule.readFloat("upperLinLimit", 0f);
493
494        useLinearReferenceFrameA = capsule.readBoolean("useLinearReferenceFrameA", false);
495
496        createJoint();
497
498        setDampingDirAng(dampingDirAng);
499        setDampingDirLin(dampingDirLin);
500        setDampingLimAng(dampingLimAng);
501        setDampingLimLin(dampingLimLin);
502        setDampingOrthoAng(dampingOrthoAng);
503        setDampingOrthoLin(dampingOrthoLin);
504        setLowerAngLimit(lowerAngLimit);
505        setLowerLinLimit(lowerLinLimit);
506        setMaxAngMotorForce(maxAngMotorForce);
507        setMaxLinMotorForce(maxLinMotorForce);
508        setPoweredAngMotor(poweredAngMotor);
509        setPoweredLinMotor(poweredLinMotor);
510        setRestitutionDirAng(restitutionDirAng);
511        setRestitutionDirLin(restitutionDirLin);
512        setRestitutionLimAng(restitutionLimAng);
513        setRestitutionLimLin(restitutionLimLin);
514        setRestitutionOrthoAng(restitutionOrthoAng);
515        setRestitutionOrthoLin(restitutionOrthoLin);
516
517        setSoftnessDirAng(softnessDirAng);
518        setSoftnessDirLin(softnessDirLin);
519        setSoftnessLimAng(softnessLimAng);
520        setSoftnessLimLin(softnessLimLin);
521        setSoftnessOrthoAng(softnessOrthoAng);
522        setSoftnessOrthoLin(softnessOrthoLin);
523
524        setTargetAngMotorVelocity(targetAngMotorVelicoty);
525        setTargetLinMotorVelocity(targetLinMotorVelicoty);
526
527        setUpperAngLimit(upperAngLimit);
528        setUpperLinLimit(upperLinLimit);
529    }
530
531    protected void createJoint() {
532        objectId = createJoint(nodeA.getObjectId(), nodeB.getObjectId(), pivotA, rotA, pivotB, rotB, useLinearReferenceFrameA);
533        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Joint {0}", Long.toHexString(objectId));
534        // = new SliderConstraint(nodeA.getObjectId(), nodeB.getObjectId(), transA, transB, useLinearReferenceFrameA);
535    }
536
537    private native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Matrix3f rotA, Vector3f pivotB, Matrix3f rotB, boolean useLinearReferenceFrameA);
538}
539