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!
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;
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!