dominos.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 
25 class Dominos : public Test
26 {
27 public:
28 
30  {
31  b2Body* b1;
32  {
33  b2EdgeShape shape;
34  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
35 
36  b2BodyDef bd;
37  b1 = m_world->CreateBody(&bd);
38  b1->CreateFixture(&shape, 0.0f);
39  }
40 
41  {
42  b2PolygonShape shape;
43  shape.SetAsBox(6.0f, 0.25f);
44 
45  b2BodyDef bd;
46  bd.position.Set(-1.5f, 10.0f);
47  b2Body* ground = m_world->CreateBody(&bd);
48  ground->CreateFixture(&shape, 0.0f);
49  }
50 
51  {
52  b2PolygonShape shape;
53  shape.SetAsBox(0.1f, 1.0f);
54 
55  b2FixtureDef fd;
56  fd.shape = &shape;
57  fd.density = 20.0f;
58  fd.friction = 0.1f;
59 
60  for (int i = 0; i < 10; ++i)
61  {
62  b2BodyDef bd;
63  bd.type = b2_dynamicBody;
64  bd.position.Set(-6.0f + 1.0f * i, 11.25f);
65  b2Body* body = m_world->CreateBody(&bd);
66  body->CreateFixture(&fd);
67  }
68  }
69 
70  {
71  b2PolygonShape shape;
72  shape.SetAsBox(7.0f, 0.25f, b2Vec2_zero, 0.3f);
73 
74  b2BodyDef bd;
75  bd.position.Set(1.0f, 6.0f);
76  b2Body* ground = m_world->CreateBody(&bd);
77  ground->CreateFixture(&shape, 0.0f);
78  }
79 
80  b2Body* b2;
81  {
82  b2PolygonShape shape;
83  shape.SetAsBox(0.25f, 1.5f);
84 
85  b2BodyDef bd;
86  bd.position.Set(-7.0f, 4.0f);
87  b2 = m_world->CreateBody(&bd);
88  b2->CreateFixture(&shape, 0.0f);
89  }
90 
91  b2Body* b3;
92  {
93  b2PolygonShape shape;
94  shape.SetAsBox(6.0f, 0.125f);
95 
96  b2BodyDef bd;
97  bd.type = b2_dynamicBody;
98  bd.position.Set(-0.9f, 1.0f);
99  bd.angle = -0.15f;
100 
101  b3 = m_world->CreateBody(&bd);
102  b3->CreateFixture(&shape, 10.0f);
103  }
104 
106  b2Vec2 anchor;
107 
108  anchor.Set(-2.0f, 1.0f);
109  jd.Initialize(b1, b3, anchor);
110  jd.collideConnected = true;
111  m_world->CreateJoint(&jd);
112 
113  b2Body* b4;
114  {
115  b2PolygonShape shape;
116  shape.SetAsBox(0.25f, 0.25f);
117 
118  b2BodyDef bd;
119  bd.type = b2_dynamicBody;
120  bd.position.Set(-10.0f, 15.0f);
121  b4 = m_world->CreateBody(&bd);
122  b4->CreateFixture(&shape, 10.0f);
123  }
124 
125  anchor.Set(-7.0f, 15.0f);
126  jd.Initialize(b2, b4, anchor);
127  m_world->CreateJoint(&jd);
128 
129  b2Body* b5;
130  {
131  b2BodyDef bd;
132  bd.type = b2_dynamicBody;
133  bd.position.Set(6.5f, 3.0f);
134  b5 = m_world->CreateBody(&bd);
135 
136  b2PolygonShape shape;
137  b2FixtureDef fd;
138 
139  fd.shape = &shape;
140  fd.density = 10.0f;
141  fd.friction = 0.1f;
142 
143  shape.SetAsBox(1.0f, 0.1f, b2Vec2(0.0f, -0.9f), 0.0f);
144  b5->CreateFixture(&fd);
145 
146  shape.SetAsBox(0.1f, 1.0f, b2Vec2(-0.9f, 0.0f), 0.0f);
147  b5->CreateFixture(&fd);
148 
149  shape.SetAsBox(0.1f, 1.0f, b2Vec2(0.9f, 0.0f), 0.0f);
150  b5->CreateFixture(&fd);
151  }
152 
153  anchor.Set(6.0f, 2.0f);
154  jd.Initialize(b1, b5, anchor);
155  m_world->CreateJoint(&jd);
156 
157  b2Body* b6;
158  {
159  b2PolygonShape shape;
160  shape.SetAsBox(1.0f, 0.1f);
161 
162  b2BodyDef bd;
163  bd.type = b2_dynamicBody;
164  bd.position.Set(6.5f, 4.1f);
165  b6 = m_world->CreateBody(&bd);
166  b6->CreateFixture(&shape, 30.0f);
167  }
168 
169  anchor.Set(7.5f, 4.0f);
170  jd.Initialize(b5, b6, anchor);
171  m_world->CreateJoint(&jd);
172 
173  b2Body* b7;
174  {
175  b2PolygonShape shape;
176  shape.SetAsBox(0.1f, 1.0f);
177 
178  b2BodyDef bd;
179  bd.type = b2_dynamicBody;
180  bd.position.Set(7.4f, 1.0f);
181 
182  b7 = m_world->CreateBody(&bd);
183  b7->CreateFixture(&shape, 10.0f);
184  }
185 
186  b2DistanceJointDef djd;
187  djd.bodyA = b3;
188  djd.bodyB = b7;
189  djd.localAnchorA.Set(6.0f, 0.0f);
190  djd.localAnchorB.Set(0.0f, -1.0f);
192  djd.length = d.Length();
193 
194  b2LinearStiffness(djd.stiffness, djd.damping, 1.0f, 1.0f, djd.bodyA, djd.bodyB);
195  m_world->CreateJoint(&djd);
196 
197  {
198  float radius = 0.2f;
199 
200  b2CircleShape shape;
201  shape.m_radius = radius;
202 
203  for (int32 i = 0; i < 4; ++i)
204  {
205  b2BodyDef bd;
206  bd.type = b2_dynamicBody;
207  bd.position.Set(5.9f + 2.0f * radius * i, 2.4f);
208  b2Body* body = m_world->CreateBody(&bd);
209  body->CreateFixture(&shape, 10.0f);
210  }
211  }
212  }
213 
214  static Test* Create()
215  {
216  return new Dominos;
217  }
218 };
219 
220 static int testIndex = RegisterTest("Examples", "Dominos", Dominos::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
d
Dominos()
Definition: dominos.cpp:29
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
B2_API const b2Vec2 b2Vec2_zero
Useful constant.
f
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
float Length() const
Get the length of this vector (the norm).
Definition: b2_math.h:89
Definition: test.h:80
float stiffness
The linear stiffness in N/m.
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
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)
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
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
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
static int testIndex
Definition: dominos.cpp:220
b2Vec2 localAnchorA
The local anchor point relative to bodyA&#39;s origin.
b2World * m_world
Definition: test.h:128
b2Vec2 localAnchorB
The local anchor point relative to bodyB&#39;s origin.
B2_API void b2LinearStiffness(float &stiffness, float &damping, float frequencyHertz, float dampingRatio, const b2Body *bodyA, const b2Body *bodyB)
Utility to compute linear stiffness values from frequency and damping ratio.
Definition: b2_joint.cpp:40
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
float length
The rest length of this joint. Clamped to a stable minimum value.
b2Vec2 position
Definition: b2_body.h:78
b2Vec2 GetWorldPoint(const b2Vec2 &localPoint) const
Definition: b2_body.h:561
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
static Test * Create()
Definition: dominos.cpp:214
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
float damping
The linear damping in N*s/m.
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:20