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  {
34  };
35 
37  {
38  // Ground
39  {
40  b2BodyDef bd;
41  b2Body* ground = m_world->CreateBody(&bd);
42 
43  b2EdgeShape shape;
44  shape.SetTwoSided(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
45  ground->CreateFixture(&shape, 0.0f);
46  }
47 
48  // Platform
49  {
50  b2BodyDef bd;
51  bd.position.Set(0.0f, 10.0f);
52  b2Body* body = m_world->CreateBody(&bd);
53 
54  b2PolygonShape shape;
55  shape.SetAsBox(3.0f, 0.5f);
56  m_platform = body->CreateFixture(&shape, 0.0f);
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.0f, 12.0f);
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.0f);
73 
74  body->SetLinearVelocity(b2Vec2(0.0f, -50.0f));
75 
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
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 
118  g_debugDraw.DrawString(5, m_textLine, "Character Linear Velocity: %f", v.y);
120  }
121 
122  static Test* Create()
123  {
124  return new Platformer;
125  }
126 
131 };
132 
133 static int testIndex = RegisterTest("Examples", "Platformer", Platformer::Create);
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
float m_bottom
Definition: platformer.cpp:127
b2Fixture * m_character
Definition: platformer.cpp:130
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
void SetEnabled(bool flag)
Definition: b2_contact.h:264
b2Fixture * GetFixtureB()
Get fixture B in this contact.
Definition: b2_contact.h:306
Definition: test.h:80
float m_radius
Definition: platformer.cpp:127
static Test * Create()
Definition: platformer.cpp:122
#define b2_linearSlop
Definition: b2_common.h:65
const b2Vec2 & GetPosition() const
Definition: b2_body.h:484
void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition: platformer.cpp:80
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float m_radius
Definition: b2_shape.h:102
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
const b2Vec2 & GetLinearVelocity() const
Definition: b2_body.h:519
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
b2Fixture * m_platform
Definition: platformer.cpp:129
b2Fixture * GetFixtureA()
Get fixture A in this contact.
Definition: b2_contact.h:296
b2Vec2 position
Definition: b2_body.h:78
State m_state
Definition: platformer.cpp:128
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
static int testIndex
Definition: platformer.cpp:133
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
void Step(Settings &settings) override
Definition: platformer.cpp:113
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2Body * GetBody()
Definition: b2_fixture.h:283
virtual void PreSolve(b2Contact *contact, const b2Manifold *oldManifold) override
Definition: test.cpp:73


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