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 * To create ragdolls, the conve twist constraint is very useful for limbs like the upper arm.
48 * It is a special point to point constraint that adds cone and twist axis limits.
49 * The x-axis serves as twist axis.
50 * @author normenhansen
51 */
52public class ConeJoint extends PhysicsJoint {
53
54    protected Matrix3f rotA, rotB;
55    protected float swingSpan1 = 1e30f;
56    protected float swingSpan2 = 1e30f;
57    protected float twistSpan = 1e30f;
58    protected boolean angularOnly = false;
59
60    public ConeJoint() {
61    }
62
63    /**
64     * @param pivotA local translation of the joint connection point in node A
65     * @param pivotB local translation of the joint connection point in node B
66     */
67    public ConeJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB) {
68        super(nodeA, nodeB, pivotA, pivotB);
69        this.rotA = new Matrix3f();
70        this.rotB = new Matrix3f();
71        createJoint();
72    }
73
74    /**
75     * @param pivotA local translation of the joint connection point in node A
76     * @param pivotB local translation of the joint connection point in node B
77     */
78    public ConeJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB, Matrix3f rotA, Matrix3f rotB) {
79        super(nodeA, nodeB, pivotA, pivotB);
80        this.rotA = rotA;
81        this.rotB = rotB;
82        createJoint();
83    }
84
85    public void setLimit(float swingSpan1, float swingSpan2, float twistSpan) {
86        this.swingSpan1 = swingSpan1;
87        this.swingSpan2 = swingSpan2;
88        this.twistSpan = twistSpan;
89        setLimit(objectId, swingSpan1, swingSpan2, twistSpan);
90    }
91
92    private native void setLimit(long objectId, float swingSpan1, float swingSpan2, float twistSpan);
93
94    public void setAngularOnly(boolean value) {
95        angularOnly = value;
96        setAngularOnly(objectId, value);
97    }
98
99    private native void setAngularOnly(long objectId, boolean value);
100
101    @Override
102    public void write(JmeExporter ex) throws IOException {
103        super.write(ex);
104        OutputCapsule capsule = ex.getCapsule(this);
105        capsule.write(rotA, "rotA", new Matrix3f());
106        capsule.write(rotB, "rotB", new Matrix3f());
107
108        capsule.write(angularOnly, "angularOnly", false);
109        capsule.write(swingSpan1, "swingSpan1", 1e30f);
110        capsule.write(swingSpan2, "swingSpan2", 1e30f);
111        capsule.write(twistSpan, "twistSpan", 1e30f);
112    }
113
114    @Override
115    public void read(JmeImporter im) throws IOException {
116        super.read(im);
117        InputCapsule capsule = im.getCapsule(this);
118        this.rotA = (Matrix3f) capsule.readSavable("rotA", new Matrix3f());
119        this.rotB = (Matrix3f) capsule.readSavable("rotB", new Matrix3f());
120
121        this.angularOnly = capsule.readBoolean("angularOnly", false);
122        this.swingSpan1 = capsule.readFloat("swingSpan1", 1e30f);
123        this.swingSpan2 = capsule.readFloat("swingSpan2", 1e30f);
124        this.twistSpan = capsule.readFloat("twistSpan", 1e30f);
125        createJoint();
126    }
127
128    protected void createJoint() {
129        objectId = createJoint(nodeA.getObjectId(), nodeB.getObjectId(), pivotA, rotA, pivotB, rotB);
130        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Joint {0}", Long.toHexString(objectId));
131        setLimit(objectId, swingSpan1, swingSpan2, twistSpan);
132        setAngularOnly(objectId, angularOnly);
133    }
134
135    private native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Matrix3f rotA, Vector3f pivotB, Matrix3f rotB);
136}
137