HelloWorld.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #include <Box2D/Box2D.h>
00020 
00021 #include <stdio.h>
00022 
00023 // This is a simple example of building and running a simulation
00024 // using Box2D. Here we create a large ground box and a small dynamic
00025 // box.
00026 // There are no graphics for this example. Box2D is meant to be used
00027 // with your rendering engine in your game engine.
00028 int main(int argc, char** argv)
00029 {
00030         B2_NOT_USED(argc);
00031         B2_NOT_USED(argv);
00032 
00033         // Define the gravity vector.
00034         b2Vec2 gravity(0.0f, -10.0f);
00035 
00036         // Construct a world object, which will hold and simulate the rigid bodies.
00037         b2World world(gravity);
00038 
00039         // Define the ground body.
00040         b2BodyDef groundBodyDef;
00041         groundBodyDef.position.Set(0.0f, -10.0f);
00042 
00043         // Call the body factory which allocates memory for the ground body
00044         // from a pool and creates the ground box shape (also from a pool).
00045         // The body is also added to the world.
00046         b2Body* groundBody = world.CreateBody(&groundBodyDef);
00047 
00048         // Define the ground box shape.
00049         b2PolygonShape groundBox;
00050 
00051         // The extents are the half-widths of the box.
00052         groundBox.SetAsBox(50.0f, 10.0f);
00053 
00054         // Add the ground fixture to the ground body.
00055         groundBody->CreateFixture(&groundBox, 0.0f);
00056 
00057         // Define the dynamic body. We set its position and call the body factory.
00058         b2BodyDef bodyDef;
00059         bodyDef.type = b2_dynamicBody;
00060         bodyDef.position.Set(0.0f, 4.0f);
00061         b2Body* body = world.CreateBody(&bodyDef);
00062 
00063         // Define another box shape for our dynamic body.
00064         b2PolygonShape dynamicBox;
00065         dynamicBox.SetAsBox(1.0f, 1.0f);
00066 
00067         // Define the dynamic body fixture.
00068         b2FixtureDef fixtureDef;
00069         fixtureDef.shape = &dynamicBox;
00070 
00071         // Set the box density to be non-zero, so it will be dynamic.
00072         fixtureDef.density = 1.0f;
00073 
00074         // Override the default friction.
00075         fixtureDef.friction = 0.3f;
00076 
00077         // Add the shape to the body.
00078         body->CreateFixture(&fixtureDef);
00079 
00080         // Prepare for simulation. Typically we use a time step of 1/60 of a
00081         // second (60Hz) and 10 iterations. This provides a high quality simulation
00082         // in most game scenarios.
00083         float32 timeStep = 1.0f / 60.0f;
00084         int32 velocityIterations = 6;
00085         int32 positionIterations = 2;
00086 
00087         // This is our little game loop.
00088         for (int32 i = 0; i < 60; ++i)
00089         {
00090                 // Instruct the world to perform a single step of simulation.
00091                 // It is generally best to keep the time step and iterations fixed.
00092                 world.Step(timeStep, velocityIterations, positionIterations);
00093 
00094                 // Now print the position and angle of the body.
00095                 b2Vec2 position = body->GetPosition();
00096                 float32 angle = body->GetAngle();
00097 
00098                 printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
00099         }
00100 
00101         // When the world destructor is called, all bodies and joints are freed. This can
00102         // create orphaned pointers, so be careful about your world management.
00103 
00104         return 0;
00105 }


mvsim
Author(s):
autogenerated on Thu Sep 7 2017 09:27:48