Wow. I really didn’t mean it to be nearly three months between posts on this! I have still been working on the project reasonably regularly, but (a) I’ve frequently been waylaid by outside circumstances (like Christmas, my actual job going a bit crazy, family members being admitted to hospital, etc.), and (b) the task I’d set myself required a lot of work, probably more than I expected. But anyway, I’ve finally made some progress, so on with the game!
I’ve spent the past couple of months adding buildings to the game world. It probably needn’t have taken as long as it did; I could have just thrown together some building models in Blender in a matter of hours and made the game engine load them, but I wanted to do things properly, partly because the buildings are going to be a pretty important part of the game, and partly because I just find it more satisfying.
So what would be wrong with just making the building models in Blender and exporting them to the game? Not a huge amount, really. It would be a perfectly viable approach in many cases. But it would have its drawbacks:
- 3D model files take up quite a lot of space, and in the case of a web-based game that has to load everything over the internet, this can add up.
- They would lack flexibility. If I wanted, for example, to have several different building models that were similar but slightly different, I’d still have to export separate complete model files for each of them. Even within a single building, it could become difficult to re-use elements of the design (like windows and doors).
- Blender isn’t really ideally suited to modelling buildings. It is an enormously capable and powerful tool that can model everything under the sun, but there’s a downside to that power: if all you want to make is walls, roofs, doors, windows and so on, all the features that are better suited to modelling vegetation, vehicles or animated characters just get in the way and complicate things.
So instead I developed my own building modelling system for the game. There are two parts to it: a renderer that can turn a simple JSON description of a building into the actual triangles you see on screen, and an editor that allows me to create that JSON description, similar to the tree editor I wrote about in a previous entry.
Conceptually, the buildings are quite simple. They’re made up of a number of wings (not quite the right term but the closest one I could think of) each of which has an outline and some storeys, as well as a roof (either flat or pitched). Walls and roofs can have models attached to them for things like windows, doors, chimneys, drainpipes, etc., and can also have holes punched through them where openings are required. These models (which I’m calling decorations, again not a perfect term) are being made in Blender and converted to a suitable binary format for the game engine and editor using a new C++ converter program.
This will almost certainly need more work in the future, but for now I’m happy that the basics are working. Once I had this up and running, I wanted to add a few more features before I considered the first phase of buildings complete:
- A bit of optimisation. I’m planning for there to be quite a lot of buildings in the game world so it’s important that they work efficiently. I described some methods for speeding up 3D rendering in quite a lot of detail in part 5 so I won’t go into too much detail now, but basically I’m using depth cuing with the buildings. My code automatically generates a low detail and high detail version of each building model. They’re fairly similar but the low detail version has many features (like doors and windows) “painted on” using textures rather than having actual 3D geometry for them. This won’t look good up close but can be acceptable for buildings further from the viewer, and it significantly reduces the number of triangles on screen.
- A method of placing the buildings into the game world. I was already using Blender to edit the terrain and place trees and woodlands, so for the buildings I used a very similar approach to what I was already doing with trees. Basically, I put down a “placeholder” object (usually a cube) within Blender and give it a name that my converter program will recognise as being a building, as well as specifying what type of building it should be. Then the game engine will place an actual building model in that location when it runs.
- Collision detection! To begin with the player character was just sliding through the walls of the buildings, which didn’t do much for the realism. I’ve added basic collision detection for the building walls which will do for the moment, though I’ll need to revisit it in future to add some more advanced features (like allowing the player to go through a door and into a building, or to slide along a wall if she walks into it at an angle rather than just coming to a dead stop). Then for good measure I also added collision detection for the trees, something which was lacking up until now.
Phew! After spending all that time on one feature I can’t wait to get onto something different. The next few things I’m planning should be a bit simpler so hopefully there won’t be another 3 month gap before the next blog post.