externals
box2d
testbed
tests
mobile_balanced.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
MobileBalanced
:
public
Test
26
{
27
public
:
28
29
enum
30
{
31
e_depth
= 4
32
};
33
34
MobileBalanced
()
35
{
36
b2Body
* ground;
37
38
// Create ground body.
39
{
40
b2BodyDef
bodyDef;
41
bodyDef.
position
.
Set
(0.0
f
, 20.0
f
);
42
ground =
m_world
->
CreateBody
(&bodyDef);
43
}
44
45
float
a = 0.5f;
46
b2Vec2
h(0.0
f
, a);
47
48
b2Body
* root =
AddNode
(ground,
b2Vec2_zero
, 0, 3.0
f
, 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.0
f
, 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.25
f
* a, a);
72
body->
CreateFixture
(&shape, density);
73
74
if
(depth ==
e_depth
)
75
{
76
return
body;
77
}
78
79
shape.
SetAsBox
(offset, 0.25
f
* a,
b2Vec2
(0, -a), 0.0
f
);
80
body->
CreateFixture
(&shape, density);
81
82
b2Vec2
a1 =
b2Vec2
(offset, -a);
83
b2Vec2
a2 =
b2Vec2
(-offset, -a);
84
b2Body
* body1 =
AddNode
(body, a1, depth + 1, 0.5
f
* offset, a);
85
b2Body
* body2 =
AddNode
(body, a2, depth + 1, 0.5
f
* offset, a);
86
87
b2RevoluteJointDef
jointDef;
88
jointDef.
bodyA
= body;
89
jointDef.
localAnchorB
= h;
90
91
jointDef.
localAnchorA
= a1;
92
jointDef.
bodyB
= body1;
93
m_world
->
CreateJoint
(&jointDef);
94
95
jointDef.
localAnchorA
= a2;
96
jointDef.
bodyB
= body2;
97
m_world
->
CreateJoint
(&jointDef);
98
99
return
body;
100
}
101
102
static
Test
*
Create
()
103
{
104
return
new
MobileBalanced
;
105
}
106
};
107
108
static
int
testIndex
=
RegisterTest
(
"Solver"
,
"Mobile Balanced"
,
MobileBalanced::Create
);
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
b2RevoluteJointDef::localAnchorB
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
Definition:
b2_revolute_joint.h:63
b2Vec2_zero
B2_API const b2Vec2 b2Vec2_zero
Useful constant.
MobileBalanced::Create
static Test * Create()
Definition:
mobile_balanced.cpp:102
MobileBalanced::e_depth
Definition:
mobile_balanced.cpp:31
f
f
b2BodyDef
Definition:
b2_body.h:52
MobileBalanced::AddNode
b2Body * AddNode(b2Body *parent, const b2Vec2 &localAnchor, int32 depth, float offset, float a)
Definition:
mobile_balanced.cpp:58
Test
Definition:
test.h:80
MobileBalanced::MobileBalanced
MobileBalanced()
Definition:
mobile_balanced.cpp:34
b2Body::GetPosition
const b2Vec2 & GetPosition() const
Definition:
b2_body.h:484
b2Vec2::SetZero
void SetZero()
Set this vector to all zeros.
Definition:
b2_math.h:50
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
int32
signed int int32
Definition:
b2_types.h:28
b2PolygonShape
Definition:
b2_polygon_shape.h:32
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition:
b2_polygon_shape.cpp:36
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
MobileBalanced
Definition:
mobile_balanced.cpp:25
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition:
b2_world.cpp:220
b2RevoluteJointDef::localAnchorA
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
Definition:
b2_revolute_joint.h:60
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
Test::m_world
b2World * m_world
Definition:
test.h:128
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
testIndex
static int testIndex
Definition:
mobile_balanced.cpp:108
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
b2RevoluteJointDef
Definition:
b2_revolute_joint.h:39
b2JointDef::bodyA
b2Body * bodyA
The first attached body.
Definition:
b2_joint.h:89
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
b2JointDef::bodyB
b2Body * bodyB
The second attached body.
Definition:
b2_joint.h:92
b2_dynamicBody
Definition:
b2_body.h:47
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21