In Java programming a game like with maze like structure. How do we check collision with wall? Anyone who knows how the wall object work? Many thanks--Petranilla

########
## ### #
#  ## P#
########

suppose '#' denote the walls in a maze and 'P' is a player and ' ' are free ways to move..if charAt(x-coord,y-coord)=='#'..it's a collision, you cannot move..if charAt(x-coord,y-coord)==' ', it's a free space where you can move

Edited 3 Years Ago by cool_zephyr

Hi Cool,

Many thanks.

I'm using image icon for the background. Hor do I check the wall? Below is the snippet on how I'm checking by getting the values of set bound:

if (x>=20 && x<=80 && y>=-5 && y<= 60 )
sw=1;
else
sw=0;

It will be long and tedious. Any thoughts?

Petranilla

try putting the co-ordinates of the walls in a vector like this

Vector<Point> wallCoords=new Vector<Point>();
//fill the vector with the top-left coordinates of your wall;

int iconWidth=width_of_your_icon;
int iconHeight=height_of_your_icon;

int playerXPos=your player's X-pos;
int playerYPos=your player's Y-pos;

now to check if your player collides with the wall

Iterator it=wallCoords.iterator();
while(it.hasNext()) {
    Point coord=(Point)it.next();
    if((playerXPos>=coord.getX() && playerXPos<=(coord.getX()+iconWidth))
        //collision with wall on x-axis occurred
    //do same checking for y-axis collision
}

Manythanks for the suggestion. I only have one image for the maze. I'll see if I can breakdown and follow your suggestion.

Thank you,
Petranilla

This article has been dead for over six months. Start a new discussion instead.