Tower Defense + Box2D
If you were writing a Tower Defense game, would it have crossed your mind to use a physical engine like Box2D for this ? How would you implement the movement of units, the behavior of the towers? What else would you learn from the physics engine in such a game?

I began to think about these and other issues a few months ago, as a result of which a rather interesting game was born.
I am a big fan of Tower Defense games. It all started a long time ago with Green TD, maps for Warcraft 3, then I tried a lot of games on flashtowerdefense.com , and after the android smartphone appeared, I often spent time playing games from the market.
Once I thought that it would be nice to write such a game myself. I wanted to come up with something unusual, but, as one friend of mine says, "the robots are already sick of it." It is difficult to disagree with him, mainly on the market in this category almost everywhere robots, zombies and mythical creatures. I wanted something brighter and more peaceful. I also had an obsession with using a physical engine in the game, firstly to make it easier to handle collisions, and secondly to add something non-standard and interesting.

As a result, the following plot was invented: instead of monsters and zombies walking on the map, I had fruits falling from a tree, instead of robot towers, animal towers. The fruit fall trajectory was set by branches growing on a tree and gravity. Fruits bounced off the branches several times on the way down, their speed and direction changed, while the animal towers had to break them, while they were falling.
I made five types of towers:
Squirrel. Throws nuts on fruits, an ordinary tower, good attack speed and damage. Despite the fact that the tower does not have any special abilities, it is very difficult to pass any level without it, it is useful both at the very beginning of the game and at the end.
White fox. Launches a freezing vortex into the fruit, thereby slowing their fall rate. Without this tower, it is almost impossible to pass any level on difficulty other than easy. Still, I could not completely do without magic, in my game I really wanted a freezing tower.
Red fox. Shoots an arrow from an onion, which flies through all the fruits in its path. This tower needs to be set wisely, so that in one shot it hits the maximum number of fruits. At some levels, there are key places under this tower, and the player's task is to make out them.
Hedgehog. Shoots needles in all directions. It has a small attack radius and damage, but due to the large number of needles it helps a lot in places where large accumulations of fruits form, or where they rotate in a circle.
Bear. Throws stones that, when hit, repel and change the trajectory of the fruit. At some levels, it can be placed below so that it will toss the surviving fruits up, allowing the rest of the towers to finish them off. It can break a lot of firewood if you put it at the beginning of the level.
I must admit, the Box2D engine made my life a lot easier. Thanks to him, I had a beautiful fall of fruits in the game, detection of their towers, as well as other collision checks. Without Box2D, there would be no bear. I have some ideas for new towers using a physics engine, such as a fox with arrows that are tied with a rope to a branch, if such an arrow hits the fruit, it will hang on the rope until other towers break it. You can come up with a lot of interesting things.
Next, I will describe in which particular objects I used Box2D, and what properties needed to be set:
b2_staticBody means that the object is motionless, it is not affected by gravity and other forces. This applies to towers, branches, and borders.
b2_dynamicBody defines a moving object that can move and bounce from other objects. All fruits and bullets are b2_dynamicBody.
isSensor = true indicates that the object does not seem to exist; when it collides with it, other objects do not bounce, but pass through it. When such an event occurs, the library makes it known what is needed for the situation when the fruit falls within the range of the tower, or the arrow passes through the apple. All fruits have the same negative value groupIndexThanks to this, they never collide with each other, but successfully bounce off branches and borders.
The b2CircleShape object has a round shape, b2PolygonShape - the shape of a polygon, in my case all such objects are rectangles.
I have to admit that introducing Box2D in Tower Defense was not an easy thing, I ran into a lot of problems, for example, do any of you guess how to use Box2D to implement a freezing tower? (in fact, the answer to this question requires solving the problems of the 9th grade in physics and deserves a separate post). A considerable amount of time was required to select the appropriate physical parameters for all objects. But in the end, everything worked out, and I can say with confidence: "Tower Defense + Box2D = Yeah!"
I named my game Forest Tower Defense and put it on the android market . Those who wish can download it for free. There you can see screenshots and videos for the game. The graphics turned out to be cartoony, so the children may also like the game. I advise all fans of Tower Defense games to try, because this is the only game of this genre with similar mechanics. I’m very interested to hear your opinion, improvement tips and ideas for new features. UPD: The development of the game was carried out according to the principle described in this article , so there is a version for Windows using Qt.


