2 July, 2009 | Written by Vincent Tan Leave a Comment

Back when I was younger (which is an obtuse way of saying “I haven’t the friggin’ idea exactly when”), I dabbled a bit in game development. There was a period when I was studying side scroller games. Remember those? The classic Super Mario Brothers was one of them.

I also noticed that in some of the games, the backgrounds moved. Yes, backgrounds, plural. I could understand forming a background “tile” made up of hills, clouds, trees, grass, flowers, rocks and whatever suited the game as background. But there was something, else, moving in the (for lack of a better word) background.

There was another background layer, moving at a different speed. Wait. Oh, it’s moving at a slower speed.

When I moved that little sprite (that’s representing my sole means of interacting with the game) on the screen to the right, the flowers and trees and rocks sped past to the left. But that faraway mountain was moving to the left at a slower speed. And the overall effect was a realistic simulation of 3D, a semblance of depth in an essentially 2D game.

Now that I think about it, I have one question. How do you calculate how slow the other background should be? I searched high and low, though I found what this effect is called (parallax scrolling), I found no trace of any suggestion to the relative speeds between the 2 backgrounds.

So I did a little thinking. And drawing. I was trying to work out mathematically the slower speed, given the “distance” between the background layers (there’s practically no distance in implementation. Maybe 0.01 units…) and the speed of the background that’s “in front”.

It didn’t make sense, because no matter how I pivot the movement, the calculations don’t work out.

Parallax scrolling backgrounds

L1 and L2 are the “distances” between the respective layers. d1 and d2 are the distances from the objects in question to the perpendicular line formed by the sprite position. v1 and v2 are the velocities of the respective layers moving to the left (or right, depending on how you view this whole thing and how you define the direction… never mind).

The layers aren’t really separate. There is a tiny distance between the layers, say 0.01 units. If you’re in a fully 2D environment, then the farthest layer is drawn, then the next closest layer is drawn, subject to transparency to allow elements from the farthest layer to be shown, and then the playing layer is drawn (where our sprite and other objects are). There’s no distance (between the layers) to speak of in a true 2D rendering environment.

I started with the “don’t move the focus, move everything else” approach, keeping the sprite in place, and moving both backgrounds to the left. This meant pivoting around the sprite. The objects drawn on the other two layers are what our sprite would see in a straight line towards somewhere forward. Those objects should coincide at the “perpendicular” line together.

Since the distances d1 and d2 are obviously different, therefore the velocity (or speed. I’m just trying to be scientifically correct here) of the two objects moving to the left must be different. There lies my problem. It meant the farthest object had to travel faster, contradicting our original observation.

What if we pivoted around the object in the “front” layer? The sprite moves to the right, and the object on the “back” layer moves to the left, and all three line up in a perpendicular line (perpendicular to the layers anyway). Too troublesome. Same with pivoting around the farthest object.

I toyed with the idea of pivoting around the vanishing point. At this point (no pun intended), I decided to give up.

So I assumed that the background image(s) in the “back” layer are appropriately sized with respect to the “front” layer. I decided a simple ratio probably worked best. Thus we have
v2 = v1 / (L1 + L2)
which should give an appropriately slowed velocity.

And now, finally, I’m telling you this. It might not matter. What matters is that you test the velocities, and if the 2 background layers scroll at a pleasing velocity, then there you have it. Ultimately, we’re just trying to simulate a 3D perspective given a 2D environment. If it’s believable, then that’s the correct velocity.

22 June, 2009 | Written by Vincent Tan Leave a Comment

I was sitting down, thinking of an amusing article to write, and I didn’t quite feel up to writing. It’s been a long weekend, and besides, it’s a special day for me today.

32 years old

So I thought, “What the heck”.

Then I thought I should at least leave you with something. So I’ll introduce you to the concept of the birthday paradox. Basically, given a group of 23 people, what’s the percentage chance that any 2 persons in that group share the same birthday? (hint: it’s higher than you think. Here’s a less clinical explanation of the paradox than the one from Wikipedia.).

18 June, 2009 | Written by Vincent Tan Leave a Comment

Have you considered the lifespan of your programs? You might have thought of scaling issues, security concerns and hardware settings. What if your program is still used 5 years down the road? 10 years? 20? Will it still work?

I had the occasion to work on a system. The thing was, it dealt with financial years. In case you’re wondering, a financial year (typically?) starts on April to the following March. So financial year 2009/2010 starts from April 2009 to March 2010. Calendar years are the “normal” ones, starting from January to December of that year.

Where was I? Oh right, the system with the financial years. So the Unix shell script that runs as an interface displayed up to the latest 5 financial years. There’s an accompanying set of C programs, as well as a corresponding set of database tables. It’s not well thought of, but it’s the best under the circumstances.

What happened was, every time a new financial year arrived, a new set of database tables was created (appropriately named of course). And a new set of C programs copied and pasted and edited (to insert, update and delete from the new database tables). And new lines of code added to that Unix shell script. This happens in late February and March, to prepare for the imminent arrival of April (April has more clout than August, even though August is well respected…)

So I did that for one year. Then the next year arrived, and I found myself with 7 financial years worth of program options, but only needed to display 5. The manager and senior colleague were already set to go for the path of least resistance: doing whatever worked last year. I decided otherwise.

