2
* Copyright (c) 2006-2010 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.
22
/// This test shows how a rope joint can be used to stabilize a chain of
23
/// bodies with a heavy payload. Notice that the rope joint just prevents
24
/// excessive stretching and has no other effect.
25
/// By disabling the rope joint you can see that the Box2D solver has trouble
26
/// supporting heavy bodies with light bodies. Try playing around with the
27
/// densities, time step, and iterations to see how they affect stability.
28
/// This test also shows how to use contact filtering. Filtering is configured
29
/// so that the payload does not collide with the chain.
30
class RopeJoint : public Test
35
b2Body* ground = NULL;
38
ground = m_world->CreateBody(&bd);
41
shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
42
ground->CreateFixture(&shape, 0.0f);
47
shape.SetAsBox(0.5f, 0.125f);
53
fd.filter.categoryBits = 0x0001;
54
fd.filter.maskBits = 0xFFFF & ~0x0002;
56
b2RevoluteJointDef jd;
57
jd.collideConnected = false;
60
const float32 y = 15.0f;
61
m_ropeDef.localAnchorA.Set(0.0f, y);
63
b2Body* prevBody = ground;
64
for (int32 i = 0; i < N; ++i)
67
bd.type = b2_dynamicBody;
68
bd.position.Set(0.5f + 1.0f * i, y);
71
shape.SetAsBox(1.5f, 1.5f);
73
fd.filter.categoryBits = 0x0002;
74
bd.position.Set(1.0f * i, y);
75
bd.angularDamping = 0.4f;
78
b2Body* body = m_world->CreateBody(&bd);
80
body->CreateFixture(&fd);
82
b2Vec2 anchor(float32(i), y);
83
jd.Initialize(prevBody, body, anchor);
84
m_world->CreateJoint(&jd);
89
m_ropeDef.localAnchorB.SetZero();
91
float32 extraLength = 0.01f;
92
m_ropeDef.maxLength = N - 1.0f + extraLength;
93
m_ropeDef.bodyB = prevBody;
97
m_ropeDef.bodyA = ground;
98
m_rope = m_world->CreateJoint(&m_ropeDef);
102
void Keyboard(unsigned char key)
109
m_world->DestroyJoint(m_rope);
114
m_rope = m_world->CreateJoint(&m_ropeDef);
120
void Step(Settings* settings)
122
Test::Step(settings);
123
m_debugDraw.DrawString(5, m_textLine, "Press (j) to toggle the rope joint.");
127
m_debugDraw.DrawString(5, m_textLine, "Rope ON");
131
m_debugDraw.DrawString(5, m_textLine, "Rope OFF");
136
static Test* Create()
138
return new RopeJoint;
141
b2RopeJointDef m_ropeDef;