2
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
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.
19
#ifndef B2_DISTANCE_JOINT_H
20
#define B2_DISTANCE_JOINT_H
22
#include <Box2D/Dynamics/Joints/b2Joint.h>
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
35
type = e_distanceJoint;
36
localAnchorA.Set(0.0f, 0.0f);
37
localAnchorB.Set(0.0f, 0.0f);
43
/// Initialize the bodies, anchors, and length using the world
45
void Initialize(b2Body* bodyA, b2Body* bodyB,
46
const b2Vec2& anchorA, const b2Vec2& anchorB);
48
/// The local anchor point relative to bodyA's origin.
51
/// The local anchor point relative to bodyB's origin.
54
/// The natural length between the anchor points.
57
/// The mass-spring-damper frequency in Hertz. A value of 0
58
/// disables softness.
61
/// The damping ratio. 0 = no damping, 1 = critical damping.
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; }
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
81
b2Vec2 GetAnchorA() const;
82
b2Vec2 GetAnchorB() const;
84
/// Get the reaction force given the inverse time step.
86
b2Vec2 GetReactionForce(float32 inv_dt) const;
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;
92
/// The local anchor point relative to bodyA's origin.
93
const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; }
95
/// The local anchor point relative to bodyB's origin.
96
const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; }
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;
103
/// Set/get frequency in Hz.
104
void SetFrequency(float32 hz);
105
float32 GetFrequency() const;
107
/// Set/get damping ratio.
108
void SetDampingRatio(float32 ratio);
109
float32 GetDampingRatio() const;
111
/// Dump joint to dmLog
114
b2DistanceJoint(const b2DistanceJointDef* data);
118
friend class b2Joint;
120
void InitVelocityConstraints(const b2SolverData& data);
121
void SolveVelocityConstraints(const b2SolverData& data);
122
bool SolvePositionConstraints(const b2SolverData& data);
124
float32 m_frequencyHz;
125
float32 m_dampingRatio;
129
b2Vec2 m_localAnchorA;
130
b2Vec2 m_localAnchorB;
141
b2Vec2 m_localCenterA;
142
b2Vec2 m_localCenterB;
150
inline void b2DistanceJoint::SetLength(float32 length)
155
inline float32 b2DistanceJoint::GetLength() const
160
inline void b2DistanceJoint::SetFrequency(float32 hz)
165
inline float32 b2DistanceJoint::GetFrequency() const
167
return m_frequencyHz;
170
inline void b2DistanceJoint::SetDampingRatio(float32 ratio)
172
m_dampingRatio = ratio;
175
inline float32 b2DistanceJoint::GetDampingRatio() const
177
return m_dampingRatio;