Post

C++ Box2D Install

This guide will provide step-by-step instructions on how to add Box2D to a Visual Studio C++ project.

Logo

About Box2D

Box2D is a free open source 2-dimensional physics simulator engine written in C++ by Erin Catto (Blizzard) and published under the MIT license. Box2D performs constrained rigid body simulation. It can simulate bodies composed of convex polygons, circles, and edge shapes. Bodies are joined with joints and acted upon by forces. The engine also applies gravity, friction, and restitution. The games created with Box2D include Angry Birds, Limbo, and Shovel Knight as well as the Unity game engine.

AngryBirds Limbo ShovelKnight Unity

Download Box2D

Download
  • Click on the Box2D repository (repo for short)
Repo
  • Click on Code and Download ZIP
Download Zip
  • Move the box2d-main.zip file into the ThirdParty folder
  • Unzip the box2d-main.zip file
Zip
  • Rename the box2d-main folder to box2d
  • Delete the box2d-main.zip file after unzipping, it is no longer needed
Folder

Build Box2D

The build instructions are in the Building section on the Box2D GitHub page. Detailed instructions are also provided in this guide below.

Building

It is recommended to follow the instructions below as they provide more details.

Install CMake

CMake is a tool to create the build environment for a project. It is used to package a project and then create the project files for the build environment (Visual Studio) and platform (Windows/Linux).

CMake Download
  • Run the downloaded file to install CMake
CMake Install
CMake Installer
  • Make sure to enable Add CMake to the PATH environment variable
    • This will make include the CMake directory in the PATH allowing CMake to be run from anywhere on the computer
CMake Path

Build Box2D

  • Run the Build.bat in the extracted box2d-main folder
    • This is a batch file and will run a set of commands to build the Box 2D Visual Studio Solution using CMake
Box2D Build
Box2D Build

Run Box2D

  • Once the build is complete, Visual Studio will be launched with the Box2D solution
Box2D Solution Explorer
  • Build and Run the Box2D samples project
Box2D Program
  • Run some of the tests to see the different features and functionality of Box2D
Box2D Tests


Save a screenshot of Box2D running in Visual Studio as it will be required for the submission of the assignment.

Add Box2D Library

Add Box2D Include Directories

The includes need to be done in all Projects in the Solution.

  • Open the Project Properties that Box2D will be used in
    • Right-click the Project and select Properties
Project Properties
  • Add the directory of the Box2D include folder to the Additional Include Directories.
    • Additional Include Directories is located in C/C++>General.
    • Add $(SolutionDir)ThirdParty\box2d\include
1
$(SolutionDir)ThirdParty\box2d\include
Include

Add Box2D Library Directories and Library (.lib)

The Box2D Library Directories and Library (.lib) only need to be completed on the project that is the application (Game). Do not perform the steps with the library project (Engine). If it is done on both, there will be a warning reported when built.

  • In the box2d directory in ThirdParty create a folder called “lib”
    • This will store the .lib file needed for the projects
Library
  • To make it easier to find, the box2d.lib will be copied to the new lib directory
    • Go to the ThirdParty\box2d\build\src\Debug folder and copy the box2d.lib and box2d.pdb to the ThirdParty\box2d\lib folder
Library Library
  • Add the directory of the Box2D library folder in Project Properties
    • Additional Library Directories is located in Librarian>General or Linker>Input
    • Add $(SolutionDir)ThirdParty\box2d\lib
1
$(SolutionDir)ThirdParty\box2d\lib
Library


  • Add the Box2D .lib files that the project needs to function
    • Additional Dependencies is located in Librarian>General or Linker>General
    • Add box2d.lib
1
box2d.lib
Lib

Create Physics Class

  • Create a new Filter called “Physics” in the Engine project
Lib
  • Add a Physics.h header and Physics.cpp source file in the Engine\Source\Physics folder
Lib
Lib

Physics.h

  • Add the following code for the Physics.h header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <box2d/box2d.h>
#include <memory>

class Physics
{
public:
	Physics() = default;

	bool Initialize();
	void Shutdown();

	void Update(float dt);

private:
	b2WorldId m_worldId;
};

Physics.cpp

  • Add the following code for the Physics.cpp source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Physics.h"

bool Physics::Initialize()
{
	b2WorldDef worldDef = b2DefaultWorldDef();
	worldDef.gravity = b2Vec2{ 0.0f, -10.0f };
	m_worldId = b2CreateWorld(&worldDef);

	return true;
}

void Physics::Shutdown()
{
	b2DestroyWorld(m_worldId);
}

void Physics::Update(float dt)
{
	b2World_Step(m_worldId, 1.0f / 60.0f, 4);
}

Add Physics to the Engine

  • In the Engine.h header include the Physics.h header
  • Add the Physics class to the Engine class and create an accessor method
1
2
3
Physics& GetPhysics() { return *m_physics; }

std::unique_ptr<Physics> m_physics;
  • In the Engine.cpp source file, create, initialize, shutdown and update the Physics class
1
2
3
4
5
6
7
m_physics = std::make_unique<Physics>();

m_physics->Initialize();

m_physics->Shutdown();

m_physics->Update(m_time->GetDeltaTime());
This post is licensed under CC BY 4.0 by the author.