I went through the code for the shell scripts, database scripts, stored procedures and C programs. The code were all flexible in that only the financial year portion was changed. So I told them that I’m going to change all the code so that the code took in the financial year as a parameter, or that it automatically calculated the correct financial years. The manager gave me the go ahead, and I went to work quickly, for it was February, and it’s unknown if I could get it to work in time.

There were some problems, and I’ll tell you about them in the 2nd part of this exciting mini-series…

15 June, 2009 | Written by Vincent Tan Leave a Comment

I was wondering, what would happen if we run out of resources on Earth? We’d probably all die, which is kind of depressing. So I thought of how not to run out of resources in the first place.

Earth plant
[image by Andrejs Zemdega]

I have 2 obvious answers. One is regeneration of our resources, through recycling, reusing and reducing. That doesn’t appear to be enough, since we consume too much.

The second answer is intergalactic colonisation. “We can’t control our own planet’s resources, so we’ll just take over another planet!” Even if we disregard the moral and selfish motivation of that reason, there’s still one tiny problem. Our scientific space program isn’t up to par yet. We might run out of resources before our science and technology is advanced enough.

So I came up with another answer. Now, if you assume that humans are driven by needs and wants, then… ok, let me define needs and wants first. Needs are critical to human survival, or at least make it easier to survive. Wants are wishes… let me give examples then. You need food, shelter and clothing (to protect from weather). You want to travel, to play video games and to wear Gucci products.

The distinction between a need and want is subjective (does steak qualify as a need or want?). We don’t need the distinction to be obvious anyway. So what’s the answer? Push the wants into a virtual world.

Create a virtual world where the wants can be satisfied by bits and bytes rather than carbon, wool, oil, water and other natural resources.

For it to work, the experience must be indistinguishable from what can be experienced now. Hence The Matrix. What we call reality, is just a sum total of what our senses tell us anyway. Simulate the senses correctly, and you get “reality”. The game franchise “The Sims” has been steadily heading towards this end. Its limits are the number of objects available (including other Sims), the number of interactions available and the number of sensory inputs.

With the wants satisfied in the virtual world, there should be more resources in the real world. If nothing else, this should give the option of regeneration more time to regenerate resources. Or more time to spread our human genes to other galaxies.

Is the virtual world solution easier to implement (we programmers have much to do then)? Will it upset the economy (most definitely)? Will the virtual world consume more resources (servers have carbon footprints too)?

11 June, 2009 | Written by Vincent Tan Leave a Comment

I’ve played 2 more game sessions since I last failed at rolling dice. I’m starting to get the hang of playing Dungeons and Dragons. In case you’re not a DnD person, I’ll keep most of the deep references out.

Here’s my general observation: It’s actually very easy to miss. Generally speaking, in a neutral setting, where there are no bonuses to hit, the percentage to hit range from 20% to 40%. Let me give you an example.

Suppose our hero fighter is engaged in battle with a kobold minion. He swings his sword at the kobold as a melee attack. In DnD terms, this means to roll 15 or higher on a d20, 15 being the kobold’s armour class (or AC). In mathematical terms, that’s a 6 out of 20 chance of hitting, or 30% chance.

And most of the hit rolls are like this. The most one could hope for, was to need to roll only an 11 or higher on a d20. That’s a slightly less than 50% chance to hit. And that’s the best case scenario.

My experience with video game role playing games was that, you issue an attack and it hits. Usually. Most of the time. I’ve never found the use of support skills or spells significant. I would cast a protection spell to reduce the amount of damage, and that would be the extent.

So I was in a bit of a quandary when playing Dungeons and Dragons. I like magical stuff. The characters wielding magic are controller types, meaning they can deal damage to multiple enemies but do less damage. They are supposed to slow the enemies, daze them, teleport them, immobilise them, lower their defenses, increase their vulnerability and so on. Basically supportive skills.

Now for my previous game, I was offered help in creating a character. So I took it, and let the helper create whatever he deemed fit. And I got a character geared towards those supportive skills. I thought, “Interesting. I thought boosting damage might be better. Oh well.”

And I truly saw the error of my ways for that game. The enemies had super high defenses. The miss rate was like 70% or 80%. Until the bonuses started to stack.

We had 6 players (which was large), and we buffed the 1 or 2 players with high damage. Because if we didn’t, we would never get any of the enemies killed, because we would never hit them. A +1 to your next attack roll, a -2 to the enemy’s defense, a +2 because of that power, a +2 because I used mine.

As for me, I shone at one particular part, where a few enemies bunched up together, and my spell disintegrated them in one shot. Ok, maybe not all of them, but it cleared most of them. Because it allowed the other players to concentrate on that demon with tons of hit points.

And for the last battle, the buffing really helped. There was this ranger, who could hit using two weapons with one of his skills. After we buffed him, he was able to hit with anything better than 1 (because a 1 was an automatic miss). He hit, and started rolling dice for damage. I believe he needed to roll 6 d10’s (and an additional d6 or d8 because he had a critical hit). After stacking all his damage bonuses, he dealt 73 points of damage. He simply needed to hit.

So from the few games I played, I finally realised the power of team work in DnD. As the characters I favoured, I was to harass the enemies, and disrupt them from harassing my team members. My job was to make it harder for the enemy, and make it easier for us to destroy them. My supportive skills are crucial to this.

Because if my team members miss the enemy, little else matters.