externals
box2d
testbed
tests
skier.cpp
Go to the documentation of this file.
1
/*
2
Test case for collision/jerking issue.
3
*/
4
5
#include "test.h"
6
7
#include <vector>
8
#include <iostream>
9
10
class
Skier
:
public
Test
11
{
12
public
:
13
Skier
()
14
{
15
b2Body
* ground =
NULL
;
16
{
17
b2BodyDef
bd;
18
ground =
m_world
->
CreateBody
(&bd);
19
20
float
const
PlatformWidth = 8.0f;
21
22
/*
23
First angle is from the horizontal and should be negative for a downward slope.
24
Second angle is relative to the preceding slope, and should be positive, creating a kind of
25
loose 'Z'-shape from the 3 edges.
26
If A1 = -10, then A2 <= ~1.5 will result in the collision glitch.
27
If A1 = -30, then A2 <= ~10.0 will result in the glitch.
28
*/
29
float
const
Angle1Degrees = -30.0f;
30
float
const
Angle2Degrees = 10.0f;
31
32
/*
33
The larger the value of SlopeLength, the less likely the glitch will show up.
34
*/
35
float
const
SlopeLength = 2.0f;
36
37
float
const
SurfaceFriction = 0.2f;
38
39
// Convert to radians
40
float
const
Slope1Incline = -Angle1Degrees *
b2_pi
/ 180.0f;
41
float
const
Slope2Incline = Slope1Incline - Angle2Degrees *
b2_pi
/ 180.0f;
42
//
43
44
m_platform_width
= PlatformWidth;
45
46
// Horizontal platform
47
b2Vec2
v1(-PlatformWidth, 0.0
f
);
48
b2Vec2
v2(0.0
f
, 0.0
f
);
49
b2Vec2
v3(SlopeLength * cosf(Slope1Incline), -SlopeLength * sinf(Slope1Incline));
50
b2Vec2
v4(v3.
x
+ SlopeLength * cosf(Slope2Incline), v3.
y
- SlopeLength * sinf(Slope2Incline));
51
b2Vec2
v5(v4.
x
, v4.
y
- 1.0f);
52
53
b2Vec2
vertices[5] = { v5, v4, v3, v2, v1 };
54
55
b2ChainShape
shape;
56
shape.
CreateLoop
(vertices, 5);
57
b2FixtureDef
fd;
58
fd.
shape
= &shape;
59
fd.
density
= 0.0f;
60
fd.
friction
= SurfaceFriction;
61
62
ground->
CreateFixture
(&fd);
63
}
64
65
{
66
float
const
BodyWidth = 1.0f;
67
float
const
BodyHeight = 2.5f;
68
float
const
SkiLength = 3.0f;
69
70
/*
71
Larger values for this seem to alleviate the issue to some extent.
72
*/
73
float
const
SkiThickness = 0.3f;
74
75
float
const
SkiFriction = 0.0f;
76
float
const
SkiRestitution = 0.15f;
77
78
b2BodyDef
bd;
79
bd.
type
=
b2_dynamicBody
;
80
81
float
initial_y = BodyHeight / 2 + SkiThickness;
82
bd.
position
.
Set
(-
m_platform_width
/ 2, initial_y);
83
84
b2Body
* skier =
m_world
->
CreateBody
(&bd);
85
86
b2PolygonShape
ski;
87
b2Vec2
verts[4];
88
verts[0].
Set
(-SkiLength / 2 - SkiThickness, -BodyHeight / 2);
89
verts[1].
Set
(-SkiLength / 2, -BodyHeight / 2 - SkiThickness);
90
verts[2].
Set
(SkiLength / 2, -BodyHeight / 2 - SkiThickness);
91
verts[3].
Set
(SkiLength / 2 + SkiThickness, -BodyHeight / 2);
92
ski.
Set
(verts, 4);
93
94
b2FixtureDef
fd;
95
fd.
density
= 1.0f;
96
97
fd.
friction
= SkiFriction;
98
fd.
restitution
= SkiRestitution;
99
100
fd.
shape
= &ski;
101
skier->
CreateFixture
(&fd);
102
103
skier->
SetLinearVelocity
(
b2Vec2
(0.5
f
, 0.0
f
));
104
105
m_skier
= skier;
106
}
107
108
g_camera
.
m_center
=
b2Vec2
(
m_platform_width
/ 2.0
f
, 0.0
f
);
109
g_camera
.
m_zoom
= 0.4f;
110
m_fixed_camera
=
true
;
111
}
112
113
void
Keyboard
(
int
key)
override
114
{
115
switch
(key)
116
{
117
case
GLFW_KEY_C
:
118
m_fixed_camera
= !
m_fixed_camera
;
119
if
(
m_fixed_camera
)
120
{
121
g_camera
.
m_center
=
b2Vec2
(
m_platform_width
/ 2.0
f
, 0.0
f
);
122
}
123
break
;
124
}
125
}
126
127
void
Step
(
Settings
& settings)
override
128
{
129
g_debugDraw
.
DrawString
(5,
m_textLine
,
"Keys: c = Camera fixed/tracking"
);
130
m_textLine
+=
m_textIncrement
;
131
132
if
(!
m_fixed_camera
)
133
{
134
g_camera
.
m_center
=
m_skier
->
GetPosition
();
135
}
136
137
Test::Step
(settings);
138
}
139
140
static
Test
*
Create
()
141
{
142
return
new
Skier
;
143
}
144
145
b2Body
*
m_skier
;
146
float
m_platform_width
;
147
bool
m_fixed_camera
;
148
};
149
150
static
int
testIndex
=
RegisterTest
(
"Bugs"
,
"Skier"
,
Skier::Create
);
Test::m_textIncrement
int32 m_textIncrement
Definition:
test.h:135
b2Vec2::y
float y
Definition:
b2_math.h:128
Skier::Step
void Step(Settings &settings) override
Definition:
skier.cpp:127
g_debugDraw
DebugDraw g_debugDraw
Definition:
draw.cpp:32
g_camera
Camera g_camera
Definition:
draw.cpp:33
NULL
#define NULL
Camera::m_center
b2Vec2 m_center
Definition:
draw.h:53
b2ChainShape
Definition:
b2_chain_shape.h:36
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
b2FixtureDef
Definition:
b2_fixture.h:61
Skier
Definition:
skier.cpp:10
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
testIndex
static int testIndex
Definition:
skier.cpp:150
Skier::Create
static Test * Create()
Definition:
skier.cpp:140
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
b2Body::SetLinearVelocity
void SetLinearVelocity(const b2Vec2 &v)
Definition:
b2_body.h:504
b2PolygonShape::Set
void Set(const b2Vec2 *points, int32 count)
Definition:
b2_polygon_shape.cpp:118
f
f
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2ChainShape::CreateLoop
void CreateLoop(const b2Vec2 *vertices, int32 count)
Definition:
b2_chain_shape.cpp:43
b2_pi
#define b2_pi
Definition:
b2_common.h:41
b2FixtureDef::restitution
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition:
b2_fixture.h:85
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition:
b2_fixture.h:82
b2_dynamicBody
@ b2_dynamicBody
Definition:
b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition:
draw.cpp:772
Skier::m_fixed_camera
bool m_fixed_camera
Definition:
skier.cpp:147
Skier::m_platform_width
float m_platform_width
Definition:
skier.cpp:146
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition:
b2_fixture.h:92
Skier::m_skier
b2Body * m_skier
Definition:
skier.cpp:145
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
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
Skier::Keyboard
void Keyboard(int key) override
Definition:
skier.cpp:113
GLFW_KEY_C
#define GLFW_KEY_C
Definition:
glfw3.h:380
Skier::Skier
Skier()
Definition:
skier.cpp:13
Test
Definition:
test.h:80
b2Body::GetPosition
const b2Vec2 & GetPosition() const
Definition:
b2_body.h:484
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
Camera::m_zoom
float m_zoom
Definition:
draw.h:54
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
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