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.Point2PointConstraint;
35import com.jme3.bullet.objects.PhysicsRigidBody;
36import com.jme3.bullet.util.Converter;
37import com.jme3.export.InputCapsule;
38import com.jme3.export.JmeExporter;
39import com.jme3.export.JmeImporter;
40import com.jme3.export.OutputCapsule;
41import com.jme3.math.Vector3f;
42import java.io.IOException;
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        ((Point2PointConstraint) constraint).setting.damping = value;
67    }
68
69    public void setImpulseClamp(float value) {
70        ((Point2PointConstraint) constraint).setting.impulseClamp = value;
71    }
72
73    public void setTau(float value) {
74        ((Point2PointConstraint) constraint).setting.tau = value;
75    }
76
77    public float getDamping() {
78        return ((Point2PointConstraint) constraint).setting.damping;
79    }
80
81    public float getImpulseClamp() {
82        return ((Point2PointConstraint) constraint).setting.impulseClamp;
83    }
84
85    public float getTau() {
86        return ((Point2PointConstraint) constraint).setting.tau;
87    }
88
89    @Override
90    public void write(JmeExporter ex) throws IOException {
91        super.write(ex);
92        OutputCapsule cap = ex.getCapsule(this);
93        cap.write(getDamping(), "damping", 1.0f);
94        cap.write(getTau(), "tau", 0.3f);
95        cap.write(getImpulseClamp(), "impulseClamp", 0f);
96    }
97
98    @Override
99    public void read(JmeImporter im) throws IOException {
100        super.read(im);
101        createJoint();
102        InputCapsule cap=im.getCapsule(this);
103        setDamping(cap.readFloat("damping", 1.0f));
104        setDamping(cap.readFloat("tau", 0.3f));
105        setDamping(cap.readFloat("impulseClamp", 0f));
106    }
107
108    protected void createJoint() {
109        constraint = new Point2PointConstraint(nodeA.getObjectId(), nodeB.getObjectId(), Converter.convert(pivotA), Converter.convert(pivotB));
110    }
111}
112