restitution.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 // Note: even with a restitution of 1.0, there is some energy change
26 // due to position correction.
27 class Restitution : public Test
28 {
29 public:
30 
32  {
33  const float threshold = 10.0f;
34 
35  {
36  b2BodyDef bd;
37  b2Body* ground = m_world->CreateBody(&bd);
38 
39  b2EdgeShape shape;
40  shape.SetTwoSided(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
41 
42  b2FixtureDef fd;
43  fd.shape = &shape;
44  fd.restitutionThreshold = threshold;
45  ground->CreateFixture(&fd);
46  }
47 
48  {
49  b2CircleShape shape;
50  shape.m_radius = 1.0f;
51 
52  b2FixtureDef fd;
53  fd.shape = &shape;
54  fd.density = 1.0f;
55 
56  float restitution[7] = { 0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f };
57 
58  for (int32 i = 0; i < 7; ++i)
59  {
60  b2BodyDef bd;
61  bd.type = b2_dynamicBody;
62  bd.position.Set(-10.0f + 3.0f * i, 20.0f);
63 
64  b2Body* body = m_world->CreateBody(&bd);
65 
66  fd.restitution = restitution[i];
67  fd.restitutionThreshold = threshold;
68  body->CreateFixture(&fd);
69  }
70  }
71  }
72 
73  static Test* Create()
74  {
75  return new Restitution;
76  }
77 };
78 
79 static int testIndex = RegisterTest("Forces", "Restitution", Restitution::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
Definition: test.h:80
static Test * Create()
Definition: restitution.cpp:73
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
static int testIndex
Definition: restitution.cpp:79
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float m_radius
Definition: b2_shape.h:102
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
float restitutionThreshold
Definition: b2_fixture.h:89
b2World * m_world
Definition: test.h:128
b2Vec2 position
Definition: b2_body.h:78
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
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


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