113 lines
3.9 KiB
C++
113 lines
3.9 KiB
C++
// MIT License
|
|
|
|
// Copyright (c) 2019 Erin Catto
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
#include "box2d/box2d.h"
|
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
|
#include "doctest.h"
|
|
#include <stdio.h>
|
|
|
|
// This is a simple example of building and running a simulation
|
|
// using Box2D. Here we create a large ground box and a small dynamic
|
|
// box.
|
|
// There are no graphics for this example. Box2D is meant to be used
|
|
// with your rendering engine in your game engine.
|
|
DOCTEST_TEST_CASE("hello world")
|
|
{
|
|
// Define the gravity vector.
|
|
b2Vec2 gravity(0.0f, -10.0f);
|
|
|
|
// Construct a world object, which will hold and simulate the rigid bodies.
|
|
b2World world(gravity);
|
|
|
|
// Define the ground body.
|
|
b2BodyDef groundBodyDef;
|
|
groundBodyDef.position.Set(0.0f, -10.0f);
|
|
|
|
// Call the body factory which allocates memory for the ground body
|
|
// from a pool and creates the ground box shape (also from a pool).
|
|
// The body is also added to the world.
|
|
b2Body* groundBody = world.CreateBody(&groundBodyDef);
|
|
|
|
// Define the ground box shape.
|
|
b2PolygonShape groundBox;
|
|
|
|
// The extents are the half-widths of the box.
|
|
groundBox.SetAsBox(50.0f, 10.0f);
|
|
|
|
// Add the ground fixture to the ground body.
|
|
groundBody->CreateFixture(&groundBox, 0.0f);
|
|
|
|
// Define the dynamic body. We set its position and call the body factory.
|
|
b2BodyDef bodyDef;
|
|
bodyDef.type = b2_dynamicBody;
|
|
bodyDef.position.Set(0.0f, 4.0f);
|
|
b2Body* body = world.CreateBody(&bodyDef);
|
|
|
|
// Define another box shape for our dynamic body.
|
|
b2PolygonShape dynamicBox;
|
|
dynamicBox.SetAsBox(1.0f, 1.0f);
|
|
|
|
// Define the dynamic body fixture.
|
|
b2FixtureDef fixtureDef;
|
|
fixtureDef.shape = &dynamicBox;
|
|
|
|
// Set the box density to be non-zero, so it will be dynamic.
|
|
fixtureDef.density = 1.0f;
|
|
|
|
// Override the default friction.
|
|
fixtureDef.friction = 0.3f;
|
|
|
|
// Add the shape to the body.
|
|
body->CreateFixture(&fixtureDef);
|
|
|
|
// Prepare for simulation. Typically we use a time step of 1/60 of a
|
|
// second (60Hz) and 10 iterations. This provides a high quality simulation
|
|
// in most game scenarios.
|
|
float timeStep = 1.0f / 60.0f;
|
|
int32 velocityIterations = 6;
|
|
int32 positionIterations = 2;
|
|
|
|
b2Vec2 position = body->GetPosition();
|
|
float angle = body->GetAngle();
|
|
|
|
// This is our little game loop.
|
|
for (int32 i = 0; i < 60; ++i)
|
|
{
|
|
// Instruct the world to perform a single step of simulation.
|
|
// It is generally best to keep the time step and iterations fixed.
|
|
world.Step(timeStep, velocityIterations, positionIterations);
|
|
|
|
// Now print the position and angle of the body.
|
|
position = body->GetPosition();
|
|
angle = body->GetAngle();
|
|
|
|
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
|
|
}
|
|
|
|
// When the world destructor is called, all bodies and joints are freed. This can
|
|
// create orphaned pointers, so be careful about your world management.
|
|
|
|
CHECK(b2Abs(position.x) < 0.01f);
|
|
CHECK(b2Abs(position.y - 1.01f) < 0.01f);
|
|
CHECK(b2Abs(angle) < 0.01f);
|
|
}
|