motor_joint.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 "settings.h"
24 #include "test.h"
25 
29 class MotorJoint : public Test
30 {
31 public:
33  {
34  b2Body* ground = NULL;
35  {
36  b2BodyDef bd;
37  ground = m_world->CreateBody(&bd);
38 
39  b2EdgeShape shape;
40  shape.SetTwoSided(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
41 
42  b2FixtureDef fd;
43  fd.shape = &shape;
44 
45  ground->CreateFixture(&fd);
46  }
47 
48  // Define motorized body
49  {
50  b2BodyDef bd;
51  bd.type = b2_dynamicBody;
52  bd.position.Set(0.0f, 8.0f);
53  b2Body* body = m_world->CreateBody(&bd);
54 
55  b2PolygonShape shape;
56  shape.SetAsBox(2.0f, 0.5f);
57 
58  b2FixtureDef fd;
59  fd.shape = &shape;
60  fd.friction = 0.6f;
61  fd.density = 2.0f;
62  body->CreateFixture(&fd);
63 
64  b2MotorJointDef mjd;
65  mjd.Initialize(ground, body);
66  mjd.maxForce = 1000.0f;
67  mjd.maxTorque = 1000.0f;
69  }
70 
71  m_go = false;
72  m_time = 0.0f;
73  }
74 
75  void Keyboard(int key) override
76  {
77  switch (key)
78  {
79  case GLFW_KEY_S:
80  m_go = !m_go;
81  break;
82  }
83  }
84 
85  void Step(Settings& settings) override
86  {
87  if (m_go && settings.m_hertz > 0.0f)
88  {
89  m_time += 1.0f / settings.m_hertz;
90  }
91 
92  b2Vec2 linearOffset;
93  linearOffset.x = 6.0f * sinf(2.0f * m_time);
94  linearOffset.y = 8.0f + 4.0f * sinf(1.0f * m_time);
95 
96  float angularOffset = 4.0f * m_time;
97 
98  m_joint->SetLinearOffset(linearOffset);
99  m_joint->SetAngularOffset(angularOffset);
100 
101  g_debugDraw.DrawPoint(linearOffset, 4.0f, b2Color(0.9f, 0.9f, 0.9f));
102 
103  Test::Step(settings);
104  g_debugDraw.DrawString(5, m_textLine, "Keys: (s) pause");
105  m_textLine += 15;
106  }
107 
108  static Test* Create()
109  {
110  return new MotorJoint;
111  }
112 
114  float m_time;
115  bool m_go;
116 };
117 
118 static int testIndex = RegisterTest("Joints", "Motor Joint", MotorJoint::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
void SetAngularOffset(float angularOffset)
Set/get the target angular offset, in radians.
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
f
void DrawPoint(const b2Vec2 &p, float size, const b2Color &color) override
Draw a point.
Definition: draw.cpp:766
void Initialize(b2Body *bodyA, b2Body *bodyB)
Initialize the bodies and offsets using the current transforms.
float x
Definition: b2_math.h:128
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
float m_hertz
Definition: settings.h:64
Definition: test.h:80
static Test * Create()
static int testIndex
float maxForce
The maximum motor force in N.
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.
Color for debug drawing. Each value has the range [0,1].
Definition: b2_draw.h:30
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
void Step(Settings &settings) override
Definition: motor_joint.cpp:85
b2Joint * CreateJoint(const b2JointDef *def)
Definition: b2_world.cpp:220
#define GLFW_KEY_S
Definition: glfw3.h:396
void SetLinearOffset(const b2Vec2 &linearOffset)
Set/get the target linear offset, in frame A, in meters.
float maxTorque
The maximum motor torque in N-m.
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
b2World * m_world
Definition: test.h:128
b2Vec2 position
Definition: b2_body.h:78
void Keyboard(int key) override
Definition: motor_joint.cpp:75
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
b2MotorJoint * m_joint
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
Motor joint definition.
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82


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