📜 ⬆️ ⬇️

Tower Defense + Box2D

If you were writing a Tower Defense game, would it have occurred to you to use a physics engine for this, for example Box2D ? How would you implement the movement of units, the behavior of the towers? What else would you have learned from the physics engine in such a game?

I started thinking about these and other questions a few months ago, as a result of which an 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 many 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, and then, as one friend of mine says, “the robots have already been pissed.” It's hard to disagree with him, mostly on the market in this category almost everywhere are robots, zombies and mythical creatures. I wanted something brighter and more peaceful. I also had an obsession with using a physics 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 along the map, I had fruit falling from the tree, instead of tower-robots, animal towers. The trajectory of the fall of the fruit was set by branches growing on a tree and gravity. Fruits on the way down bounced off the branches several times, their speed and direction changed, for the time they fell, the animal towers had to smash them.

I made five kinds of towers:
')
Squirrel. Throws nuts on the fruit, the usual 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. It lets the fruit freeze a whirlwind into the fruit, thereby slowing down their falling speed. 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 very much wanted a freezing tower.

Red fox. He shoots a bow with an arrow that flies through all the fruit in its path. This tower must be set wisely, so that in one shot it hits the maximum amount of fruit. On some levels there are key places under this tower, and the player’s task is to look at 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 clusters of fruits form, or where they rotate in a circle.

Bear. Throws stones that, when hit, repel and change the trajectory of fruit. On some levels you can put it down in such a way that it will throw the surviving fruit upwards, allowing the rest of the towers to finish them off. It can break a lot of wood, if you put it at the beginning of the level.

Box2d


I must admit, the Box2D engine made my life much easier. Thanks to him, I had a beautiful fall of fruit in the game, detection by their towers and other collision checks. Without Box2D there would be no bear. I have a few 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 a fruit, it will hang on a rope until it is broken by other towers. You can come up with a lot of interesting things.

Next, I will describe in which objects I used Box2D, and what properties needed to be set:
TitleBox2D PropertiesPicture
Fruitb2CircleShape
b2_dynamicBody
groupIndex = -1
Branchb2_staticBody
b2PolygonShape
Towerb2_staticBody
b2CircleShape
isSensor = true
Arrowb2_dynamicBody
b2PolygonShape
isSensor = true
A rockb2_dynamicBody
b2CircleShape
Walls, floor,
ceiling
b2_staticBody
b2PolygonShape

b2_staticBody means that the object is motionless, gravity and other forces do not act on it. This applies to towers, branches and borders.

b2_dynamicBody specifies a moving object that can move and bounce off other objects. All fruits and bullets are b2_dynamicBody.

isSensor = true says that the object doesn’t exist, as it collides with it, other objects do not bounce, but pass through it. When such an event occurs, the library makes it known that it is necessary for the situation when the fruit falls within the radius of the tower, or the arrow passes through the apple. All fruits have the same negative groupIndex value, due to this they never collide with each other, but successfully bounce off branches and borders.

The b2CircleShape object has a round shape, b2PolygonShape is a polygon shape, in my case all such objects are rectangles.

I must admit that the Box2D implementation in Tower Defense was not an easy game, I ran into a lot of problems, for example, does any of you have any idea how to use the freezing tower with Box2D? (in fact, the answer to this question requires solving a 9th grade physics problem 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 turned out, and I can say with confidence: "Tower Defense + Box2D = Yeah!"

Result


I called my game Forest Tower Defense and laid it out on the android market . Anyone can download it for free. You can also see screenshots and video for the game. The graphics turned out to be cartoonish, so the children can 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 am very interested to hear your opinion, tips for improvement and ideas for new features.

UPD: Development of the game was carried out according to the principle described in this article , so there is a version for Windows using Qt.

Source: https://habr.com/ru/post/136725/


All Articles