Book Image

SFML Blueprints

Book Image

SFML Blueprints

Overview of this book

Table of Contents (15 chapters)
SFML Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Pairing Box2D and SFML


Now that Box2D is installed and your system is configured to find it, let's build the physics "hello world": a falling square.

It's important to note that Box2D uses meters, kilograms, seconds, and radian for angle as units; SFML uses pixels, seconds, and degrees. So we will need to make some conversions.

Converting radians to degrees or vice versa is not difficult, but pixels to meters… this is another story. In fact, there is no way to convert a pixel to meter, unless if the number of pixels per meter is fixed. This is the technique that we will use.

So let's start by creating some utility functions. We should be able to convert radians to degrees, degrees to radians, meters to pixels, and finally pixels to meters. We will also need to fix the pixel per meter value. As we don't need any class for these functions, we will define them in a namespace converter. This will result as the following code snippet:

namespace converter
{
    constexpr double PIXELS_PER_METERS ...