externals
box2d
testbed
tests
platformer.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
Platformer
:
public
Test
26
{
27
public
:
28
29
enum
State
30
{
31
e_unknown
,
32
e_above
,
33
e_below
34
};
35
36
Platformer
()
37
{
38
// Ground
39
{
40
b2BodyDef
bd;
41
b2Body
* ground =
m_world
->
CreateBody
(&bd);
42
43
b2EdgeShape
shape;
44
shape.
SetTwoSided
(
b2Vec2
(-20.0
f
, 0.0
f
),
b2Vec2
(20.0
f
, 0.0
f
));
45
ground->
CreateFixture
(&shape, 0.0
f
);
46
}
47
48
// Platform
49
{
50
b2BodyDef
bd;
51
bd.
position
.
Set
(0.0
f
, 10.0
f
);
52
b2Body
* body =
m_world
->
CreateBody
(&bd);
53
54
b2PolygonShape
shape;
55
shape.
SetAsBox
(3.0
f
, 0.5
f
);
56
m_platform
= body->
CreateFixture
(&shape, 0.0
f
);
57
58
m_bottom
= 10.0f - 0.5f;
59
m_top
= 10.0f + 0.5f;
60
}
61
62
// Actor
63
{
64
b2BodyDef
bd;
65
bd.
type
=
b2_dynamicBody
;
66
bd.
position
.
Set
(0.0
f
, 12.0
f
);
67
b2Body
* body =
m_world
->
CreateBody
(&bd);
68
69
m_radius
= 0.5f;
70
b2CircleShape
shape;
71
shape.
m_radius
=
m_radius
;
72
m_character
= body->
CreateFixture
(&shape, 20.0
f
);
73
74
body->
SetLinearVelocity
(
b2Vec2
(0.0
f
, -50.0
f
));
75
76
m_state
=
e_unknown
;
77
}
78
}
79
80
void
PreSolve
(
b2Contact
* contact,
const
b2Manifold
* oldManifold)
override
81
{
82
Test::PreSolve
(contact, oldManifold);
83
84
b2Fixture
* fixtureA = contact->
GetFixtureA
();
85
b2Fixture
* fixtureB = contact->
GetFixtureB
();
86
87
if
(fixtureA !=
m_platform
&& fixtureA !=
m_character
)
88
{
89
return
;
90
}
91
92
if
(fixtureB !=
m_platform
&& fixtureB !=
m_character
)
93
{
94
return
;
95
}
96
97
#if 1
98
b2Vec2
position =
m_character
->
GetBody
()->
GetPosition
();
99
100
if
(position.
y
<
m_top
+
m_radius
- 3.0f *
b2_linearSlop
)
101
{
102
contact->
SetEnabled
(
false
);
103
}
104
#else
105
b2Vec2
v =
m_character
->
GetBody
()->
GetLinearVelocity
();
106
if
(v.
y
> 0.0f)
107
{
108
contact->
SetEnabled
(
false
);
109
}
110
#endif
111
}
112
113
void
Step
(
Settings
& settings)
override
114
{
115
Test::Step
(settings);
116
117
b2Vec2
v =
m_character
->
GetBody
()->
GetLinearVelocity
();
118
g_debugDraw
.
DrawString
(5,
m_textLine
,
"Character Linear Velocity: %f"
, v.
y
);
119
m_textLine
+=
m_textIncrement
;
120
}
121
122
static
Test
*
Create
()
123
{
124
return
new
Platformer
;
125
}
126
127
float
m_radius
,
m_top
,
m_bottom
;
128
State
m_state
;
129
b2Fixture
*
m_platform
;
130
b2Fixture
*
m_character
;
131
};
132
133
static
int
testIndex
=
RegisterTest
(
"Examples"
,
"Platformer"
,
Platformer::Create
);
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
b2Manifold
Definition:
b2_collision.h:99
f
f
Platformer::m_bottom
float m_bottom
Definition:
platformer.cpp:127
Platformer::e_unknown
Definition:
platformer.cpp:31
Platformer::m_character
b2Fixture * m_character
Definition:
platformer.cpp:130
b2Vec2::y
float y
Definition:
b2_math.h:128
Test::m_textLine
int32 m_textLine
Definition:
test.h:127
b2Contact::SetEnabled
void SetEnabled(bool flag)
Definition:
b2_contact.h:264
b2Contact::GetFixtureB
b2Fixture * GetFixtureB()
Get fixture B in this contact.
Definition:
b2_contact.h:306
b2BodyDef
Definition:
b2_body.h:52
Platformer::e_above
Definition:
platformer.cpp:32
Test
Definition:
test.h:80
Platformer::e_below
Definition:
platformer.cpp:33
Platformer::m_radius
float m_radius
Definition:
platformer.cpp:127
Platformer::Create
static Test * Create()
Definition:
platformer.cpp:122
b2_linearSlop
#define b2_linearSlop
Definition:
b2_common.h:65
b2Body::GetPosition
const b2Vec2 & GetPosition() const
Definition:
b2_body.h:484
b2Contact
Definition:
b2_contact.h:88
Platformer::PreSolve
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition:
platformer.cpp:80
b2CircleShape
A solid circle shape.
Definition:
b2_circle_shape.h:30
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
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
b2Shape::m_radius
float m_radius
Definition:
b2_shape.h:102
b2Body::SetLinearVelocity
void SetLinearVelocity(const b2Vec2 &v)
Definition:
b2_body.h:504
b2Body::GetLinearVelocity
const b2Vec2 & GetLinearVelocity() const
Definition:
b2_body.h:519
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
Platformer
Definition:
platformer.cpp:25
Test::m_textIncrement
int32 m_textIncrement
Definition:
test.h:135
Settings
Definition:
settings.h:25
Test::m_world
b2World * m_world
Definition:
test.h:128
Platformer::m_top
float m_top
Definition:
platformer.cpp:127
Platformer::m_platform
b2Fixture * m_platform
Definition:
platformer.cpp:129
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
Platformer::m_state
State m_state
Definition:
platformer.cpp:128
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
testIndex
static int testIndex
Definition:
platformer.cpp:133
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition:
draw.cpp:772
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
Platformer::State
State
Definition:
platformer.cpp:29
Platformer::Step
void Step(Settings &settings) override
Definition:
platformer.cpp:113
g_debugDraw
DebugDraw g_debugDraw
Definition:
draw.cpp:32
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
b2Fixture::GetBody
b2Body * GetBody()
Definition:
b2_fixture.h:283
b2_dynamicBody
Definition:
b2_body.h:47
Test::PreSolve
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition:
test.cpp:73
Platformer::Platformer
Platformer()
Definition:
platformer.cpp:36
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21