Tuesday, January 17, 2012

Collision Detection

Now that we can move a little man around the screen with the joystick, how do we keep the man from falling through the platforms?

On The Level

Fahrfall keeps track of three levels of platforms at any given time.  The platform data is stored in an array of three structures, with each structure tracking information for the platform level within a specific section of the screen.  As platforms transition from one section of the screen to another, the corresponding platform data is correspondingly transitioned from one structure in the array to another.

The data in a platform structure tracks the horizontal configuration of a given platform (i.e. where the holes are), the color data for display of that platform, and the vertical position of that platform.  The vertical position of the player object is compared to the vertical position of each platform to determine if they are on adjacent vertical lines.  If so, the player's horizontal position is checked against the platform's configuration to determine whether or not a collision has occurred.

Under The Table

The platform configuration data is stored as an 8-bit mask.  Each bit represents eight pixels worth of platform, so the configuration data spans the width of the 64x96 screen resolution.  But, a direct comparison between the player object's horizontal position and a platform's configuration data would be meaningless.

To check for a collision, either the platform's mask needs to be translated into a series of horizontal position ranges to check or the player object's position needs to be turned into a mask for a bitwise comparison.  Using the player object's position as an index into a lookup table performs the latter task quite nicely.  Once the player object's position mask has been retrieved, a bitwise AND operation between that and a platform's configuration data can determine if there is a collision.  The player movement routines now can react to a collision by moving the player upward when the platforms move upward.

Leap Of Faith

The above works great except for one special case.  If the player object is positioned on the bottom line of the screen, no collision will be detected even when a new platform enters the screen directly beneath it.  Fahrfall does not track information for off-screen platforms, because off-screen platforms literally do not exist.  No one can detect collisions with platforms that don't exist yet!

When playing DOWNFALL (the game that inspired Fahrfall), it is sometimes necessary to take a leap of faith off a platform that is about to disappear from the top of the screen.  In those cases, you don't always know where you will be able to land.  Having a platform appear beneath you at the last second before you fall off the screen is really cool, and I wanted Fahrfall to provide that same possibility.  So, I added a special collision detection check for the case of a new platform entering the bottom of the screen.  Now, all seems to be well. :-)


In the video above, I have done a little hacking to produce a semi-playable demo.  On top of the working collision detection, I have added code to move down whenever the player object is not on top of a platform.  I also sped-up the platform movement a bit to add to the excitement of the game.  There is no scoring, and the movement of the player object is still a bit "wrong".  But, what is there is enough like a game both to be somewhat entertaining and to give a good impression of where Fahrfall is going.

So Far, So Good

I'm excited about how far Fahrfall has come!  It is looking fairly good, and so far there have been few complications.  Wish me luck on keeping-up the pace!

I don't know how much more real progress I'll be able to make this week.  But I do have some more background topics to cover in the meantime.  If there is any significant development progress on Fahrfall, I'll be sure to post about that as well.

Stay tuned!

No comments:

Post a Comment