web.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 // Test distance joints, body destruction, and joint destruction.
26 class Web : public Test
27 {
28 public:
29  Web()
30  {
31  b2Body* ground = NULL;
32  {
33  b2BodyDef bd;
34  ground = m_world->CreateBody(&bd);
35 
36  b2EdgeShape shape;
37  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
38  ground->CreateFixture(&shape, 0.0f);
39  }
40 
41  {
42  b2PolygonShape shape;
43  shape.SetAsBox(0.5f, 0.5f);
44 
45  b2BodyDef bd;
46  bd.type = b2_dynamicBody;
47 
48  bd.position.Set(-5.0f, 5.0f);
49  m_bodies[0] = m_world->CreateBody(&bd);
50  m_bodies[0]->CreateFixture(&shape, 5.0f);
51 
52  bd.position.Set(5.0f, 5.0f);
53  m_bodies[1] = m_world->CreateBody(&bd);
54  m_bodies[1]->CreateFixture(&shape, 5.0f);
55 
56  bd.position.Set(5.0f, 15.0f);
57  m_bodies[2] = m_world->CreateBody(&bd);
58  m_bodies[2]->CreateFixture(&shape, 5.0f);
59 
60  bd.position.Set(-5.0f, 15.0f);
61  m_bodies[3] = m_world->CreateBody(&bd);
62  m_bodies[3]->CreateFixture(&shape, 5.0f);
63 
65  b2Vec2 p1, p2, d;
66 
67  float frequencyHz = 2.0f;
68  float dampingRatio = 0.0f;
69 
70  jd.bodyA = ground;
71  jd.bodyB = m_bodies[0];
72  jd.localAnchorA.Set(-10.0f, 0.0f);
73  jd.localAnchorB.Set(-0.5f, -0.5f);
74  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
75  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
76  d = p2 - p1;
77  jd.length = d.Length();
78  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
79  m_joints[0] = m_world->CreateJoint(&jd);
80 
81  jd.bodyA = ground;
82  jd.bodyB = m_bodies[1];
83  jd.localAnchorA.Set(10.0f, 0.0f);
84  jd.localAnchorB.Set(0.5f, -0.5f);
85  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
86  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
87  d = p2 - p1;
88  jd.length = d.Length();
89  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
90  m_joints[1] = m_world->CreateJoint(&jd);
91 
92  jd.bodyA = ground;
93  jd.bodyB = m_bodies[2];
94  jd.localAnchorA.Set(10.0f, 20.0f);
95  jd.localAnchorB.Set(0.5f, 0.5f);
96  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
97  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
98  d = p2 - p1;
99  jd.length = d.Length();
100  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
101  m_joints[2] = m_world->CreateJoint(&jd);
102 
103  jd.bodyA = ground;
104  jd.bodyB = m_bodies[3];
105  jd.localAnchorA.Set(-10.0f, 20.0f);
106  jd.localAnchorB.Set(-0.5f, 0.5f);
107  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
108  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
109  d = p2 - p1;
110  jd.length = d.Length();
111  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
112  m_joints[3] = m_world->CreateJoint(&jd);
113 
114  jd.bodyA = m_bodies[0];
115  jd.bodyB = m_bodies[1];
116  jd.localAnchorA.Set(0.5f, 0.0f);
117  jd.localAnchorB.Set(-0.5f, 0.0f);;
118  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
119  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
120  d = p2 - p1;
121  jd.length = d.Length();
122  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
123  m_joints[4] = m_world->CreateJoint(&jd);
124 
125  jd.bodyA = m_bodies[1];
126  jd.bodyB = m_bodies[2];
127  jd.localAnchorA.Set(0.0f, 0.5f);
128  jd.localAnchorB.Set(0.0f, -0.5f);
129  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
130  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
131  d = p2 - p1;
132  jd.length = d.Length();
133  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
134  m_joints[5] = m_world->CreateJoint(&jd);
135 
136  jd.bodyA = m_bodies[2];
137  jd.bodyB = m_bodies[3];
138  jd.localAnchorA.Set(-0.5f, 0.0f);
139  jd.localAnchorB.Set(0.5f, 0.0f);
140  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
141  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
142  d = p2 - p1;
143  jd.length = d.Length();
144  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
145  m_joints[6] = m_world->CreateJoint(&jd);
146 
147  jd.bodyA = m_bodies[3];
148  jd.bodyB = m_bodies[0];
149  jd.localAnchorA.Set(0.0f, -0.5f);
150  jd.localAnchorB.Set(0.0f, 0.5f);
151  p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA);
152  p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB);
153  d = p2 - p1;
154  jd.length = d.Length();
155  b2LinearStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
156  m_joints[7] = m_world->CreateJoint(&jd);
157  }
158  }
159 
160  void Keyboard(int key) override
161  {
162  switch (key)
163  {
164  case GLFW_KEY_B:
165  for (int32 i = 0; i < 4; ++i)
166  {
167  if (m_bodies[i])
168  {
170  m_bodies[i] = NULL;
171  break;
172  }
173  }
174  break;
175 
176  case GLFW_KEY_J:
177  for (int32 i = 0; i < 8; ++i)
178  {
179  if (m_joints[i])
180  {
182  m_joints[i] = NULL;
183  break;
184  }
185  }
186  break;
187  }
188  }
189 
190  void Step(Settings& settings) override
191  {
192  Test::Step(settings);
193  g_debugDraw.DrawString(5, m_textLine, "Press: (b) to delete a body, (j) to delete a joint");
195  }
196 
197  void JointDestroyed(b2Joint* joint) override
198  {
199  for (int32 i = 0; i < 8; ++i)
200  {
201  if (m_joints[i] == joint)
202  {
203  m_joints[i] = NULL;
204  break;
205  }
206  }
207  }
208 
209  static Test* Create()
210  {
211  return new Web;
212  }
213 
216 };
217 
218 static int testIndex = RegisterTest("Examples", "Web", Web::Create);
d
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
void Step(Settings &settings) override
Definition: web.cpp:190
f
float Length() const
Get the length of this vector (the norm).
Definition: b2_math.h:89
int32 m_textLine
Definition: test.h:127
Definition: test.h:80
float stiffness
The linear stiffness in N/m.
void Keyboard(int key) override
Definition: web.cpp:160
void DestroyJoint(b2Joint *joint)
Definition: b2_world.cpp:280
#define GLFW_KEY_B
Definition: glfw3.h:379
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
b2Joint * m_joints[8]
Definition: web.cpp:215
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
Definition: web.cpp:26
static Test * Create()
Definition: web.cpp:209
b2Vec2 localAnchorA
The local anchor point relative to bodyA&#39;s origin.
int32 m_textIncrement
Definition: test.h:135
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
static int testIndex
Definition: web.cpp:218
float length
The rest length of this joint. Clamped to a stable minimum value.
b2Vec2 position
Definition: b2_body.h:78
#define GLFW_KEY_J
Definition: glfw3.h:387
Web()
Definition: web.cpp:29
void DestroyBody(b2Body *body)
Definition: b2_world.cpp:139
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
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
void JointDestroyed(b2Joint *joint) override
Definition: web.cpp:197
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Body * m_bodies[4]
Definition: web.cpp:214
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
DebugDraw g_debugDraw
Definition: draw.cpp:32
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


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