~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/box2d/Box2D/Dynamics/Joints/b2DistanceJoint.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
 
3
*
 
4
* This software is provided 'as-is', without any express or implied
 
5
* warranty.  In no event will the authors be held liable for any damages
 
6
* arising from the use of this software.
 
7
* Permission is granted to anyone to use this software for any purpose,
 
8
* including commercial applications, and to alter it and redistribute it
 
9
* freely, subject to the following restrictions:
 
10
* 1. The origin of this software must not be misrepresented; you must not
 
11
* claim that you wrote the original software. If you use this software
 
12
* in a product, an acknowledgment in the product documentation would be
 
13
* appreciated but is not required.
 
14
* 2. Altered source versions must be plainly marked as such, and must not be
 
15
* misrepresented as being the original software.
 
16
* 3. This notice may not be removed or altered from any source distribution.
 
17
*/
 
18
 
 
19
#ifndef B2_DISTANCE_JOINT_H
 
20
#define B2_DISTANCE_JOINT_H
 
21
 
 
22
#include <Box2D/Dynamics/Joints/b2Joint.h>
 
23
 
 
24
/// Distance joint definition. This requires defining an
 
25
/// anchor point on both bodies and the non-zero length of the
 
26
/// distance joint. The definition uses local anchor points
 
27
/// so that the initial configuration can violate the constraint
 
28
/// slightly. This helps when saving and loading a game.
 
29
/// @warning Do not use a zero or short length.
 
30
// emscripten - b2DistanceJointDef: add functions to set/get base class members
 
31
struct b2DistanceJointDef : public b2JointDef
 
32
{
 
33
        b2DistanceJointDef()
 
34
        {
 
35
                type = e_distanceJoint;
 
36
                localAnchorA.Set(0.0f, 0.0f);
 
37
                localAnchorB.Set(0.0f, 0.0f);
 
38
                length = 1.0f;
 
39
                frequencyHz = 0.0f;
 
40
                dampingRatio = 0.0f;
 
41
        }
 
42
 
 
43
        /// Initialize the bodies, anchors, and length using the world
 
44
        /// anchors.
 
45
        void Initialize(b2Body* bodyA, b2Body* bodyB,
 
46
                                        const b2Vec2& anchorA, const b2Vec2& anchorB);
 
47
 
 
48
        /// The local anchor point relative to bodyA's origin.
 
49
        b2Vec2 localAnchorA;
 
50
 
 
51
        /// The local anchor point relative to bodyB's origin.
 
52
        b2Vec2 localAnchorB;
 
53
 
 
54
        /// The natural length between the anchor points.
 
55
        float32 length;
 
56
 
 
57
        /// The mass-spring-damper frequency in Hertz. A value of 0
 
58
        /// disables softness.
 
59
        float32 frequencyHz;
 
60
 
 
61
        /// The damping ratio. 0 = no damping, 1 = critical damping.
 
62
        float32 dampingRatio;
 
63
 
 
64
        // to generate javascript bindings
 
65
        void set_bodyA(b2Body* b) { bodyA = b; }
 
66
        void set_bodyB(b2Body* b) { bodyB = b; }
 
67
        void set_collideConnected(bool b) { collideConnected = b; }
 
68
        b2Body* get_bodyA(b2Body* b) { return bodyA; }
 
69
        b2Body* get_bodyB(b2Body* b) { return bodyB; }
 
70
        bool get_collideConnected(bool b) { return collideConnected; }
 
71
};
 
72
 
 
73
/// A distance joint constrains two points on two bodies
 
74
/// to remain at a fixed distance from each other. You can view
 
75
/// this as a massless, rigid rod.
 
76
// emscripten - b2DistanceJoint: make constructor public
 
77
class b2DistanceJoint : public b2Joint
 
78
{
 
79
public:
 
80
 
 
81
        b2Vec2 GetAnchorA() const;
 
82
        b2Vec2 GetAnchorB() const;
 
83
 
 
84
        /// Get the reaction force given the inverse time step.
 
85
        /// Unit is N.
 
86
        b2Vec2 GetReactionForce(float32 inv_dt) const;
 
87
 
 
88
        /// Get the reaction torque given the inverse time step.
 
89
        /// Unit is N*m. This is always zero for a distance joint.
 
90
        float32 GetReactionTorque(float32 inv_dt) const;
 
91
 
 
92
        /// The local anchor point relative to bodyA's origin.
 
93
        const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
 
94
 
 
95
        /// The local anchor point relative to bodyB's origin.
 
96
        const b2Vec2& GetLocalAnchorB() const  { return m_localAnchorB; }
 
97
 
 
98
        /// Set/get the natural length.
 
99
        /// Manipulating the length can lead to non-physical behavior when the frequency is zero.
 
100
        void SetLength(float32 length);
 
101
        float32 GetLength() const;
 
102
 
 
103
        /// Set/get frequency in Hz.
 
104
        void SetFrequency(float32 hz);
 
105
        float32 GetFrequency() const;
 
106
 
 
107
        /// Set/get damping ratio.
 
108
        void SetDampingRatio(float32 ratio);
 
109
        float32 GetDampingRatio() const;
 
110
 
 
111
        /// Dump joint to dmLog
 
112
        void Dump();
 
113
 
 
114
        b2DistanceJoint(const b2DistanceJointDef* data);
 
115
 
 
116
protected:
 
117
 
 
118
        friend class b2Joint;
 
119
 
 
120
        void InitVelocityConstraints(const b2SolverData& data);
 
121
        void SolveVelocityConstraints(const b2SolverData& data);
 
122
        bool SolvePositionConstraints(const b2SolverData& data);
 
123
 
 
124
        float32 m_frequencyHz;
 
125
        float32 m_dampingRatio;
 
126
        float32 m_bias;
 
127
 
 
128
        // Solver shared
 
129
        b2Vec2 m_localAnchorA;
 
130
        b2Vec2 m_localAnchorB;
 
131
        float32 m_gamma;
 
132
        float32 m_impulse;
 
133
        float32 m_length;
 
134
 
 
135
        // Solver temp
 
136
        int32 m_indexA;
 
137
        int32 m_indexB;
 
138
        b2Vec2 m_u;
 
139
        b2Vec2 m_rA;
 
140
        b2Vec2 m_rB;
 
141
        b2Vec2 m_localCenterA;
 
142
        b2Vec2 m_localCenterB;
 
143
        float32 m_invMassA;
 
144
        float32 m_invMassB;
 
145
        float32 m_invIA;
 
146
        float32 m_invIB;
 
147
        float32 m_mass;
 
148
};
 
149
 
 
150
inline void b2DistanceJoint::SetLength(float32 length)
 
151
{
 
152
        m_length = length;
 
153
}
 
154
 
 
155
inline float32 b2DistanceJoint::GetLength() const
 
156
{
 
157
        return m_length;
 
158
}
 
159
 
 
160
inline void b2DistanceJoint::SetFrequency(float32 hz)
 
161
{
 
162
        m_frequencyHz = hz;
 
163
}
 
164
 
 
165
inline float32 b2DistanceJoint::GetFrequency() const
 
166
{
 
167
        return m_frequencyHz;
 
168
}
 
169
 
 
170
inline void b2DistanceJoint::SetDampingRatio(float32 ratio)
 
171
{
 
172
        m_dampingRatio = ratio;
 
173
}
 
174
 
 
175
inline float32 b2DistanceJoint::GetDampingRatio() const
 
176
{
 
177
        return m_dampingRatio;
 
178
}
 
179
 
 
180
#endif