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!
groundDef, bodyDef: Tb2BodyDef;
groundShapeDef, shapeDef: Tb2PolygonShape;
viterations, piterations: Int32;
SetValue(gravity, 0, -10);
world := Tb2World.Create(gravity);
groundDef := Tb2BodyDef.Create;
SetValue(groundDef.position, 0, -10);
groundBody := world.CreateBody(groundDef);
groundShapeDef := Tb2PolygonShape.Create;
// Add the ground shape to the ground body.
// 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;
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);
Tags: Android Appmethod architecture Box2D C++ C++ C++Builder C++Builder XE6 Delphi Delphi Programming Delphi Programming Delphi XE5 Delphi XE6 FireMonkey FireMonkey Game Development iOS iOS iPhone Miscellaneous News Object Pascal Object Pascal OpenGL OSX physics engine RAD Studio RAD Studio XE5 RAD Studio XE6 source-code Windows XE6
Please login first in order for you to submit comments
- Page :
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?