marbs posted on June 24, 2013 at 10:31 PM

Dirty Hacks

Sometimes you just have to get code working, regardless of how pretty it is. This is especially true for game developers, since you generally want a playable prototype as fast as possible.

I find this quite difficult sometimes. My progress gets bogged down by planning for the future, by writing clean and modular code, or by finding the optimal solution for everything.

Perhaps my engineering degree created this desire for perfection? I don't think so. Code written by my lecturers was almost always very messy, with sloppy indentation and inconsistent braces. (At least it wasn't as bad those ancient hieroglyphics they call handwriting.)

So do I write perfect code? Definitely not! Looking back at code I wrote months ago, weeks ago, or sometimes even just seconds ago, I spot things I would do differently. Why is this? That answer could take up a whole other blog post.

What I want to show you is this level editor I've been working on:

Above is a cubic bezier curve.

The black shapes can be moved to manipulate the curve. The squares indicate end points (where the terrain begins and ends), while the circles represent control nodes (used to manipulate the shape of the curve.)

It was surprisingly easy to implement, taking only a few hours. Feeling confident, I decided to add circular arcs as well. After all, surely arcs would be super easy in comparison?

Above is a circular arc. It turned out to be terrifically hard to implement, and took, on and off, about three days to get working.

I found it very irritating because, at the outset, it seemed like such a simple thing to do! The user places three points; two squares and one circle; then the arc then just has to pass from one square, through the circle, to the other square. For any three given points there exists a circle that passes through, so it is just a matter of determining this circle, and taking only a section of it - the arc.

The first bit I could do - determining the circle - that was a simple wikipedia job. It was the second part that stumped me.

I managed to isolate the problem down to a couple of things. Part of the it was this: my algorithm incorrectly calculated the angle that passes from one square, through the circle, to the other square.

In the above image, the correct solution is the yellow angle. However, my calculation would always return the shortest angle, so it never worked for a reflex arc. I spent a while trying to find a mathematical solution to this, but hours passed and no progress was made.

Through experimentation, I found that by manually adjusting a couple of boolean parameters of the arc generation code I could always create the correct arc! The parameters controlled a couple of things: whether to flip the arc direction, and whether to subtract its angle from 360 degrees.

And so I arrived at my solution: brute force!

Given that one combination of the aforementioned two boolean parameters always led to the solution, this meant that only 4 possible combinations exist. So I stuck the arc generation code inside a loop which tries all 4 combinations and returns only the correct one. It works flawlessly!

Is this solution sub-optimal and terribly ugly? Yes; but that's not important! What really matters is now I can move on to making it playable, and find out whether the game is fun.

For similar tales, read the Gamasutra article on Dirty Game Development Tricks. My favourite one of those is where the programmer exploits a buffer overflow to patch his own game. Oh, and also the one where they store audio data within a 3D model.

What dirty hacks and programming sins have you committed in order to get things done?

marbs posted on February 10, 2013 at 6:55 PM

Giant Creme Egg

I love Cadbury creme eggs. My favourite tea mug is a creme egg mug. I've been known to stock up on creme eggs during Easter to try and make it through the rest of the year when they are unavailable.

When I am not on the computer doing programming I often like to do cooking or baking. As a result over the past three days I did something I've always wanted to: made a giant creme egg! (There - that's my tenuous link from game development to giant creme eggs. Now onto the fun stuff!)
-2.2kg icing sugar
-1.85kg Cadbury dairy milk chocolate
-1kg golden syrup
-350g butter
-5 tablespoons of vanilla essence
-Yellow food colouring (though you should use orange)

-Two 25cm x 17cm egg moulds
-Mixing bowl large enough to place over your head
-Pastry brush

