Box2D physics engine in Object Pascal

  

There is plenty of interesting Delphi, Appmethod and FireMonkey information published very frequently on www.fmxexpress.com 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;

{$APPTYPE CONSOLE}

uses
System.SysUtils,
UPhysics2DTypes,
UPhysics2D;

var
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;

begin
try
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.
body.CreateFixture(fd);

// 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
begin
// 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]));
end;

world.Free;

except
on E: Exception do
Writeln(E.ClassName, ‘: ‘, E.Message);
end;

readln;
end.

Share This | Email this page to a friend

Comments are closed.