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 "box2d/box2d.h"
24 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
25 #include "doctest.h"
26 #include <stdio.h>
27 
28 // This is a simple example of building and running a simulation
29 // using Box2D. Here we create a large ground box and a small dynamic
30 // box.
31 // There are no graphics for this example. Box2D is meant to be used
32 // with your rendering engine in your game engine.
33 DOCTEST_TEST_CASE("hello world")
34 {
35 	// Define the gravity vector.
36 	b2Vec2 gravity(0.0f, -10.0f);
37 
38 	// Construct a world object, which will hold and simulate the rigid bodies.
39 	b2World world(gravity);
40 
41 	// Define the ground body.
42 	b2BodyDef groundBodyDef;
43 	groundBodyDef.position.Set(0.0f, -10.0f);
44 
45 	// Call the body factory which allocates memory for the ground body
46 	// from a pool and creates the ground box shape (also from a pool).
47 	// The body is also added to the world.
48 	b2Body* groundBody = world.CreateBody(&groundBodyDef);
49 
50 	// Define the ground box shape.
51 	b2PolygonShape groundBox;
52 
53 	// The extents are the half-widths of the box.
54 	groundBox.SetAsBox(50.0f, 10.0f);
55 
56 	// Add the ground fixture to the ground body.
57 	groundBody->CreateFixture(&groundBox, 0.0f);
58 
59 	// Define the dynamic body. We set its position and call the body factory.
60 	b2BodyDef bodyDef;
61 	bodyDef.type = b2_dynamicBody;
62 	bodyDef.position.Set(0.0f, 4.0f);
63 	b2Body* body = world.CreateBody(&bodyDef);
64 
65 	// Define another box shape for our dynamic body.
66 	b2PolygonShape dynamicBox;
67 	dynamicBox.SetAsBox(1.0f, 1.0f);
68 
69 	// Define the dynamic body fixture.
70 	b2FixtureDef fixtureDef;
71 	fixtureDef.shape = &dynamicBox;
72 
73 	// Set the box density to be non-zero, so it will be dynamic.
74 	fixtureDef.density = 1.0f;
75 
76 	// Override the default friction.
77 	fixtureDef.friction = 0.3f;
78 
79 	// Add the shape to the body.
80 	body->CreateFixture(&fixtureDef);
81 
82 	// Prepare for simulation. Typically we use a time step of 1/60 of a
83 	// second (60Hz) and 10 iterations. This provides a high quality simulation
84 	// in most game scenarios.
85 	float timeStep = 1.0f / 60.0f;
86 	int32 velocityIterations = 6;
87 	int32 positionIterations = 2;
88 
89 	b2Vec2 position = body->GetPosition();
90 	float angle = body->GetAngle();
91 
92 	// This is our little game loop.
93 	for (int32 i = 0; i < 60; ++i)
94 	{
95 		// Instruct the world to perform a single step of simulation.
96 		// It is generally best to keep the time step and iterations fixed.
97 		world.Step(timeStep, velocityIterations, positionIterations);
98 
99 		// Now print the position and angle of the body.
100 		position = body->GetPosition();
101 		angle = body->GetAngle();
102 
103 		printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
104 	}
105 
106 	// When the world destructor is called, all bodies and joints are freed. This can
107 	// create orphaned pointers, so be careful about your world management.
108 
109 	CHECK(b2Abs(position.x) < 0.01f);
110 	CHECK(b2Abs(position.y - 1.01f) < 0.01f);
111 	CHECK(b2Abs(angle) < 0.01f);
112 }
113