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:
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.0f);
48  b2Vec2 v2(0.0f, 0.0f);
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.5f, 0.0f));
104 
105  m_skier = skier;
106  }
107 
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:
119  if(m_fixed_camera)
120  {
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");
131 
132  if(!m_fixed_camera)
133  {
135  }
136 
137  Test::Step(settings);
138  }
139 
140  static Test* Create()
141  {
142  return new Skier;
143  }
144 
148 };
149 
150 static int testIndex = RegisterTest("Bugs", "Skier", Skier::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
f
static int testIndex
Definition: skier.cpp:150
void Step(Settings &settings) override
Definition: skier.cpp:127
float x
Definition: b2_math.h:128
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
static Test * Create()
Definition: skier.cpp:140
Definition: test.h:80
const b2Vec2 & GetPosition() const
Definition: b2_body.h:484
A 2D column vector.
Definition: b2_math.h:41
float m_platform_width
Definition: skier.cpp:146
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
b2Body * m_skier
Definition: skier.cpp:145
b2Vec2 m_center
Definition: draw.h:53
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
bool m_fixed_camera
Definition: skier.cpp:147
void CreateLoop(const b2Vec2 *vertices, int32 count)
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
#define b2_pi
Definition: b2_common.h:41
float m_zoom
Definition: draw.h:54
b2Vec2 position
Definition: b2_body.h:78
void Set(const b2Vec2 *points, int32 count)
Skier()
Definition: skier.cpp:13
void Keyboard(int key) override
Definition: skier.cpp:113
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
Camera g_camera
Definition: draw.cpp:33
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2_fixture.h:85
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
float friction
The friction coefficient, usually in the range [0,1].
Definition: b2_fixture.h:82
Definition: skier.cpp:10
#define GLFW_KEY_C
Definition: glfw3.h:380


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