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);
const b2Shape * shape
Definition: b2_fixture.h:76
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
f
Definition: imgui.h:164
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
int32 m_textLine
Definition: test.h:127
float minLength
Minimum length. Clamped to a stable minimum value.
Definition: test.h:80
float angularDamping
Definition: b2_body.h:99
void DestroyJoint(b2Joint *joint)
Definition: b2_world.cpp:280
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
void Step(Settings &settings) override
signed int int32
Definition: b2_types.h:28
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float m_radius
Definition: b2_shape.h:102
b2Filter filter
Contact filtering data.
Definition: b2_fixture.h:99
void UpdateUI() override
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
b2Joint * m_distanceJoint
bool collideConnected
Set this flag to true if the attached bodies should collide.
Definition: b2_joint.h:95
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Vec2 localAnchorA
The local anchor point relative to bodyA&#39;s origin.
int32 m_textIncrement
Definition: test.h:135
IMGUI_API void End()
Definition: imgui.cpp:5371
b2World * m_world
Definition: test.h:128
b2Vec2 localAnchorB
The local anchor point relative to bodyB&#39;s origin.
static int testIndex
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
b2Vec2 position
Definition: b2_body.h:78
static Test * Create()
float maxLength
Maximum length. Must be greater than or equal to the minimum length.
b2DistanceJointDef m_distanceJointDef
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
IMGUI_API bool Checkbox(const char *label, bool *v)
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
uint16 categoryBits
The collision category bits. Normally you would just set one bit.
Definition: b2_fixture.h:47
DebugDraw g_debugDraw
Definition: draw.cpp:32
uint16 maskBits
Definition: b2_fixture.h:51
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:22