Search code examples
javaboundary2d-games

Boundaries in Java Gaming, How do "professionals" do it?


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


Solution

  • 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.