Box2D physics engine in Object Pascal

Posted by on in Blogs

There is plenty of interesting Delphi, Appmethod and FireMonkey information published very frequently on website.

A very interesting article was published on fmxexpress about Box2d Physics Engine For Delphi XE6 FireMonkey on Android, iOS, Windows and OSX a few days ago.

You have probably heard about "Angry Birds". It is probably the most easy to remember example of an application that is using Box2D.

Box2D is one the most popular, open-source engine for simulating rigid bodies in 2D. The original C++ project has been developed by Erin Catto and has the zlib license.

Box2D has been translated from C++ to other programming languages. Qianyuan Wang converted the Box2D physics engine to Delphi a few years ago and its source code is available on Google Code.

The physics engine is independent from any particular graphics technology, so it was possible to translate just the rendering part of the Box2D-Delphi Testbed project - with about 60 different visual tests - from Windows OpenGL to FireMonkey code, so it can be used not only on Windows, but also on OSX and mobile platforms, like iOS and Android.

That's very cool! You can download the FireMonkey version of Box2D-Delphi from github.

The source code is very interesting and you are going to quickly look for the documentation. There is a pdf version of the C++ Box2D manual available here.

Every single project needs to have a "Hello World" example for starters. Box2D is not different. The Delphi port of Box2D contains only the VCL Forms and FireMonkey Forms versions of the "Hello World Box2D" project, so I thought it would be good to add a console version of this program for completeness.

Here it is, enjoy!

program HelloBox2D_Console;



world: Tb2World;
groundBody: Tb2Body;
gravity: TVector2;
groundDef, bodyDef: Tb2BodyDef;
groundShapeDef, shapeDef: Tb2PolygonShape;
fd: Tb2FixtureDef;
body: Tb2Body;
timeStep: PhysicsFloat;
viterations, piterations: Int32;
i: Integer;
position: TVector2;
angle: PhysicsFloat;

SetValue(gravity, 0, -10);

world := Tb2World.Create(gravity);

groundDef := Tb2BodyDef.Create;
SetValue(groundDef.position, 0, -10);
groundBody := world.CreateBody(groundDef);

groundShapeDef := Tb2PolygonShape.Create;
groundShapeDef.SetAsBox(50.0, 10.0);

// Add the ground shape to the ground body.
groundBody.CreateFixture(groundShapeDef, 0.0);

// Define the dynamic body. We set its position and call the body factory.
bodyDef := Tb2BodyDef.Create;
bodyDef.bodyType := b2_dynamicBody;
SetValue(bodyDef.position, 0.0, 4.0);
body := world.CreateBody(bodyDef);

// Define another box shape for our dynamic body.
shapeDef := Tb2PolygonShape.Create;
shapeDef.SetAsBox(1.0, 1.0);

fd := Tb2FixtureDef.Create;
fd.shape := shapeDef;
// Set the box density to be non-zero, so it will be dynamic.
fd.density := 1.0;

// Override the default friction.
fd.friction := 0.3;

// Add the shape to the body.

// 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.
timeStep := 1.0 / 60.0;
viterations := 6;
piterations := 2;

for i := 0 to 59 do
// 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, viterations, piterations);

// Now print the position and angle of the body.
position := body.GetPosition;
angle := body.GetAngle;

writeln(Format('%d x: %f y: %f a: %f', [i, position.x, position.y, angle]));


on E: Exception do
Writeln(E.ClassName, ': ', E.Message);


Gold User, Rank: 9, Points: 364
Crazy about Delphi Programming!


  • Guest
    Wouter van Nifterick Tuesday, 29 April 2014

    Awesome. I'm reading this on an iPad, but I don't have a Mac to compile for IOS. Is there a demo in the appstore?

  • Guest
    Paweł Głowacki Wednesday, 30 April 2014

    I do not think that there is a demo of Box2D Object Pascal in AppStore, but it is actually a very good idea:-)

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: