Runtime Revolution
 
Articles Other News

Sprite Animation: Part 3 – putting it all together

by John Craig

 

The last time we got the car sprite moving around – now we'll add the race track and collision detection from part 1. I've added a test race track image to the test stack from last time.

One of the first steps is detecting when the car hits a barrier - take a look at the following two collisions;

In the first collision, the car must turn to the left to steer out of the collision. In the second, the car angle is roughly the same, but the car must steer to the right. Simply detecting a collision between the car and track won't give us enough information, so I've also added a couple of 'bumper' images which we move along with the car. These two images can be shown and hidden by clicking the 'bumpers' button in the test stack. You'll have to look carefully – they are only small 6x1 pixel images, but that's all we need to detect collisions at the front left and front right of the car.

I've commented the latest version of the test stack – if you search for 'Added', you'll see each addition from the last test stack. It's easiest to look through the stack source and play around with the stack, but here are the main points;

  • A 'uCrash' property has been added to the car to flag that both bumpers have collided – a 'head on' collision.
  • The ability to steer when crashed and stopped has been added – normally the steering only takes effect when the car is in motion.
  • Two collision detection commands have been added – one to check the left bumper against the race track and one for the right bumper against the race track.
  • A check for only one bumper colliding with the track. If this is the case, then we can bounce the car off the track and slow down slightly, rather than coming to a complete stop.
  • We 'shadow' the car each time the update handler runs: the bumper images are moved along with the car.

With these additions, we can now drive the car within the boundaries of the track and bounce off the track if the collision angle is shallow enough.

Improvements, adding effects and debugging!

With a bit of experimentation, you can add sound effects like engine and crash sounds, or add extra car characteristics, like the back end sliding round corners. I asked for feedback on the game demo I had been working on earlier this year and got some interesting feedback;

  • It ran great on Windows
  • It ran poorly on Mac

I ran it on Linux and discovered that, like the Mac, the game play almost ground to a complete halt.

I haven't completely sorted out my own demo yet (getting time is the hardest part), but have already discovered what some of the problems are.

Running the update handler every 30 milliseconds caused Linux to run very slowly. The three parts of the screen being updated were two race cars and player information at the bottom of the screen. By making this handler run every 10 seconds and updating car 1 on the first call, car 2 on the second and player info on the third, the game ran at almost the same speed as the Windows version. The other problem seems to be overlapping controls: some of the controls at the bottom of the screen overlap the racetrack, which must be causing a lengthier screen update under Linux.

I'll report back later when I get some time to experiment and have some more answers, but in the mean time, have fun!

 
©2005 Runtime Revolution Ltd, 15-19 York Place, Edinburgh, Scotland, UK, EH1 3EB.
Questions? Email info@runrev.com for answers.