PlayRho  1.1.0
An Interactive Real-Time-Oriented C++ Physics Engine & Library
HelloWorld.cpp

This is the source file for the HelloWorld application that demonstrates use of the playrho::d2::World class and more. After instantiating a world, the code creates a body and its fixture to act as the ground, creates another body and a fixture for it to act like a ball, then steps the world using the world playrho::d2::World::Step(const StepConf&) function which simulates a ball falling to the ground and outputs the position of the ball after each step.

/*
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
* Copyright (c) 2020 Louis Langholtz https://github.com/louis-langholtz/PlayRho
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <PlayRho/PlayRho.hpp>
#include <iostream>
#include <iomanip>
// This is a simpler example of building and running a simulation using PlayRho.
// The code creates a large box to be the ground and a small disk to be a ball
// above the ground. There are no graphics for this example. PlayRho is meant
// to be used with the rendering engine of your game engine.
int main()
{
// Bring used namespaces into global namespace to simplify this source code.
using namespace playrho;
using namespace playrho::d2;
// Construct a world object which will hold and simulate bodies.
auto world = World{};
// Call world's body creation method which allocates memory for ground body and
// adds it to the world.
const auto ground = CreateBody(world, BodyConf{}.UseLocation(Length2{0_m, -10_m}));
// Define the ground shape. Use a polygon configured as a box for this.
// The extents are the half-width and half-height of the box.
const auto box = Shape{PolygonShapeConf{}.SetAsBox(50_m, 10_m)};
// Add the box shape to the ground body.
CreateFixture(world, FixtureConf{}.UseBody(ground).UseShape(box));
// Define location above ground for a "dynamic" body & create this body within world.
const auto ball = CreateBody(world, BodyConf{}
.UseLocation(Length2{0_m, 4_m})
.UseLinearAcceleration(EarthlyGravity));
// Define a disk shape for the ball body and create a fixture to add it.
CreateFixture(world, FixtureConf{}.UseBody(ball).UseShape(DiskShapeConf{}.UseRadius(1_m)));
// Setup the C++ stream output format.
std::cout << std::fixed << std::setprecision(2);
// A little game-like loop.
for (auto i = 0; i < 60; ++i)
{
// Perform a step of the simulation. Keep the time step & iterations fixed.
// Typically code uses a time step of 1/60 of a second (60Hz). The defaults
// are setup for that and to generally provide a high enough quality simulation
// in most scenarios.
Step(world);
const auto transformation = GetTransformation(world, ball);
const auto location = GetLocation(transformation);
const auto angle = GetAngle(transformation);
// Now print the location and angle of the body.
std::cout << std::setw(5) << GetX(location);
std::cout << std::setw(5) << GetY(location);
std::cout << std::setw(5) << angle;
std::cout << '\n';
}
return 0;
}
playrho::BodyType::Static
@ Static
Static body type.
playrho::d2::CreateBody
BodyID CreateBody(World &world, const BodyConf &def)
Creates a rigid body with the given configuration.
Definition: WorldBody.cpp:58
playrho::d2::PolygonShapeConf::SetAsBox
PolygonShapeConf & SetAsBox(Length hx, Length hy) noexcept
Sets the vertices to represent an axis-aligned box centered on the local origin.
Definition: PolygonShapeConf.cpp:42
playrho::d2
Name space for 2-dimensionally related PlayRho names.
Definition: AABB.cpp:34
playrho::GetX
constexpr auto & GetX(T &value)
Gets the "X" element of the given value - i.e. the first element.
Definition: Math.hpp:66
playrho
Name space for all PlayRho related names.
Definition: AABB.cpp:33
playrho::d2::World
Definition of an independent and simulatable "world".
Definition: World.hpp:129
playrho::d2::FixtureConf
Fixture definition.
Definition: FixtureConf.hpp:46
playrho::d2::BodyConf::UseLocation
constexpr BodyConf & UseLocation(Length2 l) noexcept
Use the given location.
Definition: BodyConf.hpp:172
playrho::d2::PolygonShapeConf
Polygon shape configuration.
Definition: PolygonShapeConf.hpp:42
playrho::d2::GetAngle
Angle GetAngle(const UnitVec value)
Gets the angle of the given unit vector.
Definition: Math.hpp:718
playrho::d2::EarthlyGravity
constexpr auto EarthlyGravity
Earthly gravity in 2-dimensions.
Definition: Vector2.hpp:154
playrho::d2::FixtureConf::UseBody
FixtureConf & UseBody(BodyID value) noexcept
Uses the given value for the body member variable.
Definition: FixtureConf.hpp:66
playrho::d2::CreateFixture
FixtureID CreateFixture(World &world, FixtureConf def, bool resetMassData)
Creates a fixture within the specified world.
Definition: WorldFixture.cpp:48
playrho::d2::DiskShapeConf::UseRadius
constexpr DiskShapeConf & UseRadius(NonNegative< Length > r) noexcept
Uses the given value as the radius.
Definition: DiskShapeConf.hpp:65
playrho::d2::BodyConf
Configuration for a body.
Definition: BodyConf.hpp:50
playrho::d2::GetLocation
constexpr Length2 GetLocation(const Transformation &value) noexcept
Gets the location information from the given transformation.
Definition: Transformation.hpp:69
playrho::Vector
Vector.
Definition: Vector.hpp:49
playrho::d2::GetTransformation
constexpr Transformation GetTransformation(const Length2 ctr, const UnitVec rot, const Length2 localCtr) noexcept
Gets the transformation for the given values.
Definition: Math.hpp:875
playrho::d2::Shape
Shape.
Definition: Shape.hpp:183
playrho::GetY
constexpr auto & GetY(T &value)
Gets the "Y" element of the given value - i.e. the second element.
Definition: Math.hpp:73
playrho::d2::Step
StepStats Step(WorldImpl &world, const StepConf &conf)
Steps the given world the specified amount.
Definition: WorldImplMisc.cpp:85
playrho::d2::DiskShapeConf
Disk shape configuration.
Definition: DiskShapeConf.hpp:42