dump_loader.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 // This test holds worlds dumped using b2World::Dump.
26 class DumpLoader : public Test
27 {
28 public:
29 
31  {
32  b2ChainShape chainShape;
33  b2Vec2 vertices[] = {b2Vec2(-5,0), b2Vec2(5,0), b2Vec2(5,5), b2Vec2(4,1), b2Vec2(-4,1), b2Vec2(-5,5)};
34  chainShape.CreateLoop(vertices, 6);
35 
36  b2FixtureDef groundFixtureDef;
37  groundFixtureDef.density = 0;
38  groundFixtureDef.shape = &chainShape;
39 
40  b2BodyDef groundBodyDef;
41  groundBodyDef.type = b2_staticBody;
42 
43  b2Body *groundBody = m_world->CreateBody(&groundBodyDef);
44  b2Fixture *groundBodyFixture = groundBody->CreateFixture(&groundFixtureDef);
45 
46  b2CircleShape ballShape;
47  ballShape.m_radius = 1;
48 
49  b2FixtureDef ballFixtureDef;
50  ballFixtureDef.restitution = 0.75f;
51  ballFixtureDef.density = 1;
52  ballFixtureDef.shape = &ballShape;
53 
54  b2BodyDef ballBodyDef;
55  ballBodyDef.type = b2BodyType::b2_dynamicBody;
56  ballBodyDef.position = b2Vec2(0, 10);
57  // ballBodyDef.angularDamping = 0.2f;
58 
59  m_ball = m_world->CreateBody(&ballBodyDef);
60  b2Fixture *ballFixture = m_ball->CreateFixture(&ballFixtureDef);
61  m_ball->ApplyForceToCenter(b2Vec2(-1000, -400), true);
62  }
63 
64  void Step(Settings& settings) override
65  {
67  float omega = m_ball->GetAngularVelocity();
68 
69  b2MassData massData;
70  m_ball->GetMassData(&massData);
71 
72  float ke = 0.5f * massData.mass * b2Dot(v, v) + 0.5f * massData.I * omega * omega;
73 
74  g_debugDraw.DrawString(5, m_textLine, "kinetic energy = %.6f", ke);
76 
77  Test::Step(settings);
78  }
79 
80  static Test* Create()
81  {
82  return new DumpLoader;
83  }
84 
86 };
87 
88 static int testIndex = RegisterTest("Bugs", "Dump Loader", DumpLoader::Create);
const b2Shape * shape
Definition: b2_fixture.h:76
float mass
The mass of the shape, usually in kilograms.
Definition: b2_shape.h:36
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
float density
The density, usually in kg/m^2.
Definition: b2_fixture.h:92
float b2Dot(const b2Vec2 &a, const b2Vec2 &b)
Perform the dot product on two vectors.
Definition: b2_math.h:395
b2Body * m_ball
Definition: dump_loader.cpp:85
void ApplyForceToCenter(const b2Vec2 &force, bool wake)
Definition: b2_body.h:759
float GetAngularVelocity() const
Definition: b2_body.h:539
int32 m_textLine
Definition: test.h:127
Definition: test.h:80
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
void Step(Settings &settings) override
Definition: dump_loader.cpp:64
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
const b2Vec2 & GetLinearVelocity() const
Definition: b2_body.h:519
void CreateLoop(const b2Vec2 *vertices, int32 count)
void GetMassData(b2MassData *data) const
Definition: b2_body.h:554
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
float I
The rotational inertia of the shape about the local origin.
Definition: b2_shape.h:42
b2Vec2 position
Definition: b2_body.h:78
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
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
This holds the mass data computed for a shape.
Definition: b2_shape.h:33
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
static int testIndex
Definition: dump_loader.cpp:88
static Test * Create()
Definition: dump_loader.cpp:80


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