简体   繁体   中英

block collision issues on a 2D map tiled java

I'm having problems with wall collision. Basically I want my player to stop whenever it collides with a block. Here's what I did so far:

Keylistener set up:

addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_A){
                pressL = true;  

            }

            if(e.getKeyCode() == KeyEvent.VK_D){
                pressR = true;  

            }

            if(e.getKeyCode() == KeyEvent.VK_W){
                pressU = true;  

            }

            if(e.getKeyCode() == KeyEvent.VK_S){
                pressD = true;  

            }
        }

        public void keyReleased(KeyEvent e){
            if(e.getKeyCode() == KeyEvent.VK_A){

                pressL = false; 

            }

            if(e.getKeyCode() == KeyEvent.VK_D){

                pressR = false;


            }

            if(e.getKeyCode() == KeyEvent.VK_W){

                pressU = false;


            }

            if(e.getKeyCode() == KeyEvent.VK_S){

                pressD = false; 


            }
        }   

        public void keyTyped(KeyEvent e){

        }
    });

Player's movement:

public void playerMovement(){ 
   player.horizontalMovement(0);
   player.verticalMovement(0)

   map.horizontalMovement(0);   
   map.verticalMovement(0);     

   if(pressR && !pressL && !pressU && !pressD){
         if(!east){
         toggleRight();                 
         }

         if(collision("east"))
         east = true;                               
     }          

   if(pressL && !pressR && !pressD && !pressU){
        if(!west)
        toggleLeft();                    

        if(collision("west"))
        west = true;
   }

   if(pressD && !pressU && !pressR && !pressL){
        if(!south)
        toggleDown();

        if(collision("south"))
        south = true;
   }

   if(pressU && !pressD && !pressL && !pressR){ 
        if(!north)
        toggleUp();

        if(collision("north"))
        north = true;

   }
}

Here's where the collision test is:

public boolean collision(String loc){

    Rectangle pR = player.getBound();
    Rectangle pM = map.getBound(0, 0);

    if(loc.equals("east")){     
        if(pR.equals(pM)){
            if(west)                
            return false;               

            if(!west)               
            return true;                
        } west = false; south = false;north = false;                            
     }

    if(loc.equals("west"))      
            if(pR.intersects(pM)){
                if(east)                
            return false;   

            if(!east)               
            return true;        
        } east = false; south = false;north = false;
    }

    if(loc.equals("south")){
        if(pR.intersects(pM)){
                if(north)               
            return false;


            if(!north)              
            return true;    


        } north = false; west = false;east = false; 
    }

    if(loc.equals("north")){
        if(pR.intersects(pM)){
                if(south)               
            return false;


            if(!south)              
            return true;                        

        } south = false; west = false;east = false; 
    }

    return false;
}

I set up my code likes this to avoid being stuck whenever I collide with a block I'm testing with. It works but there are a lot of bugs I'm encountering. One example is sometimes I get stuck, or the player can pass through the block with pressing the vertical with the horizontal keys. I'm having problems figuring out the proper algorithm for this. And by the way the direction is based on the viewer's direction not the player's.

Can someone share with me a descent way of do it? Thanks.

For player and walls (blocks) create Shapes (eg Polygon can be used). The use Shape's methods

public boolean intersects(double x, double y, double w, double h);
public boolean intersects(Rectangle2D r);

Or you can create Areas from the Shapes and use

public void intersect(Area rhs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM