wrecking_ball.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 #include "imgui/imgui.h"
25 
34 class WreckingBall : public Test
35 {
36 public:
38  {
39  b2Body* ground = NULL;
40  {
41  b2BodyDef bd;
42  ground = m_world->CreateBody(&bd);
43 
44  b2EdgeShape shape;
45  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
46  ground->CreateFixture(&shape, 0.0f);
47  }
48 
49  {
50  b2PolygonShape shape;
51  shape.SetAsBox(0.5f, 0.125f);
52 
53  b2FixtureDef fd;
54  fd.shape = &shape;
55  fd.density = 20.0f;
56  fd.friction = 0.2f;
57  fd.filter.categoryBits = 0x0001;
58  fd.filter.maskBits = 0xFFFF & ~0x0002;
59 
61  jd.collideConnected = false;
62 
63  const int32 N = 10;
64  const float y = 15.0f;
66 
67  b2Body* prevBody = ground;
68  for (int32 i = 0; i < N; ++i)
69  {
70  b2BodyDef bd;
71  bd.type = b2_dynamicBody;
72  bd.position.Set(0.5f + 1.0f * i, y);
73  if (i == N - 1)
74  {
75  bd.position.Set(1.0f * i, y);
76  bd.angularDamping = 0.4f;
77  }
78 
79  b2Body* body = m_world->CreateBody(&bd);
80 
81  if (i == N - 1)
82  {
83  b2CircleShape circleShape;
84  circleShape.m_radius = 1.5f;
85  b2FixtureDef sfd;
86  sfd.shape = &circleShape;
87  sfd.density = 100.0f;
88  sfd.filter.categoryBits = 0x0002;
89  body->CreateFixture(&sfd);
90  }
91  else
92  {
93  body->CreateFixture(&fd);
94  }
95 
96  b2Vec2 anchor(float(i), y);
97  jd.Initialize(prevBody, body, anchor);
98  m_world->CreateJoint(&jd);
99 
100  prevBody = body;
101  }
102 
104 
105  float extraLength = 0.01f;
107  m_distanceJointDef.maxLength = N - 1.0f + extraLength;
108  m_distanceJointDef.bodyB = prevBody;
109  }
110 
111  {
112  m_distanceJointDef.bodyA = ground;
114  m_stabilize = true;
115  }
116  }
117 
118  void UpdateUI() override
119  {
120  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
121  ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
122  ImGui::Begin("Wrecking Ball Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
123 
124  if (ImGui::Checkbox("Stabilize", &m_stabilize))
125  {
126  if (m_stabilize == true && m_distanceJoint == nullptr)
127  {
129  }
130  else if (m_stabilize == false && m_distanceJoint != nullptr)
131  {
133  m_distanceJoint = nullptr;
134  }
135  }
136 
137  ImGui::End();
138  }
139 
140  void Step(Settings& settings) override
141  {
142  Test::Step(settings);
143 
144  if (m_distanceJoint)
145  {
146  g_debugDraw.DrawString(5, m_textLine, "Distance Joint ON");
147  }
148  else
149  {
150  g_debugDraw.DrawString(5, m_textLine, "Distance Joint OFF");
151  }
153  }
154 
155  static Test* Create()
156  {
157  return new WreckingBall;
158  }
159 
163 };
164 
165 static int testIndex = RegisterTest("Examples", "Wrecking Ball", WreckingBall::Create);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2EdgeShape
Definition: b2_edge_shape.h:32
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Filter::maskBits
uint16 maskBits
Definition: b2_fixture.h:51
b2RevoluteJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
Definition: b2_revolute_joint.cpp:41
NULL
#define NULL
ImGui::SetNextWindowPos
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
b2BodyDef::angularDamping
float angularDamping
Definition: b2_body.h:99
b2DistanceJointDef::localAnchorB
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition: b2_distance_joint.h:56
WreckingBall::Step
void Step(Settings &settings) override
Definition: wrecking_ball.cpp:140
WreckingBall::m_distanceJoint
b2Joint * m_distanceJoint
Definition: wrecking_ball.cpp:161
imgui.h
WreckingBall::WreckingBall
WreckingBall()
Definition: wrecking_ball.cpp:37
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2CircleShape
A solid circle shape.
Definition: b2_circle_shape.h:30
b2World::DestroyJoint
void DestroyJoint(b2Joint *joint)
Definition: b2_world.cpp:280
b2Vec2::SetZero
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
ImVec2
Definition: imgui.h:164
b2FixtureDef
Definition: b2_fixture.h:61
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2EdgeShape::SetTwoSided
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
Definition: b2_edge_shape.cpp:36
b2RevoluteJointDef
Definition: b2_revolute_joint.h:39
f
f
WreckingBall::UpdateUI
void UpdateUI() override
Definition: wrecking_ball.cpp:118
b2DistanceJointDef::maxLength
float maxLength
Maximum length. Must be greater than or equal to the minimum length.
Definition: b2_distance_joint.h:65
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:5371
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
b2Joint
Definition: b2_joint.h:110
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
testIndex
static int testIndex
Definition: wrecking_ball.cpp:165
b2FixtureDef::filter
b2Filter filter
Contact filtering data.
Definition: b2_fixture.h:99
b2Shape::m_radius
float m_radius
Definition: b2_shape.h:102
b2JointDef::collideConnected
bool collideConnected
Set this flag to true if the attached bodies should collide.
Definition: b2_joint.h:95
WreckingBall::Create
static Test * Create()
Definition: wrecking_ball.cpp:155
ImGui::Checkbox
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui_widgets.cpp:876
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
b2DistanceJointDef::minLength
float minLength
Minimum length. Clamped to a stable minimum value.
Definition: b2_distance_joint.h:62
ImGui::SetNextWindowSize
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2BodyDef
Definition: b2_body.h:52
Settings
Definition: settings.h:25
b2FixtureDef::shape
const b2Shape * shape
Definition: b2_fixture.h:76
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2PolygonShape
Definition: b2_polygon_shape.h:32
ImGuiWindowFlags_NoMove
@ ImGuiWindowFlags_NoMove
Definition: imgui.h:688
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
Test::m_textLine
int32 m_textLine
Definition: test.h:127
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
WreckingBall::m_stabilize
bool m_stabilize
Definition: wrecking_ball.cpp:162
WreckingBall
Definition: wrecking_ball.cpp:34
int32
signed int int32
Definition: b2_types.h:28
Test
Definition: test.h:80
b2DistanceJointDef
Definition: b2_distance_joint.h:33
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
b2Filter::categoryBits
uint16 categoryBits
The collision category bits. Normally you would just set one bit.
Definition: b2_fixture.h:47
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
ImGuiWindowFlags_NoResize
@ ImGuiWindowFlags_NoResize
Definition: imgui.h:687
WreckingBall::m_distanceJointDef
b2DistanceJointDef m_distanceJointDef
Definition: wrecking_ball.cpp:160
b2JointDef::bodyA
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
Test::m_world
b2World * m_world
Definition: test.h:128
b2DistanceJointDef::localAnchorA
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition: b2_distance_joint.h:53
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
b2JointDef::bodyB
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08