Polar bear

Xmas game part 1

I like to do something special for Christmas. Most years I make my own custom Christmas cards. 5 years ago I produced a small game to celebrate and I want to try producing a Christmas themed game again.

Step one is to come up with a concept. I’ve spent a while thinking about this, the game needs to fulfil a few criteria:

  • It needs to be simple enough that I can implement it in a few days
  • The animation needs to be really simple (I have no real animation skills)
  • The game needs to be accessible, suitable for all ages and abilities to play
  • It needs to be fun
  • It needs to have some link to the festive season

After studying a few different types of games and thinking very hard about how I can produce a game I came up with the following idea. A platformer which involves a polar bear jumping between platforms. The game ends when the bear ends up in the water. Points are scored for each jump successfully completed between platforms. The game starts off easy with small gaps and large flat platforms. As the game progresses the gaps get larger, the platforms become smaller, inclined and slippery. The image at the top of the post is my first drawing of the concept.

The next step splits the game into two parts: the game mechanics and the styling. The game mechanics includes all the physics, rules and controls of the game whilst the style includes all the animation of the objects and all the different images and textures needed. Once these two parts are completed we can integrate them and test out the game.

I’m going to start with the game mechanics. There are two objects in this game: the polar bear and the platforms. The platforms will scroll horizontally to give the apparent forward (or backward) motion of the bear whilst the bear will move vertically to give its vertical motion.

There will be 3 inputs to the game, one input will make the bear run faster (accelerate), one will make the bear run slower (decelerate) and one will make the bear jump. The longer the jump control is held down the more powerful the jump. The jump is commenced as soon as the jump control is released.

I can now start putting together the equations to model the behaviour of the bear object. The bear has 4 parameters: horizontal and vertical displacement sx and sy and horizontal and vertical velocity vx and vy. At regular intervals (say 50 times per second) the forces on the bear are calculated and this affects the velocity parameters. The velocity parameters are then used to update the displacement parameters. Once the new displacement parameters are known, collisions can be detected. I’ll come back to collisions.

First lets look at the forces on the bear. When in the air there are two forces affecting the bear: gravity and air resistance. Gravity always acts only on the vertical component and has a constant effect of magnitude g (gravitational acceleration). Air resistance acts proportionally to the velocity of the bear in the direction opposite of the velocity. We can easily split this into a vertical and a horizontal component.

This gives us our two equations:

vxn+1 = a * vxn

vyn+1 = b * (vyn – g)

where a is the horizontal aerodynamic parameter, b is the vertical aerodynamic parameter and g is the gravitational parameter. (a and b can be the same or can be different to give slightly more adjustable physics)

When on a platform there is a further force from the player controlled acceleration/deceleration. We also need to consider the effects of slippery surfaces. The amount of friction between the bear and the surface limits the force in the direction of the surface of the platform.

We can model this as a velocity in the direction of the surface of the platform.

vsn+1 = c * (vsn – g * sin(θ) + d * cos(θ) + e)

where c is platform aerodynamic parameter and d is the friction parameter and e is the player controlled acceleration parameter.

Finally we have a force from jumping, since a jump is instantaneous we simply add an amount of velocity depending on how long the jump control has been pressed for. The jump is always normal to the surface of the platform.

It’s very difficult to give exact numbers for each of the parameters. The parameters give the game it’s feel. We can give more or less gravity, make the bear faster or slower, make the bear more or less responsive and the platforms more slippery. To get a feel for where the game feels right requires some testing.

Finally we have collision detection. If we are currently on the platform we are testing for whether the bear has gone off the edge of the platform. If we are in the air we are testing for if the bear has hit anything. We test if the bottom edge of the bear bounding box has hit the water or the top of a platform and we test if the right hand edge of the bounding box has hit the side of a platform. Only in the case where the bear hits the top of the platform does the game continue. The velocity of the bear in the direction of the surface of the platform is set as the new velocity with the normal component discarded.

This translates to quite a number of rules and equations but put them all together and you get this demo. The platforms are the red objects, the bear is the yellow object. The arrow keys control the acceleration/deceleration and the space bar makes the bear jump. It’s not the finished article by any means but it gives a framework to start from.

Next time I’ll be looking at producing the graphics for the game especially the animation of the bear.

Leave a Reply

Your email address will not be published. Required fields are marked *