### Creating circular worlds like Democracy

Last Ludum Dare I implemented a circular world for my game. I wanted to do that for some time, but before this LD I had no idea how to implement such a thing. Now I see that is pretty easy to do so. In this post I will explain how to create a circular world like the one I used in Democracy.

The beauty of a circular world is that we can work only with the radius of the planet, ignoring its position at the screen. So, first thing to do is defining our planet to a radius “r” (lets say 150px) and to screen position (0, 0). To center the planet in the window, you move the camera to (-w/2, -h/2) where w is the window width and h is the window height. You should see something like this:

Now that we have an empty world, lets add some objects on it. All objects must have a position in this world, but this position isn’t the same of in screen, it is simpler. Suppose that at the point (0, r), what could be the north of our planet, is the angle 0°, then the east (r, 0) is 90° and so on, until the north point again. See the image below for reference:

Now our object can be at positions like 47° or 270°, but we still must draw it into the screen. That’s pretty simple. Let’s say we have a sprite with (s_w, s_h), width and height, respectively. Then we set its screen position to (0, 0) and its anchor to (0, r+s_h). With this we have an sprite at the north point 0°, to relocate it to 306° we simply set its rotation as “sprite.rotation = 306” and done!

White box is at north (0°) and the red box is at 306°.

Additionally you can also set the height of the object in that world (the distance of that object to the ground). To put a cloud at 30px high, you simply add the height to the sprites anchor, such that anchor is (0, r + s_w + height):

Sometimes you may find useful to convert the world position, now (position, height), to the screen position. For example, to add some particles. So, to find the screen coordinates (x, y) of an object at (position, height) you do:

```angle = radians(position);
x = sin(angle) * r + height;
y = -cos(angle) * r + height;```

Contrarily, when you want to convert the screen position to world coordinates, for example to find where the mouse is pointing. You can do:

```position = degrees(atan2(mouse.y - camera.y, mouse.x - camera.x));
height = euclidDistance(mouse.x, mouse.y, camera.x, camera.y) - r;```

Simple right?

Let me know if you found this article useful, or if you have an alternative way to do that, or yet, if you created some circular world based on this. Cheers!

### Democracy, my game for LD#32

The 32th ludum dare happened this weekend (April 16, 2015), with the theme “unconventional weapon“.

## Theme

I have have to say that I hate this theme. Really, there were so many good themes there, why this one could win? Damn you!!!! The theme isn’t an excuse to not do a game, but I spent so much time thinking of what I could do with this… A game not based on fights? Weapon is love? The weapon must be abstract! This is clichê? Oh damn… I finally came up with an idea, after 14 hours (counting sleep time)!

My game entry is about “democracy”, with quotes. The democracy and progress are the weapon of the government in the game, they want you to attack other species in order to “spread democracy and progress through the galaxy”. The player controls the invasions to planets inhabited by bugs with the goal to kill the natives and collect the planet resources.

The game is a very humble tribute to starship troopers (novel by Robert Heinlein and film by Paul Verhoven and Edward Neumeier). I wanted to create, somehow, the sense of order and progress presented by movie.

## Development

I always start by the core mechanics because it is the main component of the game – if I can’t find time to make graphics, at least I have a block-based game.

I also always wanted to create a game in a circular world, that was my excuse. I had no idea how to do that, but at is was pretty simple. You have a world centered at (0, 0) with radius (r), than you can set the origin of all objects in the world to (0, r) and only manipulate the rotation to place than in the world.

After the basic world structure, I could drop structures, and these structures could create units. To control the units, I created a behavior tree to control each unit – the first time I really use behavior3js – and a behavior tree to control the bugs strategy. Figure below show these behaviors trees:

Behavior tree for player and enemy units – Click to see in full size.

Behavior tree for the nests, its used to create orders to enemy units – Click to see in full size.

Then it came to particles and easing! Man, I love these two. The animation below shows the game at this point:

After 28 hours I started to create the sprites and visual things in the world. I was stupid enough to lost time adding some details to sprites that don’t even will see because the sprites are very small (about 20 pixels). I also spend a good time trying to create a futurist screen aspect, which ended up very cool.

I added some scenes, UI and some other effects to the graphics:

When the clock marked 5 hours to end the competition I started create sounds effects and music. I was desperate because of the time, I still had to adjust the levels of my game and I didn’t know what to do with sounds. At the end it was pretty simple and I didn’t do much, actually recorded some sounds with Audacity, generated most of them in BFXR and generated the music in SoundHelix.

I finished the game in the packing hour, the extra hour after 48, the time to deploy and write about your game.

## Summary:

Good stuff:

• Game finished is always good!
• I’m using BTs successfully, even to control hundreds of creatures.
• A circular world (always wanted to try).
• Visually smooth and lot of cool PARTICLES!!!!!
• Generative music and sounds worked well.