Getting stuck on GameDev

Well, as luck would have it, the hugely pressing work project that has taken up almost all of my waking time is coming to an end. If we wanted to really get into specifics, it was actually finished three weeks ago, when management decided to cancel it. In a bout of extreme managerial skill, the desire to have the project not continue was not passed all the way down to those of us that were actually working on the thing. Not that I’m bitter or anything.

There is upside, though! A return to Game Development! I must say, I really missed being able to devote time to it!

Unfortunately, this bit revelation came just a bit too late for me to really dig in to anything overly meaty but I did manage to finish exercise #4, “click to serve the ball”.

This exercise has us starting each “life” with the ball affixed to the paddle instead of the play area; clicking the mouse “unsticks” the ball, and it starts off by launching upwards instead of downwards. This is all very standard in breakout/arkanoid type games, so I’m sure you get the picture without me having to capture one.

I had a bit of a hacky way to accomplish this last week, but I had a bit more of a chance to think about how I wanted it to work, and I ended up with something that logically works the way I originally implemented it, but which is much cleaner (and will get cleaner soon; although I’ve already pushed an update, I’m going to work it a little bit more).

The ball has a new notion of a “locked entity”, or a parent, if you will, and can operate in both locked/parented and unlocked/orphan mode, with unlocked mode being the default mode that works the way the ball has always worked.

When the ball is locked to another entity (e.g. the Paddle), a couple of things change. The first is that when the lock is established, the ball calculates the offset between its own current position and its parents, and the second is that while it is locked, the update method only updates the position to keep it relative to the parent.

The rest is easy; when the ball is reset, it can optionally be given an entity to lock itself to, which causes it to basically just stick in one relative position and do nothing (which, unlike the government, is exactly what we want). A simple mouse click handler checks to see if the ball is currently locked to the paddle entity, and if it is, unlocks it. Once unlocked, the ball immediately starts moving. Easy!

This all works as I want it to, but the code is not quite as clean as I would like it to be. In particular, the ball stores not only the parent entity, but also an offset to apply to it, and the update method has to keep modifying its own global position in order to keep things working. This is a bit of a crufty side effect of my initial hacky implementation.

A much better plan (to my eyes) is that when the ball is locked to an entity, its position should become the offset to the parent (e.g. after calculating the offset, store it directly into the position instead of into a separate member). Additionally, the code to unlock the entity would have to re-apply the parent position to give the ball an absolute world position again.

The rest is easy; the ball just doesn’t do anything in its update method while it is locked (or at least, nothing motion related) and the render method should apply the parent position to the render position provided so that the ball still renders at the appropriate location.

Theoretically the scheme on the whole could be pushed up directly into the framework of the engine itself, possibly with the render handling being smart enough to calculate the absolute world position on its own. That seems like an interesting side project, but I don’t think it’s overly useful here, so I’m going to let that one pass by.

For the next week, I’ll be cleaning up that code a little bit, and then exploring exercise #5, fixing the ball getting stuck on the screen boundary. To be honest, at least a part of that will be checking to see if that can actually happen, since the code I have in place currently for bouncing the ball on the sides of the screen is already enhanced (and stolen from ts-tennis) from what the course originally provided.

Time will tell!