HelloWorld.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #include <Box2D/Box2D.h>
20 
21 #include <stdio.h>
22 
23 // This is a simple example of building and running a simulation
24 // using Box2D. Here we create a large ground box and a small dynamic
25 // box.
26 // There are no graphics for this example. Box2D is meant to be used
27 // with your rendering engine in your game engine.
28 int main(int argc, char** argv)
29 {
30  B2_NOT_USED(argc);
31  B2_NOT_USED(argv);
32 
33  // Define the gravity vector.
34  b2Vec2 gravity(0.0f, -10.0f);
35 
36  // Construct a world object, which will hold and simulate the rigid bodies.
37  b2World world(gravity);
38 
39  // Define the ground body.
40  b2BodyDef groundBodyDef;
41  groundBodyDef.position.Set(0.0f, -10.0f);
42 
43  // Call the body factory which allocates memory for the ground body
44  // from a pool and creates the ground box shape (also from a pool).
45  // The body is also added to the world.
46  b2Body* groundBody = world.CreateBody(&groundBodyDef);
47 
48  // Define the ground box shape.
49  b2PolygonShape groundBox;
50 
51  // The extents are the half-widths of the box.
52  groundBox.SetAsBox(50.0f, 10.0f);
53 
54  // Add the ground fixture to the ground body.
55  groundBody->CreateFixture(&groundBox, 0.0f);
56 
57  // Define the dynamic body. We set its position and call the body factory.
58  b2BodyDef bodyDef;
59  bodyDef.type = b2_dynamicBody;
60  bodyDef.position.Set(0.0f, 4.0f);
61  b2Body* body = world.CreateBody(&bodyDef);
62 
63  // Define another box shape for our dynamic body.
64  b2PolygonShape dynamicBox;
65  dynamicBox.SetAsBox(1.0f, 1.0f);
66 
67  // Define the dynamic body fixture.
68  b2FixtureDef fixtureDef;
69  fixtureDef.shape = &dynamicBox;
70 
71  // Set the box density to be non-zero, so it will be dynamic.
72  fixtureDef.density = 1.0f;
73 
74  // Override the default friction.
75  fixtureDef.friction = 0.3f;
76 
77  // Add the shape to the body.
78  body->CreateFixture(&fixtureDef);
79 
80  // Prepare for simulation. Typically we use a time step of 1/60 of a
81  // second (60Hz) and 10 iterations. This provides a high quality simulation
82  // in most game scenarios.
83  float32 timeStep = 1.0f / 60.0f;
84  int32 velocityIterations = 6;
85  int32 positionIterations = 2;
86 
87  // This is our little game loop.
88  for (int32 i = 0; i < 60; ++i)
89  {
90  // Instruct the world to perform a single step of simulation.
91  // It is generally best to keep the time step and iterations fixed.
92  world.Step(timeStep, velocityIterations, positionIterations);
93 
94  // Now print the position and angle of the body.
95  b2Vec2 position = body->GetPosition();
96  float32 angle = body->GetAngle();
97 
98  printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
99  }
100 
101  // When the world destructor is called, all bodies and joints are freed. This can
102  // create orphaned pointers, so be careful about your world management.
103 
104  return 0;
105 }
const b2Shape * shape
Definition: b2Fixture.h:71
#define printf
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2World.cpp:897
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2Body.cpp:166
#define B2_NOT_USED(x)
Definition: b2Settings.h:26
float32 GetAngle() const
Definition: b2Body.h:484
A 2D column vector.
Definition: b2Math.h:52
int main(int argc, char **argv)
Definition: HelloWorld.cpp:28
signed int int32
Definition: b2Settings.h:31
b2BodyType type
Definition: b2Body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
float32 density
The density, usually in kg/m^2.
Definition: b2Fixture.h:83
void SetAsBox(float32 hx, float32 hy)
float32 y
Definition: b2Math.h:139
b2Vec2 position
Definition: b2Body.h:78
const b2Vec2 & GetPosition() const
Definition: b2Body.h:479
GLdouble angle
float32 x
Definition: b2Math.h:139
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:64
float32 friction
The friction coefficient, usually in the range [0,1].
Definition: b2Fixture.h:77
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2World.cpp:107
float float32
Definition: b2Settings.h:35
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51