externals
box2d
testbed
tests
pinball.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
27
class
Pinball
:
public
Test
28
{
29
public
:
30
Pinball
()
31
{
32
// Ground body
33
b2Body
* ground = NULL;
34
{
35
b2BodyDef
bd;
36
ground =
m_world
->
CreateBody
(&bd);
37
38
b2Vec2
vs[5];
39
vs[0].
Set
(-8.0
f
, 6.0
f
);
40
vs[1].
Set
(-8.0
f
, 20.0
f
);
41
vs[2].
Set
(8.0
f
, 20.0
f
);
42
vs[3].
Set
(8.0
f
, 6.0
f
);
43
vs[4].
Set
(0.0
f
, -2.0
f
);
44
45
b2ChainShape
loop;
46
loop.
CreateLoop
(vs, 5);
47
b2FixtureDef
fd;
48
fd.
shape
= &loop;
49
fd.
density
= 0.0f;
50
ground->
CreateFixture
(&fd);
51
}
52
53
// Flippers
54
{
55
b2Vec2
p1(-2.0
f
, 0.0
f
), p2(2.0
f
, 0.0
f
);
56
57
b2BodyDef
bd;
58
bd.
type
=
b2_dynamicBody
;
59
60
bd.
position
= p1;
61
b2Body
* leftFlipper =
m_world
->
CreateBody
(&bd);
62
63
bd.
position
= p2;
64
b2Body
* rightFlipper =
m_world
->
CreateBody
(&bd);
65
66
b2PolygonShape
box;
67
box.
SetAsBox
(1.75
f
, 0.1
f
);
68
69
b2FixtureDef
fd;
70
fd.
shape
= &box;
71
fd.
density
= 1.0f;
72
73
leftFlipper->
CreateFixture
(&fd);
74
rightFlipper->
CreateFixture
(&fd);
75
76
b2RevoluteJointDef
jd;
77
jd.
bodyA
= ground;
78
jd.
localAnchorB
.
SetZero
();
79
jd.
enableMotor
=
true
;
80
jd.
maxMotorTorque
= 1000.0f;
81
jd.
enableLimit
=
true
;
82
83
jd.
motorSpeed
= 0.0f;
84
jd.
localAnchorA
= p1;
85
jd.
bodyB
= leftFlipper;
86
jd.
lowerAngle
= -30.0f *
b2_pi
/ 180.0f;
87
jd.
upperAngle
= 5.0f *
b2_pi
/ 180.0f;
88
m_leftJoint
= (
b2RevoluteJoint
*)
m_world
->
CreateJoint
(&jd);
89
90
jd.
motorSpeed
= 0.0f;
91
jd.
localAnchorA
= p2;
92
jd.
bodyB
= rightFlipper;
93
jd.
lowerAngle
= -5.0f *
b2_pi
/ 180.0f;
94
jd.
upperAngle
= 30.0f *
b2_pi
/ 180.0f;
95
m_rightJoint
= (
b2RevoluteJoint
*)
m_world
->
CreateJoint
(&jd);
96
}
97
98
// Circle character
99
{
100
b2BodyDef
bd;
101
bd.
position
.
Set
(1.0
f
, 15.0
f
);
102
bd.
type
=
b2_dynamicBody
;
103
bd.
bullet
=
true
;
104
105
m_ball
=
m_world
->
CreateBody
(&bd);
106
107
b2CircleShape
shape;
108
shape.
m_radius
= 0.2f;
109
110
b2FixtureDef
fd;
111
fd.
shape
= &shape;
112
fd.
density
= 1.0f;
113
m_ball
->
CreateFixture
(&fd);
114
}
115
116
m_button
=
false
;
117
}
118
119
void
Step
(
Settings
& settings)
override
120
{
121
if
(
m_button
)
122
{
123
m_leftJoint
->
SetMotorSpeed
(20.0
f
);
124
m_rightJoint
->
SetMotorSpeed
(-20.0
f
);
125
}
126
else
127
{
128
m_leftJoint
->
SetMotorSpeed
(-10.0
f
);
129
m_rightJoint
->
SetMotorSpeed
(10.0
f
);
130
}
131
132
Test::Step
(settings);
133
134
g_debugDraw
.
DrawString
(5,
m_textLine
,
"Press 'a' to control the flippers"
);
135
m_textLine
+=
m_textIncrement
;
136
137
}
138
139
void
Keyboard
(
int
key)
override
140
{
141
switch
(key)
142
{
143
case
GLFW_KEY_A
:
144
m_button
=
true
;
145
break
;
146
}
147
}
148
149
void
KeyboardUp
(
int
key)
override
150
{
151
switch
(key)
152
{
153
case
GLFW_KEY_A
:
154
m_button
=
false
;
155
break
;
156
}
157
}
158
159
static
Test
*
Create
()
160
{
161
return
new
Pinball
;
162
}
163
164
b2RevoluteJoint
*
m_leftJoint
;
165
b2RevoluteJoint
*
m_rightJoint
;
166
b2Body
*
m_ball
;
167
bool
m_button
;
168
};
169
170
static
int
testIndex
=
RegisterTest
(
"Examples"
,
"Pinball"
,
Pinball::Create
);
b2FixtureDef::shape
const b2Shape * shape
Definition:
b2_fixture.h:76
b2RevoluteJoint
Definition:
b2_revolute_joint.h:94
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
b2RevoluteJointDef::lowerAngle
float lowerAngle
The lower angle for the joint limit (radians).
Definition:
b2_revolute_joint.h:72
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition:
b2_fixture.h:92
Pinball::Step
void Step(Settings &settings) override
Definition:
pinball.cpp:119
Pinball::Pinball
Pinball()
Definition:
pinball.cpp:30
b2RevoluteJoint::SetMotorSpeed
void SetMotorSpeed(float speed)
Set the motor speed in radians per second.
Definition:
b2_revolute_joint.cpp:378
f
f
Test::m_textLine
int32 m_textLine
Definition:
test.h:127
Pinball::Create
static Test * Create()
Definition:
pinball.cpp:159
b2BodyDef
Definition:
b2_body.h:52
b2FixtureDef
Definition:
b2_fixture.h:61
Pinball
Definition:
pinball.cpp:27
Test
Definition:
test.h:80
b2ChainShape
Definition:
b2_chain_shape.h:36
b2BodyDef::bullet
bool bullet
Definition:
b2_body.h:115
Pinball::m_button
bool m_button
Definition:
pinball.cpp:167
b2Vec2::SetZero
void SetZero()
Set this vector to all zeros.
Definition:
b2_math.h:50
b2CircleShape
A solid circle shape.
Definition:
b2_circle_shape.h:30
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
GLFW_KEY_A
#define GLFW_KEY_A
Definition:
glfw3.h:378
b2PolygonShape
Definition:
b2_polygon_shape.h:32
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition:
b2_polygon_shape.cpp:36
testIndex
static int testIndex
Definition:
pinball.cpp:170
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
Pinball::KeyboardUp
void KeyboardUp(int key) override
Definition:
pinball.cpp:149
b2Shape::m_radius
float m_radius
Definition:
b2_shape.h:102
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
Pinball::m_leftJoint
b2RevoluteJoint * m_leftJoint
Definition:
pinball.cpp:164
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
Pinball::m_rightJoint
b2RevoluteJoint * m_rightJoint
Definition:
pinball.cpp:165
b2ChainShape::CreateLoop
void CreateLoop(const b2Vec2 *vertices, int32 count)
Definition:
b2_chain_shape.cpp:43
Test::m_textIncrement
int32 m_textIncrement
Definition:
test.h:135
Settings
Definition:
settings.h:25
b2RevoluteJointDef::enableMotor
bool enableMotor
A flag to enable the joint motor.
Definition:
b2_revolute_joint.h:78
Test::m_world
b2World * m_world
Definition:
test.h:128
b2_pi
#define b2_pi
Definition:
b2_common.h:41
Pinball::Keyboard
void Keyboard(int key) override
Definition:
pinball.cpp:139
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
b2RevoluteJointDef::motorSpeed
float motorSpeed
The desired motor speed. Usually in radians per second.
Definition:
b2_revolute_joint.h:81
b2RevoluteJointDef::upperAngle
float upperAngle
The upper angle for the joint limit (radians).
Definition:
b2_revolute_joint.h:75
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
b2RevoluteJointDef::enableLimit
bool enableLimit
A flag to enable joint limits.
Definition:
b2_revolute_joint.h:69
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition:
draw.cpp:772
b2RevoluteJointDef
Definition:
b2_revolute_joint.h:39
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
b2RevoluteJointDef::maxMotorTorque
float maxMotorTorque
Definition:
b2_revolute_joint.h:85
b2JointDef::bodyA
b2Body * bodyA
The first attached body.
Definition:
b2_joint.h:89
g_debugDraw
DebugDraw g_debugDraw
Definition:
draw.cpp:32
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
Pinball::m_ball
b2Body * m_ball
Definition:
pinball.cpp:166
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21