body_types.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 BodyTypes : public Test
26 {
27 public:
29  {
30  b2Body* ground = NULL;
31  {
32  b2BodyDef bd;
33  ground = m_world->CreateBody(&bd);
34 
35  b2EdgeShape shape;
36  shape.SetTwoSided(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
37 
38  b2FixtureDef fd;
39  fd.shape = &shape;
40 
41  ground->CreateFixture(&fd);
42  }
43 
44  // Define attachment
45  {
46  b2BodyDef bd;
47  bd.type = b2_dynamicBody;
48  bd.position.Set(0.0f, 3.0f);
50 
51  b2PolygonShape shape;
52  shape.SetAsBox(0.5f, 2.0f);
53  m_attachment->CreateFixture(&shape, 2.0f);
54  }
55 
56  // Define platform
57  {
58  b2BodyDef bd;
59  bd.type = b2_dynamicBody;
60  bd.position.Set(-4.0f, 5.0f);
62 
63  b2PolygonShape shape;
64  shape.SetAsBox(0.5f, 4.0f, b2Vec2(4.0f, 0.0f), 0.5f * b2_pi);
65 
66  b2FixtureDef fd;
67  fd.shape = &shape;
68  fd.friction = 0.6f;
69  fd.density = 2.0f;
71 
73  rjd.Initialize(m_attachment, m_platform, b2Vec2(0.0f, 5.0f));
74  rjd.maxMotorTorque = 50.0f;
75  rjd.enableMotor = true;
76  m_world->CreateJoint(&rjd);
77 
79  pjd.Initialize(ground, m_platform, b2Vec2(0.0f, 5.0f), b2Vec2(1.0f, 0.0f));
80 
81  pjd.maxMotorForce = 1000.0f;
82  pjd.enableMotor = true;
83  pjd.lowerTranslation = -10.0f;
84  pjd.upperTranslation = 10.0f;
85  pjd.enableLimit = true;
86 
87  m_world->CreateJoint(&pjd);
88 
89  m_speed = 3.0f;
90  }
91 
92  // Create a payload
93  {
94  b2BodyDef bd;
95  bd.type = b2_dynamicBody;
96  bd.position.Set(0.0f, 8.0f);
97  b2Body* body = m_world->CreateBody(&bd);
98 
99  b2PolygonShape shape;
100  shape.SetAsBox(0.75f, 0.75f);
101 
102  b2FixtureDef fd;
103  fd.shape = &shape;
104  fd.friction = 0.6f;
105  fd.density = 2.0f;
106 
107  body->CreateFixture(&fd);
108  }
109  }
110 
111  void Keyboard(int key) override
112  {
113  switch (key)
114  {
115  case GLFW_KEY_D:
117  break;
118 
119  case GLFW_KEY_S:
121  break;
122 
123  case GLFW_KEY_K:
127  break;
128  }
129  }
130 
131  void Step(Settings& settings) override
132  {
133  // Drive the kinematic body.
135  {
138 
139  if ((p.x < -10.0f && v.x < 0.0f) ||
140  (p.x > 10.0f && v.x > 0.0f))
141  {
142  v.x = -v.x;
144  }
145  }
146 
147  Test::Step(settings);
148 
149  g_debugDraw.DrawString(5, m_textLine, "Keys: (d) dynamic, (s) static, (k) kinematic");
151  }
152 
153  static Test* Create()
154  {
155  return new BodyTypes;
156  }
157 
160  float m_speed;
161 };
162 
163 static int testIndex = RegisterTest("Examples", "Body Types", BodyTypes::Create);
Test::m_textIncrement
int32 m_textIncrement
Definition: test.h:135
BodyTypes::m_platform
b2Body * m_platform
Definition: body_types.cpp:159
b2EdgeShape
Definition: b2_edge_shape.h:32
b2Body::GetLinearVelocity
const b2Vec2 & GetLinearVelocity() const
Definition: b2_body.h:519
g_debugDraw
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2RevoluteJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
Definition: b2_revolute_joint.cpp:41
b2Body::GetType
b2BodyType GetType() const
Get the type of this body.
Definition: b2_body.h:474
NULL
#define NULL
GLFW_KEY_D
#define GLFW_KEY_D
Definition: glfw3.h:381
GLFW_KEY_K
#define GLFW_KEY_K
Definition: glfw3.h:388
b2Body::SetType
void SetType(b2BodyType type)
Set the type of this body. This may alter the mass and velocity.
Definition: b2_body.cpp:111
b2PrismaticJointDef::lowerTranslation
float lowerTranslation
The lower translation limit, usually in meters.
Definition: b2_prismatic_joint.h:72
GLFW_KEY_S
#define GLFW_KEY_S
Definition: glfw3.h:396
b2Transform::p
b2Vec2 p
Definition: b2_math.h:360
b2_staticBody
@ b2_staticBody
Definition: b2_body.h:45
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2FixtureDef
Definition: b2_fixture.h:61
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::SetAngularVelocity
void SetAngularVelocity(float omega)
Definition: b2_body.h:524
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
b2Body::SetLinearVelocity
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
b2RevoluteJointDef
Definition: b2_revolute_joint.h:39
f
f
BodyTypes::Create
static Test * Create()
Definition: body_types.cpp:153
b2BodyDef::type
b2BodyType type
Definition: b2_body.h:74
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
b2RevoluteJointDef::enableMotor
bool enableMotor
A flag to enable the joint motor.
Definition: b2_revolute_joint.h:78
BodyTypes
Definition: body_types.cpp:25
b2_pi
#define b2_pi
Definition: b2_common.h:41
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
BodyTypes::m_speed
float m_speed
Definition: body_types.cpp:160
b2_dynamicBody
@ b2_dynamicBody
Definition: b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
BodyTypes::BodyTypes
BodyTypes()
Definition: body_types.cpp:28
BodyTypes::m_attachment
b2Body * m_attachment
Definition: body_types.cpp:158
b2PrismaticJointDef::enableLimit
bool enableLimit
Enable/disable the joint limit.
Definition: b2_prismatic_joint.h:69
b2PrismaticJointDef
Definition: b2_prismatic_joint.h:35
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition: b2_polygon_shape.cpp:36
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
b2Vec2::x
float x
Definition: b2_math.h:128
b2BodyDef
Definition: b2_body.h:52
Settings
Definition: settings.h:25
b2FixtureDef::shape
const b2Shape * shape
Definition: b2_fixture.h:76
BodyTypes::Step
void Step(Settings &settings) override
Definition: body_types.cpp:131
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
testIndex
static int testIndex
Definition: body_types.cpp:163
b2PolygonShape
Definition: b2_polygon_shape.h:32
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
Test::m_textLine
int32 m_textLine
Definition: test.h:127
b2PrismaticJointDef::upperTranslation
float upperTranslation
The upper translation limit, usually in meters.
Definition: b2_prismatic_joint.h:75
Test
Definition: test.h:80
b2BodyDef::position
b2Vec2 position
Definition: b2_body.h:78
b2PrismaticJointDef::enableMotor
bool enableMotor
Enable/disable the joint motor.
Definition: b2_prismatic_joint.h:78
Test::Step
virtual void Step(Settings &settings)
Definition: test.cpp:278
b2Body::GetTransform
const b2Transform & GetTransform() const
Definition: b2_body.h:479
b2PrismaticJointDef::maxMotorForce
float maxMotorForce
The maximum motor torque, usually in N-m.
Definition: b2_prismatic_joint.h:81
BodyTypes::Keyboard
void Keyboard(int key) override
Definition: body_types.cpp:111
b2RevoluteJointDef::maxMotorTorque
float maxMotorTorque
Definition: b2_revolute_joint.h:85
Test::m_world
b2World * m_world
Definition: test.h:128
b2PrismaticJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor, const b2Vec2 &axis)
Definition: b2_prismatic_joint.cpp:73
b2_kinematicBody
@ b2_kinematicBody
Definition: b2_body.h:46
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165


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