bridge.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 Bridge : public Test
26 {
27 public:
28 
29  enum
30  {
31  e_count = 30
32  };
33 
35  {
36  b2Body* ground = NULL;
37  {
38  b2BodyDef bd;
39  ground = m_world->CreateBody(&bd);
40 
41  b2EdgeShape shape;
42  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
43  ground->CreateFixture(&shape, 0.0f);
44  }
45 
46  {
47  b2PolygonShape shape;
48  shape.SetAsBox(0.5f, 0.125f);
49 
50  b2FixtureDef fd;
51  fd.shape = &shape;
52  fd.density = 20.0f;
53  fd.friction = 0.2f;
54 
56 
57  b2Body* prevBody = ground;
58  for (int32 i = 0; i < e_count; ++i)
59  {
60  b2BodyDef bd;
61  bd.type = b2_dynamicBody;
62  bd.position.Set(-14.5f + 1.0f * i, 5.0f);
63  b2Body* body = m_world->CreateBody(&bd);
64  body->CreateFixture(&fd);
65 
66  b2Vec2 anchor(-15.0f + 1.0f * i, 5.0f);
67  jd.Initialize(prevBody, body, anchor);
68  m_world->CreateJoint(&jd);
69 
70  if (i == (e_count >> 1))
71  {
72  m_middle = body;
73  }
74  prevBody = body;
75  }
76 
77  b2Vec2 anchor(-15.0f + 1.0f * e_count, 5.0f);
78  jd.Initialize(prevBody, ground, anchor);
79  m_world->CreateJoint(&jd);
80  }
81 
82  for (int32 i = 0; i < 2; ++i)
83  {
84  b2Vec2 vertices[3];
85  vertices[0].Set(-0.5f, 0.0f);
86  vertices[1].Set(0.5f, 0.0f);
87  vertices[2].Set(0.0f, 1.5f);
88 
89  b2PolygonShape shape;
90  shape.Set(vertices, 3);
91 
92  b2FixtureDef fd;
93  fd.shape = &shape;
94  fd.density = 1.0f;
95 
96  b2BodyDef bd;
97  bd.type = b2_dynamicBody;
98  bd.position.Set(-8.0f + 8.0f * i, 12.0f);
99  b2Body* body = m_world->CreateBody(&bd);
100  body->CreateFixture(&fd);
101  }
102 
103  for (int32 i = 0; i < 3; ++i)
104  {
105  b2CircleShape shape;
106  shape.m_radius = 0.5f;
107 
108  b2FixtureDef fd;
109  fd.shape = &shape;
110  fd.density = 1.0f;
111 
112  b2BodyDef bd;
113  bd.type = b2_dynamicBody;
114  bd.position.Set(-6.0f + 6.0f * i, 10.0f);
115  b2Body* body = m_world->CreateBody(&bd);
116  body->CreateFixture(&fd);
117  }
118  }
119 
120  static Test* Create()
121  {
122  return new Bridge;
123  }
124 
126 };
127 
128 static int testIndex = RegisterTest("Joints", "Bridge", Bridge::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
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
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
b2Body * m_middle
Definition: bridge.cpp:125
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
static Test * Create()
Definition: bridge.cpp:120
b2World * m_world
Definition: test.h:128
static int testIndex
Definition: bridge.cpp:128
Bridge()
Definition: bridge.cpp:34
b2Vec2 position
Definition: b2_body.h:78
void Set(const b2Vec2 *points, int32 count)
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
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:19