I began to think about these and other issues a few months ago, as a result of which a rather interesting game was born.
I am a big fan of Tower Defense games. It all started a long time ago with Green TD, maps for Warcraft 3, then I tried a lot of games on flashtowerdefense.com , and after the android smartphone appeared, I often spent time playing games from the market.
Idea
Once I thought that it would be nice to write such a game myself. I wanted to come up with something unusual, but, as one friend of mine says, "the robots are already sick of it." It is difficult to disagree with him, mainly on the market in this category almost everywhere robots, zombies and mythical creatures. I wanted something brighter and more peaceful. I also had an obsession with using a physical engine in the game, firstly to make it easier to handle collisions, and secondly to add something non-standard and interesting.

As a result, the following plot was invented: instead of monsters and zombies walking on the map, I had fruits falling from a tree, instead of robot towers, animal towers. The fruit fall trajectory was set by branches growing on a tree and gravity. Fruits bounced off the branches several times on the way down, their speed and direction changed, while the animal towers had to break them, while they were falling.
I made five types of towers:





Box2d
I must admit, the Box2D engine made my life a lot easier. Thanks to him, I had a beautiful fall of fruits in the game, detection of their towers, as well as other collision checks. Without Box2D, there would be no bear. I have some ideas for new towers using a physics engine, such as a fox with arrows that are tied with a rope to a branch, if such an arrow hits the fruit, it will hang on the rope until other towers break it. You can come up with a lot of interesting things.
Next, I will describe in which particular objects I used Box2D, and what properties needed to be set:
Title | Box2D Properties | Picture |
---|---|---|
The fruit | b2CircleShape b2_dynamicBody groupIndex = -1 | ![]() |
Branch | b2_staticBody b2PolygonShape | ![]() |
Tower | b2_staticBody b2CircleShape isSensor = true | ![]() |
Arrow | b2_dynamicBody b2PolygonShape isSensor = true | ![]() |
A rock | b2_dynamicBody b2CircleShape | ![]() |
Walls, floor, ceiling | b2_staticBody b2PolygonShape | ![]() |
b2_staticBody means that the object is motionless, it is not affected by gravity and other forces. This applies to towers, branches, and borders.
b2_dynamicBody defines a moving object that can move and bounce from other objects. All fruits and bullets are b2_dynamicBody.
isSensor = true indicates that the object does not seem to exist; when it collides with it, other objects do not bounce, but pass through it. When such an event occurs, the library makes it known what is needed for the situation when the fruit falls within the range of the tower, or the arrow passes through the apple. All fruits have the same negative value groupIndexThanks to this, they never collide with each other, but successfully bounce off branches and borders.
The b2CircleShape object has a round shape, b2PolygonShape - the shape of a polygon, in my case all such objects are rectangles.
I have to admit that introducing Box2D in Tower Defense was not an easy thing, I ran into a lot of problems, for example, do any of you guess how to use Box2D to implement a freezing tower? (in fact, the answer to this question requires solving the problems of the 9th grade in physics and deserves a separate post). A considerable amount of time was required to select the appropriate physical parameters for all objects. But in the end, everything worked out, and I can say with confidence: "Tower Defense + Box2D = Yeah!"
Result
I named my game Forest Tower Defense and put it on the android market . Those who wish can download it for free. There you can see screenshots and videos for the game. The graphics turned out to be cartoony, so the children may also like the game. I advise all fans of Tower Defense games to try, because this is the only game of this genre with similar mechanics. I’m very interested to hear your opinion, improvement tips and ideas for new features. UPD: The development of the game was carried out according to the principle described in this article , so there is a version for Windows using Qt.
