Question

How do professionals do boundaries in a 2D game? The way I do is say I don't want the sprite to move into a certain area:

//Example
if ((playerPosX >= 825) && (playerPosX  <= 910)&& (playerPosY >= 170) && (playerPosY <= 255)) {
    //do nothing
}else{
    //move
}

But some games out there have a lot of boundaries so I'm wondering, is there an easier way. I don't think there is any way someone would use the above method throughout a whole game, just to block of movement.

EDIT: My question is mainly regarding a game where you can walk around, similar to Pokemon or final fantasy

Was it helpful?

Solution 2

Two possibilities come to my mind,

  • 1st describe the blocked areas with polygons and do a point in polygone test to determine whether the sprite may move to this position.

  • 2nd like in an image manipulation programm create some kind of a mask (layer), where zero bits indicate the position where you can move and ones for the blocked areas. This can be extended to indicated depth see also z-buffer to partially hide a sprite,

EDIT:

if ( mask[ nextY ][ nextX ] == 0 ) {
   currX = nextX;
   currY = nextY;
}

assuming all variables are int and mask is a 2d int array.

OTHER TIPS

It depends on the game.

  • In grid-based games, then you often do a lookup into the grid whenever a sprite moves to check whether it has moved into a "blocked" area. If so, detect the collision and/or perform some corrective movement of the sprite. This is simple and efficient, the only issue is that it restricts you to designing your level/scenery around a grid structure.
  • In games with very dynamic environments (e.g. lots of moving asteroids) then you would typically have a collision detection system based on spatial partitioning (e.g. a quadtree) that is used to detect collisions between arbitrary objects. Each object would typically have a bounding box.
  • In games with complex but fixed geometry defined by vectors, you might pre-compute a BSP-tree and use this for fast detection of collision with scenery. This technique is common in 3D FPS games, but it works in 2D too.

Many games would actually use a combination of the above: e.g. a BSP tree for fixed scenery, and a quadtree for dynamic objects managed by a physics subsystem, for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top