1) Mix together the golden syrup and butter. Gradually add the icing sugar in, a little bit at a time. Once mixed thoroughly, you should have a bowl weighing over 3kg and tasting delicious. (Takes about 2 hours)
2) Place mixture in the fridge, then perform steps 3 to 7.
3) Melt the chocolate over boiling water, starting with just about 200g.
4) Put a little bit of oil in each egg mould, and use the pastry brush to spread it around.
5) Using the pastry brush, paint a layer of melted chocolate onto the egg moulds. Paint slightly beyond the edge of the mould. Don't make the layers too thick.
6) Put the moulds in the fridge for 10 to 15 minutes (such that the chocolate is no longer runny). Then repeat step 5: keep repeating this process until all chocolate has been used up. This amounted to 14 layers of chocolate, over the course of nearly 4 hours.
7) Leave in the fridge overnight.
8) With a sharp knife, cut the excess chocolate off that protrudes above the top of the mould. This should leave you with a flat chocolate plateau about half an inch wide all the way around.
9) Take the mixture out the fridge, and separate about 1/3rd of it into a separate bowl, and add lots of colouring to it. We used yellow food colouring, but that was a bit of an oversight - creme eggs are actually orange in the middle.
10) With the egg halves still in their moulds, gradually add in the uncoloured mixture. Spread it around with a knife, making sure to spread the mixture up above the height of the mould - this is because the mixture is slightly elastic, and always returns to a smaller size than initially spread into. Leave a large dent in the middle of each egg.
11) Fill the middle dents with the coloured mixture. Pray that you have enough to fill up to the top. Then leave in the fridge overnight (again).
12) Melt some more chocolate, and spread a thick layer all around the rim of one egg half. With both halves still in their moulds, place them adjacent such that they are symmetrical.
13) Each egg half weighs approximately 2.5kg. Enlist the help of a fellow human being, and have them slowly lift up one egg half while you lift up the other, rotating both halves upwards until they stick together forming an egg shape, oddly enough.
14) Carefully place the whole egg in a bowl for stability, and press around the mould to ensure the chocolate forms a tight seal all the way around. Place in the fridge for 1 hour.
15) Remove the egg from the fridge, and placing it horizontally in a bowl again, lift the top mould off. Cut all the excess chocolate off. Flip the egg over and remove the other mould. Use paper towels when touching the egg to avoid it melting.
16) Pose the egg for some saucy shots.
17) Wonder what on earth you will now do with this monstrosity. Take into account the following facts:
-It weighs 5kg;
-It contains approximately 4000% of your GDA of sugar;
-It is equivalent to about 125 Cadbury creme eggs.

marbs posted on October 30, 2012 at 12:25 AM

Slower Speed of Light in Games

Wouldn't it be cool to see the effects of special relativity very visibly by lowering the speed of light? Well now you can by playing this game!

Developed by MIT Game Lab, you can download A Slower Speed of Light here.

The aim of the game is simple: collect 100 orbs. With each orb you collect, the speed of light is lowered. Things start getting a bit psychedelic when you have upwards of 80 orbs, due to the Doppler effect shifting colour hues, and warped models from length contraction.

Another game employing this same idea is Velocity Raptor. I found velocity raptor a lot more informative, plus it takes you through the effects of special relativity one step at a time. The puzzles are a lot harder though. I got up to level 31.

To finish, it seems that Frog Fractions didn't get much love in my last blog, perhaps because I didn't sell it too well. Let me try again!

In Frog Fractions, you play as a frog who eats bugs and collects fruit. Use fruit to buy upgrades. Upgrade your lily pad to a turtle, and swim underwater to find a stash of fruit. Use all that fruit to upgrade you turtle to a dragon, and then attach a warp drive. Now you can fly your frog on your dragon through space to combat the bugs in asteroids on the way to Mars! But oh no, you get taken to court on Mars, and sentenced to prison. You manage to escape from prison, swimming through the underwater caves while listening to the history of boxing. Finally, you find an old space ship, and play a text adventure to set up the targeting computer and put goo in the sleeping pod. You can at last fly back home, where you find yourself president of the world, and in charge of the economy. To become rich, you can print as much money as you like in order to aid in your bug porn business.

That is Frog Fractions. I apologise that I had to ruin the whole story.