friction.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 Friction : public Test
26 {
27 public:
28 
30  {
31  {
32  b2BodyDef bd;
33  b2Body* ground = m_world->CreateBody(&bd);
34 
35  b2EdgeShape shape;
36  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
37  ground->CreateFixture(&shape, 0.0f);
38  }
39 
40  {
41  b2PolygonShape shape;
42  shape.SetAsBox(13.0f, 0.25f);
43 
44  b2BodyDef bd;
45  bd.position.Set(-4.0f, 22.0f);
46  bd.angle = -0.25f;
47 
48  b2Body* ground = m_world->CreateBody(&bd);
49  ground->CreateFixture(&shape, 0.0f);
50  }
51 
52  {
53  b2PolygonShape shape;
54  shape.SetAsBox(0.25f, 1.0f);
55 
56  b2BodyDef bd;
57  bd.position.Set(10.5f, 19.0f);
58 
59  b2Body* ground = m_world->CreateBody(&bd);
60  ground->CreateFixture(&shape, 0.0f);
61  }
62 
63  {
64  b2PolygonShape shape;
65  shape.SetAsBox(13.0f, 0.25f);
66 
67  b2BodyDef bd;
68  bd.position.Set(4.0f, 14.0f);
69  bd.angle = 0.25f;
70 
71  b2Body* ground = m_world->CreateBody(&bd);
72  ground->CreateFixture(&shape, 0.0f);
73  }
74 
75  {
76  b2PolygonShape shape;
77  shape.SetAsBox(0.25f, 1.0f);
78 
79  b2BodyDef bd;
80  bd.position.Set(-10.5f, 11.0f);
81 
82  b2Body* ground = m_world->CreateBody(&bd);
83  ground->CreateFixture(&shape, 0.0f);
84  }
85 
86  {
87  b2PolygonShape shape;
88  shape.SetAsBox(13.0f, 0.25f);
89 
90  b2BodyDef bd;
91  bd.position.Set(-4.0f, 6.0f);
92  bd.angle = -0.25f;
93 
94  b2Body* ground = m_world->CreateBody(&bd);
95  ground->CreateFixture(&shape, 0.0f);
96  }
97 
98  {
99  b2PolygonShape shape;
100  shape.SetAsBox(0.5f, 0.5f);
101 
102  b2FixtureDef fd;
103  fd.shape = &shape;
104  fd.density = 25.0f;
105 
106  float friction[5] = {0.75f, 0.5f, 0.35f, 0.1f, 0.0f};
107 
108  for (int i = 0; i < 5; ++i)
109  {
110  b2BodyDef bd;
111  bd.type = b2_dynamicBody;
112  bd.position.Set(-15.0f + 4.0f * i, 28.0f);
113  b2Body* body = m_world->CreateBody(&bd);
114 
115  fd.friction = friction[i];
116  body->CreateFixture(&fd);
117  }
118  }
119  }
120 
121  static Test* Create()
122  {
123  return new Friction;
124  }
125 };
126 
127 static int testIndex = RegisterTest("Forces", "Friction", Friction::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
static Test * Create()
Definition: friction.cpp:121
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
Definition: test.h:80
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.
void SetAsBox(float hx, float hy)
static int testIndex
Definition: friction.cpp:127
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
Friction()
Definition: friction.cpp:29
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
float angle
The world angle of the body in radians.
Definition: b2_body.h:81
b2Vec2 position
Definition: b2_body.h:78
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
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


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