compound_shapes.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 
26 class CompoundShapes : public Test
27 {
28 public:
30  {
31  {
32  b2BodyDef bd;
33  bd.position.Set(0.0f, 0.0f);
34  b2Body* body = m_world->CreateBody(&bd);
35 
36  b2EdgeShape shape;
37  shape.SetTwoSided(b2Vec2(50.0f, 0.0f), b2Vec2(-50.0f, 0.0f));
38 
39  body->CreateFixture(&shape, 0.0f);
40  }
41 
42  // Table 1
43  {
44  b2BodyDef bd;
45  bd.type = b2_dynamicBody;
46  bd.position.Set(-15.0f, 1.0f);
47  m_table1 = m_world->CreateBody(&bd);
48 
49  b2PolygonShape top;
50  top.SetAsBox(3.0f, 0.5f, b2Vec2(0.0f, 3.5f), 0.0f);
51 
52  b2PolygonShape leftLeg;
53  leftLeg.SetAsBox(0.5f, 1.5f, b2Vec2(-2.5f, 1.5f), 0.0f);
54 
55  b2PolygonShape rightLeg;
56  rightLeg.SetAsBox(0.5f, 1.5f, b2Vec2(2.5f, 1.5f), 0.0f);
57 
58  m_table1->CreateFixture(&top, 2.0f);
59  m_table1->CreateFixture(&leftLeg, 2.0f);
60  m_table1->CreateFixture(&rightLeg, 2.0f);
61  }
62 
63  // Table 2
64  {
65  b2BodyDef bd;
66  bd.type = b2_dynamicBody;
67  bd.position.Set(-5.0f, 1.0f);
68  m_table2 = m_world->CreateBody(&bd);
69 
70  b2PolygonShape top;
71  top.SetAsBox(3.0f, 0.5f, b2Vec2(0.0f, 3.5f), 0.0f);
72 
73  b2PolygonShape leftLeg;
74  leftLeg.SetAsBox(0.5f, 2.0f, b2Vec2(-2.5f, 2.0f), 0.0f);
75 
76  b2PolygonShape rightLeg;
77  rightLeg.SetAsBox(0.5f, 2.0f, b2Vec2(2.5f, 2.0f), 0.0f);
78 
79  m_table2->CreateFixture(&top, 2.0f);
80  m_table2->CreateFixture(&leftLeg, 2.0f);
81  m_table2->CreateFixture(&rightLeg, 2.0f);
82  }
83 
84  // Spaceship 1
85  {
86  b2BodyDef bd;
87  bd.type = b2_dynamicBody;
88  bd.position.Set(5.0f, 1.0f);
89  m_ship1 = m_world->CreateBody(&bd);
90 
91  b2Vec2 vertices[3];
92 
93  b2PolygonShape left;
94  vertices[0].Set(-2.0f, 0.0f);
95  vertices[1].Set(0.0f, 4.0f / 3.0f);
96  vertices[2].Set(0.0f, 4.0f);
97  left.Set(vertices, 3);
98 
99  b2PolygonShape right;
100  vertices[0].Set(2.0f, 0.0f);
101  vertices[1].Set(0.0f, 4.0f / 3.0f);
102  vertices[2].Set(0.0f, 4.0f);
103  right.Set(vertices, 3);
104 
105  m_ship1->CreateFixture(&left, 2.0f);
106  m_ship1->CreateFixture(&right, 2.0f);
107  }
108 
109  // Spaceship 2
110  {
111  b2BodyDef bd;
112  bd.type = b2_dynamicBody;
113  bd.position.Set(15.0f, 1.0f);
114  m_ship2 = m_world->CreateBody(&bd);
115 
116  b2Vec2 vertices[3];
117 
118  b2PolygonShape left;
119  vertices[0].Set(-2.0f, 0.0f);
120  vertices[1].Set(1.0f, 2.0f);
121  vertices[2].Set(0.0f, 4.0f);
122  left.Set(vertices, 3);
123 
124  b2PolygonShape right;
125  vertices[0].Set(2.0f, 0.0f);
126  vertices[1].Set(-1.0f, 2.0f);
127  vertices[2].Set(0.0f, 4.0f);
128  right.Set(vertices, 3);
129 
130  m_ship2->CreateFixture(&left, 2.0f);
131  m_ship2->CreateFixture(&right, 2.0f);
132  }
133  }
134 
135  void Spawn()
136  {
137  // Table 1 obstruction
138  {
139  b2BodyDef bd;
140  bd.type = b2_dynamicBody;
141  bd.position = m_table1->GetPosition();
142  bd.angle = m_table1->GetAngle();
143 
144  b2Body* body = m_world->CreateBody(&bd);
145 
146  b2PolygonShape box;
147  box.SetAsBox(4.0f, 0.1f, b2Vec2(0.0f, 3.0f), 0.0f);
148 
149  body->CreateFixture(&box, 2.0f);
150  }
151 
152  // Table 2 obstruction
153  {
154  b2BodyDef bd;
155  bd.type = b2_dynamicBody;
156  bd.position = m_table2->GetPosition();
157  bd.angle = m_table2->GetAngle();
158 
159  b2Body* body = m_world->CreateBody(&bd);
160 
161  b2PolygonShape box;
162  box.SetAsBox(4.0f, 0.1f, b2Vec2(0.0f, 3.0f), 0.0f);
163 
164  body->CreateFixture(&box, 2.0f);
165  }
166 
167  // Ship 1 obstruction
168  {
169  b2BodyDef bd;
170  bd.type = b2_dynamicBody;
171  bd.position = m_ship1->GetPosition();
172  bd.angle = m_ship1->GetAngle();
173  bd.gravityScale = 0.0f;
174 
175  b2Body* body = m_world->CreateBody(&bd);
176 
177  b2CircleShape circle;
178  circle.m_radius = 0.5f;
179  circle.m_p.Set(0.0f, 2.0f);
180 
181  body->CreateFixture(&circle, 2.0f);
182  }
183 
184  // Ship 2 obstruction
185  {
186  b2BodyDef bd;
187  bd.type = b2_dynamicBody;
188  bd.position = m_ship2->GetPosition();
189  bd.angle = m_ship2->GetAngle();
190  bd.gravityScale = 0.0f;
191 
192  b2Body* body = m_world->CreateBody(&bd);
193 
194  b2CircleShape circle;
195  circle.m_radius = 0.5f;
196  circle.m_p.Set(0.0f, 2.0f);
197 
198  body->CreateFixture(&circle, 2.0f);
199  }
200  }
201 
202  void UpdateUI() override
203  {
204  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
205  ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
207 
208  if (ImGui::Button("Spawn"))
209  {
210  Spawn();
211  }
212 
213  ImGui::End();
214  }
215 
216  static Test* Create()
217  {
218  return new CompoundShapes;
219  }
220 
225 };
226 
227 static int testIndex = RegisterTest("Examples", "Compound Shapes", CompoundShapes::Create);
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
Definition: imgui.h:164
Definition: test.h:80
const b2Vec2 & GetPosition() const
Definition: b2_body.h:484
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
b2Vec2 m_p
Position.
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
static Test * Create()
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
IMGUI_API void End()
Definition: imgui.cpp:5371
float GetAngle() const
Definition: b2_body.h:489
b2World * m_world
Definition: test.h:128
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
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
void Set(const b2Vec2 *points, int32 count)
float gravityScale
Scale the gravity applied to this body.
Definition: b2_body.h:124
static int testIndex
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void UpdateUI() override
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


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