mobile_unbalanced.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 MobileUnbalanced : public Test
26 {
27 public:
28 
29  enum
30  {
31  e_depth = 4
32  };
33 
35  {
36  b2Body* ground;
37 
38  // Create ground body.
39  {
40  b2BodyDef bodyDef;
41  bodyDef.position.Set(0.0f, 20.0f);
42  ground = m_world->CreateBody(&bodyDef);
43  }
44 
45  float a = 0.5f;
46  b2Vec2 h(0.0f, a);
47 
48  b2Body* root = AddNode(ground, b2Vec2_zero, 0, 3.0f, a);
49 
50  b2RevoluteJointDef jointDef;
51  jointDef.bodyA = ground;
52  jointDef.bodyB = root;
53  jointDef.localAnchorA.SetZero();
54  jointDef.localAnchorB = h;
55  m_world->CreateJoint(&jointDef);
56  }
57 
58  b2Body* AddNode(b2Body* parent, const b2Vec2& localAnchor, int32 depth, float offset, float a)
59  {
60  float density = 20.0f;
61  b2Vec2 h(0.0f, a);
62 
63  b2Vec2 p = parent->GetPosition() + localAnchor - h;
64 
65  b2BodyDef bodyDef;
66  bodyDef.type = b2_dynamicBody;
67  bodyDef.position = p;
68  b2Body* body = m_world->CreateBody(&bodyDef);
69 
70  b2PolygonShape shape;
71  shape.SetAsBox(0.25f * a, a);
72  body->CreateFixture(&shape, density);
73 
74  if (depth == e_depth)
75  {
76  return body;
77  }
78 
79  b2Vec2 a1 = b2Vec2(offset, -a);
80  b2Vec2 a2 = b2Vec2(-offset, -a);
81  b2Body* body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
82  b2Body* body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);
83 
84  b2RevoluteJointDef jointDef;
85  jointDef.bodyA = body;
86  jointDef.localAnchorB = h;
87 
88  jointDef.localAnchorA = a1;
89  jointDef.bodyB = body1;
90  m_world->CreateJoint(&jointDef);
91 
92  jointDef.localAnchorA = a2;
93  jointDef.bodyB = body2;
94  m_world->CreateJoint(&jointDef);
95 
96  return body;
97  }
98 
99  static Test* Create()
100  {
101  return new MobileUnbalanced;
102  }
103 };
104 
105 static int testIndex = RegisterTest("Solver", "Mobile Unbalanced", MobileUnbalanced::Create);
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
B2_API const b2Vec2 b2Vec2_zero
Useful constant.
f
static int testIndex
static Test * Create()
Definition: test.h:80
const b2Vec2 & GetPosition() const
Definition: b2_body.h:484
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
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 * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2World * m_world
Definition: test.h:128
b2Vec2 position
Definition: b2_body.h:78
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
b2Body * AddNode(b2Body *parent, const b2Vec2 &localAnchor, int32 depth, float offset, float a)
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
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