cantilever.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 // It is difficult to make a cantilever made of links completely rigid with weld joints.
26 // You will have to use a high number of iterations to make them stiff.
27 // So why not go ahead and use soft weld joints? They behave like a revolute
28 // joint with a rotational spring.
29 class Cantilever : public Test
30 {
31 public:
32 
33  enum
34  {
35  e_count = 8
36  };
37 
39  {
40  b2Body* ground = NULL;
41  {
42  b2BodyDef bd;
43  ground = m_world->CreateBody(&bd);
44 
45  b2EdgeShape shape;
46  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
47  ground->CreateFixture(&shape, 0.0f);
48  }
49 
50  {
51  b2PolygonShape shape;
52  shape.SetAsBox(0.5f, 0.125f);
53 
54  b2FixtureDef fd;
55  fd.shape = &shape;
56  fd.density = 20.0f;
57 
58  b2WeldJointDef jd;
59 
60  b2Body* prevBody = ground;
61  for (int32 i = 0; i < e_count; ++i)
62  {
63  b2BodyDef bd;
64  bd.type = b2_dynamicBody;
65  bd.position.Set(-14.5f + 1.0f * i, 5.0f);
66  b2Body* body = m_world->CreateBody(&bd);
67  body->CreateFixture(&fd);
68 
69  b2Vec2 anchor(-15.0f + 1.0f * i, 5.0f);
70  jd.Initialize(prevBody, body, anchor);
71  m_world->CreateJoint(&jd);
72 
73  prevBody = body;
74  }
75  }
76 
77  {
78  b2PolygonShape shape;
79  shape.SetAsBox(1.0f, 0.125f);
80 
81  b2FixtureDef fd;
82  fd.shape = &shape;
83  fd.density = 20.0f;
84 
85  b2WeldJointDef jd;
86  float frequencyHz = 5.0f;
87  float dampingRatio = 0.7f;
88 
89  b2Body* prevBody = ground;
90  for (int32 i = 0; i < 3; ++i)
91  {
92  b2BodyDef bd;
93  bd.type = b2_dynamicBody;
94  bd.position.Set(-14.0f + 2.0f * i, 15.0f);
95  b2Body* body = m_world->CreateBody(&bd);
96  body->CreateFixture(&fd);
97 
98  b2Vec2 anchor(-15.0f + 2.0f * i, 15.0f);
99  jd.Initialize(prevBody, body, anchor);
100  b2AngularStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, jd.bodyA, jd.bodyB);
101  m_world->CreateJoint(&jd);
102 
103  prevBody = body;
104  }
105  }
106 
107  {
108  b2PolygonShape shape;
109  shape.SetAsBox(0.5f, 0.125f);
110 
111  b2FixtureDef fd;
112  fd.shape = &shape;
113  fd.density = 20.0f;
114 
115  b2WeldJointDef jd;
116 
117  b2Body* prevBody = ground;
118  for (int32 i = 0; i < e_count; ++i)
119  {
120  b2BodyDef bd;
121  bd.type = b2_dynamicBody;
122  bd.position.Set(-4.5f + 1.0f * i, 5.0f);
123  b2Body* body = m_world->CreateBody(&bd);
124  body->CreateFixture(&fd);
125 
126  if (i > 0)
127  {
128  b2Vec2 anchor(-5.0f + 1.0f * i, 5.0f);
129  jd.Initialize(prevBody, body, anchor);
130  m_world->CreateJoint(&jd);
131  }
132 
133  prevBody = body;
134  }
135  }
136 
137  {
138  b2PolygonShape shape;
139  shape.SetAsBox(0.5f, 0.125f);
140 
141  b2FixtureDef fd;
142  fd.shape = &shape;
143  fd.density = 20.0f;
144 
145  b2WeldJointDef jd;
146  float frequencyHz = 8.0f;
147  float dampingRatio = 0.7f;
148 
149  b2Body* prevBody = ground;
150  for (int32 i = 0; i < e_count; ++i)
151  {
152  b2BodyDef bd;
153  bd.type = b2_dynamicBody;
154  bd.position.Set(5.5f + 1.0f * i, 10.0f);
155  b2Body* body = m_world->CreateBody(&bd);
156  body->CreateFixture(&fd);
157 
158  if (i > 0)
159  {
160  b2Vec2 anchor(5.0f + 1.0f * i, 10.0f);
161  jd.Initialize(prevBody, body, anchor);
162 
163  b2AngularStiffness(jd.stiffness, jd.damping, frequencyHz, dampingRatio, prevBody, body);
164 
165  m_world->CreateJoint(&jd);
166  }
167 
168  prevBody = body;
169  }
170  }
171 
172  for (int32 i = 0; i < 2; ++i)
173  {
174  b2Vec2 vertices[3];
175  vertices[0].Set(-0.5f, 0.0f);
176  vertices[1].Set(0.5f, 0.0f);
177  vertices[2].Set(0.0f, 1.5f);
178 
179  b2PolygonShape shape;
180  shape.Set(vertices, 3);
181 
182  b2FixtureDef fd;
183  fd.shape = &shape;
184  fd.density = 1.0f;
185 
186  b2BodyDef bd;
187  bd.type = b2_dynamicBody;
188  bd.position.Set(-8.0f + 8.0f * i, 12.0f);
189  b2Body* body = m_world->CreateBody(&bd);
190  body->CreateFixture(&fd);
191  }
192 
193  for (int32 i = 0; i < 2; ++i)
194  {
195  b2CircleShape shape;
196  shape.m_radius = 0.5f;
197 
198  b2FixtureDef fd;
199  fd.shape = &shape;
200  fd.density = 1.0f;
201 
202  b2BodyDef bd;
203  bd.type = b2_dynamicBody;
204  bd.position.Set(-6.0f + 6.0f * i, 10.0f);
205  b2Body* body = m_world->CreateBody(&bd);
206  body->CreateFixture(&fd);
207  }
208  }
209 
210  static Test* Create()
211  {
212  return new Cantilever;
213  }
214 
216 };
217 
218 static int testIndex = RegisterTest("Joints", "Cantilever", Cantilever::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
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: test.h:80
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
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
float damping
The rotational damping in N*m*s.
Definition: b2_weld_joint.h:64
b2World * m_world
Definition: test.h:128
b2Body * m_middle
Definition: cantilever.cpp:215
b2Vec2 position
Definition: b2_body.h:78
void Set(const b2Vec2 *points, int32 count)
static Test * Create()
Definition: cantilever.cpp:210
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
static int testIndex
Definition: cantilever.cpp:218
B2_API void b2AngularStiffness(float &stiffness, float &damping, float frequencyHertz, float dampingRatio, const b2Body *bodyA, const b2Body *bodyB)
Utility to compute rotational stiffness values frequency and damping ratio.
Definition: b2_joint.cpp:65
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:19