gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Platformer Game Coding

#33465 - Celeryface - Mon Jan 03, 2005 9:07 pm

I'm just starting to write a platformer game-type demo, and I was wondering if anyone has seen a site/FAQ for the collision detection for this game type?

For example: Collision detection between the player and the platform, or the player and the enemy.

It doesn't have to be GBA-specific, but rather the theory on this subject, and on the way to make the player jump (for example the way Super Mario jumps in Super Mario Bros. 3).

Thanks in advance. :)

#33485 - BlackAura - Tue Jan 04, 2005 11:04 am

The collision detection depends entirely on how you're storing the level data, and how you're moving the objects around. For 3D games, there's really only one basic way to do it (and a few variations), but for 2D games there are many, many more ways. Although, since virtually everything on a GBA is going to be tile-based, it narrows it down a little.

Movement is a bit easier. Probably the best way is to simulate how objects move in the real world. Every object (including the player) should have a position, a velocity vector, and an acceleration vector. Every frame, you add the velocity vector (or some scalar factor of it) to the position vector, and you add the acceleration vector to the velocity vector. To simulate gravity, you set the acceleration vector to straight down.

For example (in C-style pseudo code), you might do something like this:
Code:
variable_type player_x, player_y, player_vx, player_vy;

while(main_loop_is_doing_something)
{
    // Player controls
    if((getJoypad() && BUTTON_A)&&(playerOnGround))
    {
        player_vy = -100;
        playerOnGround = false;
    }

    // Move the player
    player_x += player_vx;
    player_y += player_vy;

    // Check for collisions with the ground
    if(playerHitTheGround())
    {
        playerOnGround = true;
        player_vy = 0;
    }

    // Apply gravity if the player is in the air
    if(playerOnGround)
        player_vy += 1;


That's just a basic outline, and you'd have to change it to allow for more complex movements, state changes, and proper collision detection. This technique doesn't work very well if you're using integers for the position and velocity of the objects, so it's best if you use floating point on systems with an FPU (not a GBA, but a PC or something), or fixed point values on systems without an FPU (like a GBA).

As for the collision detection, it depends on exactly what you want to do with it. What you need to do is check if your player will pass through a wall / the floor after it has moved, and then react to that collision appropriately.

Generally speaking, unless you want collision with non-square shapes, you basically want to test if two bounding boxes are intersecting. The simplest way is to first check if it's not intersecting - do some test to determine if the objects are not intersecting. If you fail all of those tests, then you have a collision. For example:
Code:
typedef struct thing_s
{
   int x, y;
   int width, height;
} thing_t;

// Returns true if two things are overlapping
int isOverlapping(thing_t *thing, thing_t *other)
{
   // Is thing to the left of other?
   if( (thing->x + thing->width) < other->x )
      return 0;
 
 // Is thing to the right of other?
 if( (other->x + other->width) < thing->x )
  return 0;

 // Is thing above other?
 if( (thing->y + thing->height) < other->y )
  return 0;

 // Is thing below other?
 if( (other->y + other->height) < thing->x )
  return 0;

 // They must be colliding then...
 return 1;
}


If you want to make sure that works, get a pen and a piece of paper and draw it all out.

If you want to do collisions with non-square shapes, you'd best learn some geometry.

#33496 - identitycrisisuk - Tue Jan 04, 2005 3:22 pm

There's a few threads I've started around here that might be useful to you (or not):

Walking down diagonal tiles
Methods of jumping in a platform game

The first one goes into collisions a bit using a non square tile but one that isn't very hard to work out collisions. The other one has some discussion on how you would jump using velocity and an array of set velocity values (not something I've tried).
_________________
Code:
CanIKickIt(YES_YOU_CAN);

#33509 - Celeryface - Tue Jan 04, 2005 7:01 pm

Thanks for the replies :)

Does anyone have any info on using large tiled maps for scrolling? I saw a thread on here about stiching backgrounds together, but I'm not sure how to do that or f that method is the correct way.

Thanks! :)