1 | /* |
2 | * Copyright (c) 2006-2011 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_ROPE_JOINT_H |
20 | #define B2_ROPE_JOINT_H |
21 | |
22 | #include <Box2D/Dynamics/Joints/b2Joint.h> |
23 | |
24 | /// Rope joint definition. This requires two body anchor points and |
25 | /// a maximum lengths. |
26 | /// Note: by default the connected objects will not collide. |
27 | /// see collideConnected in b2JointDef. |
28 | struct b2RopeJointDef : public b2JointDef |
29 | { |
30 | b2RopeJointDef() |
31 | { |
32 | type = e_ropeJoint; |
33 | localAnchorA.Set(-1.0f, 0.0f); |
34 | localAnchorB.Set(1.0f, 0.0f); |
35 | maxLength = 0.0f; |
36 | } |
37 | |
38 | /// The local anchor point relative to bodyA's origin. |
39 | b2Vec2 localAnchorA; |
40 | |
41 | /// The local anchor point relative to bodyB's origin. |
42 | b2Vec2 localAnchorB; |
43 | |
44 | /// The maximum length of the rope. |
45 | /// Warning: this must be larger than b2_linearSlop or |
46 | /// the joint will have no effect. |
47 | float32 maxLength; |
48 | }; |
49 | |
50 | /// A rope joint enforces a maximum distance between two points |
51 | /// on two bodies. It has no other effect. |
52 | /// Warning: if you attempt to change the maximum length during |
53 | /// the simulation you will get some non-physical behavior. |
54 | /// A model that would allow you to dynamically modify the length |
55 | /// would have some sponginess, so I chose not to implement it |
56 | /// that way. See b2DistanceJoint if you want to dynamically |
57 | /// control length. |
58 | class b2RopeJoint : public b2Joint |
59 | { |
60 | public: |
61 | b2Vec2 GetAnchorA() const; |
62 | b2Vec2 GetAnchorB() const; |
63 | |
64 | b2Vec2 GetReactionForce(float32 inv_dt) const; |
65 | float32 GetReactionTorque(float32 inv_dt) const; |
66 | |
67 | /// The local anchor point relative to bodyA's origin. |
68 | const b2Vec2& GetLocalAnchorA() const { return m_localAnchorA; } |
69 | |
70 | /// The local anchor point relative to bodyB's origin. |
71 | const b2Vec2& GetLocalAnchorB() const { return m_localAnchorB; } |
72 | |
73 | /// Set/Get the maximum length of the rope. |
74 | void SetMaxLength(float32 length) { m_maxLength = length; } |
75 | float32 GetMaxLength() const; |
76 | |
77 | b2LimitState GetLimitState() const; |
78 | |
79 | /// Dump joint to dmLog |
80 | void Dump(); |
81 | |
82 | protected: |
83 | |
84 | friend class b2Joint; |
85 | b2RopeJoint(const b2RopeJointDef* data); |
86 | |
87 | void InitVelocityConstraints(const b2SolverData& data); |
88 | void SolveVelocityConstraints(const b2SolverData& data); |
89 | bool SolvePositionConstraints(const b2SolverData& data); |
90 | |
91 | // Solver shared |
92 | b2Vec2 m_localAnchorA; |
93 | b2Vec2 m_localAnchorB; |
94 | float32 m_maxLength; |
95 | float32 m_length; |
96 | float32 m_impulse; |
97 | |
98 | // Solver temp |
99 | int32 m_indexA; |
100 | int32 m_indexB; |
101 | b2Vec2 m_u; |
102 | b2Vec2 m_rA; |
103 | b2Vec2 m_rB; |
104 | b2Vec2 m_localCenterA; |
105 | b2Vec2 m_localCenterB; |
106 | float32 m_invMassA; |
107 | float32 m_invMassB; |
108 | float32 m_invIA; |
109 | float32 m_invIB; |
110 | float32 m_mass; |
111 | b2LimitState m_state; |
112 | }; |
113 | |
114 | #endif |
115 | |