externals
box2d
testbed
tests
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
:
32
MotorJoint
()
33
{
34
b2Body
* ground =
NULL
;
35
{
36
b2BodyDef
bd;
37
ground =
m_world
->
CreateBody
(&bd);
38
39
b2EdgeShape
shape;
40
shape.
SetTwoSided
(
b2Vec2
(-20.0
f
, 0.0
f
),
b2Vec2
(20.0
f
, 0.0
f
));
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.0
f
, 8.0
f
);
53
b2Body
* body =
m_world
->
CreateBody
(&bd);
54
55
b2PolygonShape
shape;
56
shape.
SetAsBox
(2.0
f
, 0.5
f
);
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;
68
m_joint
= (
b2MotorJoint
*)
m_world
->
CreateJoint
(&mjd);
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.0
f
*
m_time
);
94
linearOffset.
y
= 8.0f + 4.0f * sinf(1.0
f
*
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.0
f
,
b2Color
(0.9
f
, 0.9
f
, 0.9
f
));
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
113
b2MotorJoint
*
m_joint
;
114
float
m_time
;
115
bool
m_go
;
116
};
117
118
static
int
testIndex
=
RegisterTest
(
"Joints"
,
"Motor Joint"
,
MotorJoint::Create
);
b2EdgeShape
Definition:
b2_edge_shape.h:32
b2Vec2::y
float y
Definition:
b2_math.h:128
g_debugDraw
DebugDraw g_debugDraw
Definition:
draw.cpp:32
NULL
#define NULL
testIndex
static int testIndex
Definition:
motor_joint.cpp:118
settings.h
b2MotorJointDef::Initialize
void Initialize(b2Body *bodyA, b2Body *bodyB)
Initialize the bodies and offsets using the current transforms.
Definition:
b2_motor_joint.cpp:42
GLFW_KEY_S
#define GLFW_KEY_S
Definition:
glfw3.h:396
MotorJoint::m_go
bool m_go
Definition:
motor_joint.cpp:115
MotorJoint::Keyboard
void Keyboard(int key) override
Definition:
motor_joint.cpp:75
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
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
MotorJoint::MotorJoint
MotorJoint()
Definition:
motor_joint.cpp:32
MotorJoint::m_joint
b2MotorJoint * m_joint
Definition:
motor_joint.cpp:113
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2World::CreateJoint
b2Joint * CreateJoint(const b2JointDef *def)
Definition:
b2_world.cpp:220
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition:
b2_fixture.h:82
MotorJoint
Definition:
motor_joint.cpp:29
MotorJoint::m_time
float m_time
Definition:
motor_joint.cpp:114
b2_dynamicBody
@ b2_dynamicBody
Definition:
b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition:
draw.cpp:772
Settings::m_hertz
float m_hertz
Definition:
settings.h:64
b2Color
Color for debug drawing. Each value has the range [0,1].
Definition:
b2_draw.h:30
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
b2MotorJoint::SetLinearOffset
void SetLinearOffset(const b2Vec2 &linearOffset)
Set/get the target linear offset, in frame A, in meters.
Definition:
b2_motor_joint.cpp:266
b2BodyDef
Definition:
b2_body.h:52
DebugDraw::DrawPoint
void DrawPoint(const b2Vec2 &p, float size, const b2Color &color) override
Draw a point.
Definition:
draw.cpp:766
Settings
Definition:
settings.h:25
b2FixtureDef::shape
const b2Shape * shape
Definition:
b2_fixture.h:76
b2MotorJointDef::maxForce
float maxForce
The maximum motor force in N.
Definition:
b2_motor_joint.h:52
b2MotorJointDef
Motor joint definition.
Definition:
b2_motor_joint.h:30
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
Test::m_textLine
int32 m_textLine
Definition:
test.h:127
MotorJoint::Create
static Test * Create()
Definition:
motor_joint.cpp:108
b2MotorJoint
Definition:
b2_motor_joint.h:64
b2MotorJointDef::maxTorque
float maxTorque
The maximum motor torque in N-m.
Definition:
b2_motor_joint.h:55
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
Test::m_world
b2World * m_world
Definition:
test.h:128
b2MotorJoint::SetAngularOffset
void SetAngularOffset(float angularOffset)
Set/get the target angular offset, in radians.
Definition:
b2_motor_joint.cpp:281
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
MotorJoint::Step
void Step(Settings &settings) override
Definition:
motor_joint.cpp:85
mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:08