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:
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.0f, 6.0f);
40  vs[1].Set(-8.0f, 20.0f);
41  vs[2].Set(8.0f, 20.0f);
42  vs[3].Set(8.0f, 6.0f);
43  vs[4].Set(0.0f, -2.0f);
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.0f, 0.0f), p2(2.0f, 0.0f);
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.75f, 0.1f);
68 
69  b2FixtureDef fd;
70  fd.shape = &box;
71  fd.density = 1.0f;
72 
73  leftFlipper->CreateFixture(&fd);
74  rightFlipper->CreateFixture(&fd);
75 
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;
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;
96  }
97 
98  // Circle character
99  {
100  b2BodyDef bd;
101  bd.position.Set(1.0f, 15.0f);
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  {
125  }
126  else
127  {
128  m_leftJoint->SetMotorSpeed(-10.0f);
130  }
131 
132  Test::Step(settings);
133 
134  g_debugDraw.DrawString(5, m_textLine, "Press 'a' to control the flippers");
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 
167  bool m_button;
168 };
169 
170 static int testIndex = RegisterTest("Examples", "Pinball", Pinball::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
float lowerAngle
The lower angle for the joint limit (radians).
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
void Step(Settings &settings) override
Definition: pinball.cpp:119
Pinball()
Definition: pinball.cpp:30
void SetMotorSpeed(float speed)
Set the motor speed in radians per second.
f
int32 m_textLine
Definition: test.h:127
static Test * Create()
Definition: pinball.cpp:159
Definition: test.h:80
bool bullet
Definition: b2_body.h:115
bool m_button
Definition: pinball.cpp:167
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
#define GLFW_KEY_A
Definition: glfw3.h:378
void SetAsBox(float hx, float hy)
static int testIndex
Definition: pinball.cpp:170
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
void KeyboardUp(int key) override
Definition: pinball.cpp:149
float m_radius
Definition: b2_shape.h:102
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
b2RevoluteJoint * m_leftJoint
Definition: pinball.cpp:164
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2RevoluteJoint * m_rightJoint
Definition: pinball.cpp:165
void CreateLoop(const b2Vec2 *vertices, int32 count)
int32 m_textIncrement
Definition: test.h:135
bool enableMotor
A flag to enable the joint motor.
b2World * m_world
Definition: test.h:128
#define b2_pi
Definition: b2_common.h:41
void Keyboard(int key) override
Definition: pinball.cpp:139
b2Vec2 position
Definition: b2_body.h:78
float motorSpeed
The desired motor speed. Usually in radians per second.
float upperAngle
The upper angle for the joint limit (radians).
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
bool enableLimit
A flag to enable joint limits.
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92
b2Body * m_ball
Definition: pinball.cpp:166


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21