externals
box2d
testbed
tests
conveyor_belt.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
ConveyorBelt
:
public
Test
26
{
27
public
:
28
29
ConveyorBelt
()
30
{
31
// Ground
32
{
33
b2BodyDef
bd;
34
b2Body
* ground =
m_world
->
CreateBody
(&bd);
35
36
b2EdgeShape
shape;
37
shape.
SetTwoSided
(
b2Vec2
(-20.0
f
, 0.0
f
),
b2Vec2
(20.0
f
, 0.0
f
));
38
ground->
CreateFixture
(&shape, 0.0
f
);
39
}
40
41
// Platform
42
{
43
b2BodyDef
bd;
44
bd.
position
.
Set
(-5.0
f
, 5.0
f
);
45
b2Body
* body =
m_world
->
CreateBody
(&bd);
46
47
b2PolygonShape
shape;
48
shape.
SetAsBox
(10.0
f
, 0.5
f
);
49
50
b2FixtureDef
fd;
51
fd.
shape
= &shape;
52
fd.
friction
= 0.8f;
53
m_platform
= body->
CreateFixture
(&fd);
54
}
55
56
// Boxes
57
for
(
int32
i = 0; i < 5; ++i)
58
{
59
b2BodyDef
bd;
60
bd.
type
=
b2_dynamicBody
;
61
bd.
position
.
Set
(-10.0
f
+ 2.0
f
* i, 7.0
f
);
62
b2Body
* body =
m_world
->
CreateBody
(&bd);
63
64
b2PolygonShape
shape;
65
shape.
SetAsBox
(0.5
f
, 0.5
f
);
66
body->
CreateFixture
(&shape, 20.0
f
);
67
}
68
}
69
70
void
PreSolve
(
b2Contact
* contact,
const
b2Manifold
* oldManifold)
override
71
{
72
Test::PreSolve
(contact, oldManifold);
73
74
b2Fixture
* fixtureA = contact->
GetFixtureA
();
75
b2Fixture
* fixtureB = contact->
GetFixtureB
();
76
77
if
(fixtureA ==
m_platform
)
78
{
79
contact->
SetTangentSpeed
(5.0
f
);
80
}
81
82
if
(fixtureB ==
m_platform
)
83
{
84
contact->
SetTangentSpeed
(-5.0
f
);
85
}
86
}
87
88
void
Step
(
Settings
& settings)
override
89
{
90
Test::Step
(settings);
91
}
92
93
static
Test
*
Create
()
94
{
95
return
new
ConveyorBelt
;
96
}
97
98
b2Fixture
*
m_platform
;
99
};
100
101
static
int
testIndex
=
RegisterTest
(
"Examples"
,
"Conveyor Belt"
,
ConveyorBelt::Create
);
b2FixtureDef::shape
const b2Shape * shape
Definition:
b2_fixture.h:76
ConveyorBelt::m_platform
b2Fixture * m_platform
Definition:
conveyor_belt.cpp:98
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
b2Manifold
Definition:
b2_collision.h:99
f
f
ConveyorBelt::Create
static Test * Create()
Definition:
conveyor_belt.cpp:93
ConveyorBelt::Step
void Step(Settings &settings) override
Definition:
conveyor_belt.cpp:88
b2Contact::GetFixtureB
b2Fixture * GetFixtureB()
Get fixture B in this contact.
Definition:
b2_contact.h:306
b2BodyDef
Definition:
b2_body.h:52
b2FixtureDef
Definition:
b2_fixture.h:61
Test
Definition:
test.h:80
testIndex
static int testIndex
Definition:
conveyor_belt.cpp:101
b2Contact::SetTangentSpeed
void SetTangentSpeed(float speed)
Set the desired tangent speed for a conveyor belt behavior. In meters per second. ...
Definition:
b2_contact.h:376
b2Contact
Definition:
b2_contact.h:88
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
int32
signed int int32
Definition:
b2_types.h:28
ConveyorBelt::ConveyorBelt
ConveyorBelt()
Definition:
conveyor_belt.cpp:29
b2PolygonShape
Definition:
b2_polygon_shape.h:32
b2Fixture
Definition:
b2_fixture.h:116
b2EdgeShape::SetTwoSided
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
Definition:
b2_edge_shape.cpp:36
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
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
ConveyorBelt::PreSolve
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition:
conveyor_belt.cpp:70
Settings
Definition:
settings.h:25
Test::m_world
b2World * m_world
Definition:
test.h:128
b2Contact::GetFixtureA
b2Fixture * GetFixtureA()
Get fixture A in this contact.
Definition:
b2_contact.h:296
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
b2EdgeShape
Definition:
b2_edge_shape.h:32
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition:
b2_fixture.h:82
ConveyorBelt
Definition:
conveyor_belt.cpp:25
b2_dynamicBody
Definition:
b2_body.h:47
Test::PreSolve
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition:
test.cpp:73
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19