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.Vector3f;
40import java.io.IOException;
41import java.util.logging.Level;
42import java.util.logging.Logger;
43
44/**
45 * <i>From bullet manual:</i><br>
46 * Point to point constraint, also known as ball socket joint limits the translation
47 * so that the local pivot points of 2 rigidbodies match in worldspace.
48 * A chain of rigidbodies can be connected using this constraint.
49 * @author normenhansen
50 */
51public class Point2PointJoint extends PhysicsJoint {
52
53    public Point2PointJoint() {
54    }
55
56    /**
57     * @param pivotA local translation of the joint connection point in node A
58     * @param pivotB local translation of the joint connection point in node B
59     */
60    public Point2PointJoint(PhysicsRigidBody nodeA, PhysicsRigidBody nodeB, Vector3f pivotA, Vector3f pivotB) {
61        super(nodeA, nodeB, pivotA, pivotB);
62        createJoint();
63    }
64
65    public void setDamping(float value) {
66        setDamping(objectId, value);
67    }
68
69    private native void setDamping(long objectId, float value);
70
71    public void setImpulseClamp(float value) {
72        setImpulseClamp(objectId, value);
73    }
74
75    private native void setImpulseClamp(long objectId, float value);
76
77    public void setTau(float value) {
78        setTau(objectId, value);
79    }
80
81    private native void setTau(long objectId, float value);
82
83    public float getDamping() {
84        return getDamping(objectId);
85    }
86
87    private native float getDamping(long objectId);
88
89    public float getImpulseClamp() {
90        return getImpulseClamp(objectId);
91    }
92
93    private native float getImpulseClamp(long objectId);
94
95    public float getTau() {
96        return getTau(objectId);
97    }
98
99    private native float getTau(long objectId);
100
101    @Override
102    public void write(JmeExporter ex) throws IOException {
103        super.write(ex);
104        OutputCapsule cap = ex.getCapsule(this);
105        cap.write(getDamping(), "damping", 1.0f);
106        cap.write(getTau(), "tau", 0.3f);
107        cap.write(getImpulseClamp(), "impulseClamp", 0f);
108    }
109
110    @Override
111    public void read(JmeImporter im) throws IOException {
112        super.read(im);
113        createJoint();
114        InputCapsule cap = im.getCapsule(this);
115        setDamping(cap.readFloat("damping", 1.0f));
116        setDamping(cap.readFloat("tau", 0.3f));
117        setDamping(cap.readFloat("impulseClamp", 0f));
118    }
119
120    protected void createJoint() {
121        objectId = createJoint(nodeA.getObjectId(), nodeB.getObjectId(), pivotA, pivotB);
122        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Joint {0}", Long.toHexString(objectId));
123    }
124
125    private native long createJoint(long objectIdA, long objectIdB, Vector3f pivotA, Vector3f pivotB);
126}
127