Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!


Topple The Tower Box2d Game Source Code For Delphi XE6 Firemonkey On Windows

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE5 XE6 Firemonkey Topple The Tower Game Source Code | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSI have polished up a new Box2D Firemonkey game demo prototype with full source code for Delphi XE5 and XE6 Firemonkey (and AppMethod). This demo is called Topple The Tower that I put together to demonstrate two different pieces of functionality that are useful for building games with the Box2D Firemonkey Physics Engine. The two pieces of functionality are attaching Delphi Firemonkey objects to Box2d bodies and detecting the strength of collisions between Box2d bodies.

The Tb2Body has a property called UserData which is a Pointer. You can assign your own objects or data to the UserData property (like a TRectangle) and then access them later. Here is a simple example of how to do that in code:
sd: Tb2PolygonShape;
bd: Tb2BodyDef;
body: Tb2Body;


body := m_world.CreateBody(bd, False);
body.CreateFixture(sd, 5.0, False);

body.UserData := TRectangle.Create(nil);
TRectangle(body.UserData).Parent := WorldObject;
TRectangle(body.UserData).Position.X := body.GetPosition.x;
TRectangle(body.UserData).Position.Y := body.GetPosition.y;
TRectangle(body.UserData).Width := 22;
TRectangle(body.UserData).Height := 22;

What this allows you to do is mirror the position of a Box2d body as a Firemonkey object like a TRectangle. I used a bit of a hack and use the OnPaint event of the TPaintBox in the project to move all of the objects like the TRectangles to keep them in sync with the Box2d bodies. You should probably use a game loop instead however. The drawing of the TPaintBox is actually turned off (change Settings.drawShapes to True if you want to see the Box2d bodies drawn).

For detecting collisions and the strength of collisions between Box2d objects I followed this tutorial and some source code from the other Box2d Firemonkey demos to create a TMyb2ContactListener class that has two event methods. The first one is BeginContact(var contact: Tb2Contact) and the second one is PostSolve(var contact: Tb2Contact; const impulse: Tb2ContactImpulse). BeginContact works if you just need to know when collisions happened and I originally started with this function. However, if you need to know the strength of collisions you use PostSolve(). First I check to see if the two bodies that collided were bodies that I want to take action on and then I use the TagFloat property to record how many times the bodies have collided. In my game loop (in this case the OnPaint event) is where I count up the damage each body has and remove it. Here is the PostSolve() event code (you will notice how I use the UserData property here to interface with my Firemonkey objects):
procedure TMyb2ContactListener.PostSolve(var contact: Tb2Contact; const impulse: Tb2ContactImpulse);
const MAX_IMPULSE:Integer = 100;
bodyA, bodyB: Tb2Body;
if (impulse.normalImpulses[0]>MAX_IMPULSE) then
bodyA := contact.m_fixtureA.GetBody;
bodyB := contact.m_fixtureB.GetBody;

if Assigned(bodyA) AND Assigned(BodyB) then
if (bodyB.UserData<>nil) AND (bodyA.UserData<>nil) then
if (TFmxObject(bodyB.UserData) is TCircle) AND (TFmxObject(bodyA.UserData) is TRectangle) then
if TRectangle(bodyA.UserData).Tag=2 then
TRectangle(bodyA.UserData).TagFloat := TRectangle(bodyA.UserData).TagFloat+5;
else if (TFmxObject(bodyB.UserData) is TRectangle) AND (TFmxObject(bodyA.UserData) is TRectangle) then
if (TRectangle(bodyA.UserData).Tag=2) then
TRectangle(bodyA.UserData).TagFloat := TRectangle(bodyA.UserData).TagFloat + 1;

Finally, I used TFrames to create the start screen, instruction screen, and game over screen. They are set to design time visible false. This keeps them separate which makes them much easier to use. For the sound I use the Windows API but it has a TMediaPlayer for other platforms (or if you want to use each platform’s native play sound API you could). For the music I used a TMediaPlayer. The popping sound and the music are from

I set this project up to use Win64 as that has the best speed at handling the Box2d Firemonkey calculations but Win32 will also work fine. I built this demo for Windows and OSX desktop but it would run on Android and IOS with some changes to the layout of the project. On a tablet it would probably run fine without any layout changes.

Download the full source code for the Box2d Firemonkey Topple The Tower Prototype Demo.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging


Unleashing Creativity With Song Writer AI: A Deep Dive


How To Build Stable Diffusion Text To Image Prompts


FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

Leave a Reply