shape_editing.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 ShapeEditing : public Test
26 {
27 public:
28 
30  {
31  {
32  b2BodyDef bd;
33  b2Body* ground = m_world->CreateBody(&bd);
34 
35  b2EdgeShape shape;
36  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
37  ground->CreateFixture(&shape, 0.0f);
38  }
39 
40  b2BodyDef bd;
41  bd.type = b2_dynamicBody;
42  bd.position.Set(0.0f, 10.0f);
43  m_body = m_world->CreateBody(&bd);
44 
45  b2PolygonShape shape;
46  shape.SetAsBox(4.0f, 4.0f, b2Vec2(0.0f, 0.0f), 0.0f);
47  m_fixture1 = m_body->CreateFixture(&shape, 10.0f);
48 
49  m_fixture2 = NULL;
50 
51  m_sensor = false;
52  }
53 
54  void Keyboard(int key) override
55  {
56  switch (key)
57  {
58  case GLFW_KEY_C:
59  if (m_fixture2 == NULL)
60  {
61  b2CircleShape shape;
62  shape.m_radius = 3.0f;
63  shape.m_p.Set(0.5f, -4.0f);
64  m_fixture2 = m_body->CreateFixture(&shape, 10.0f);
65  m_body->SetAwake(true);
66  }
67  break;
68 
69  case GLFW_KEY_D:
70  if (m_fixture2 != NULL)
71  {
73  m_fixture2 = NULL;
74  m_body->SetAwake(true);
75  }
76  break;
77 
78  case GLFW_KEY_S:
79  if (m_fixture2 != NULL)
80  {
81  m_sensor = !m_sensor;
83  }
84  break;
85  }
86  }
87 
88  void Step(Settings& settings) override
89  {
90  Test::Step(settings);
91  g_debugDraw.DrawString(5, m_textLine, "Press: (c) create a shape, (d) destroy a shape.");
93  g_debugDraw.DrawString(5, m_textLine, "sensor = %d", m_sensor);
95  }
96 
97  static Test* Create()
98  {
99  return new ShapeEditing;
100  }
101 
105  bool m_sensor;
106 };
107 
108 static int testIndex = RegisterTest("Examples", "Shape Editing", ShapeEditing::Create);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
b2EdgeShape
Definition: b2_edge_shape.h:32
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body::SetAwake
void SetAwake(bool flag)
Definition: b2_body.h:638
NULL
#define NULL
GLFW_KEY_D
#define GLFW_KEY_D
Definition: glfw3.h:381
ShapeEditing::m_fixture2
b2Fixture * m_fixture2
Definition: shape_editing.cpp:104
ShapeEditing::m_body
b2Body * m_body
Definition: shape_editing.cpp:102
GLFW_KEY_S
#define GLFW_KEY_S
Definition: glfw3.h:396
ShapeEditing::Create
static Test * Create()
Definition: shape_editing.cpp:97
b2Fixture::SetSensor
void SetSensor(bool sensor)
Set if this fixture is a sensor.
Definition: b2_fixture.cpp:224
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2CircleShape
A solid circle shape.
Definition: b2_circle_shape.h:30
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2Body::DestroyFixture
void DestroyFixture(b2Fixture *fixture)
Definition: b2_body.cpp:213
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
f
f
ShapeEditing::Keyboard
void Keyboard(int key) override
Definition: shape_editing.cpp:54
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
ShapeEditing::m_sensor
bool m_sensor
Definition: shape_editing.cpp:105
ShapeEditing
Definition: shape_editing.cpp:25
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
b2Shape::m_radius
float m_radius
Definition: b2_shape.h:102
b2Fixture
Definition: b2_fixture.h:116
testIndex
static int testIndex
Definition: shape_editing.cpp:108
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
b2BodyDef
Definition: b2_body.h:52
Settings
Definition: settings.h:25
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2PolygonShape
Definition: b2_polygon_shape.h:32
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
ShapeEditing::ShapeEditing
ShapeEditing()
Definition: shape_editing.cpp:29
Test::m_textLine
int32 m_textLine
Definition: test.h:127
GLFW_KEY_C
#define GLFW_KEY_C
Definition: glfw3.h:380
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
ShapeEditing::Step
void Step(Settings &settings) override
Definition: shape_editing.cpp:88
ShapeEditing::m_fixture1
b2Fixture * m_fixture1
Definition: shape_editing.cpp:103
b2CircleShape::m_p
b2Vec2 m_p
Position.
Definition: b2_circle_shape.h:57
Test::m_world
b2World * m_world
Definition: test.h:128
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08