0

I'm working on a little jump an run game, but my collision detection don't work properly. This is the backbone of my collision detection:

for(int i = 0; i < Frame.teilesArray.size();i++){
    if(Frame.teilesArray.get(i).getimgInt()== 0 || Frame.teilesArray.get(i).getimgInt()== 1){
        if(bounding.intersects(Frame.teilesArray.get(i).getBounding())){
            Rectangle intersection = (Rectangle) bounding.createIntersection(Frame.teilesArray.get(i).getBounding());
        }
    }
}

And this is how I try to solve but it doesn't work:

public void update() {
    for(int i = 0; i < Frame.teilesArray.size();i++){ //teilesArray is a Array with all tiles from the map (every block has 64x64 px)
        if(Frame.teilesArray.get(i).getimgInt()== 0 || Frame.teilesArray.get(i).getimgInt()== 1){ // means that only the two special blocks will check of collision not all blocks only this blocks
            if(bounding.intersects(Frame.teilesArray.get(i).getBounding())){ // bounding is the rectangle of the Player and Frame.teilesArray.get(i).getBounding() is the rectangle of one block in the Array list
                Rectangle intersection = (Rectangle) bounding.createIntersection(Frame.teilesArray.get(i).getBounding());  // made a new rectangle out off the intersection

            if (bounding.OUT_TOP < Frame.teilesArray.get(i).getBounding().OUT_BOTTOM ) {
                ply_y += intersection.getHeight();
            }

             if (bounding.OUT_LEFT < Frame.teilesArray.get(i).getBounding().OUT_RIGHT && richtung == 2) { //richtung links
                ply_x += intersection.getWidth();
            }

             if (bounding.OUT_BOTTOM > Frame.teilesArray.get(i).getBounding().OUT_TOP ) {
                ply_y -= intersection.getHeight();
            }

             if (bounding.OUT_RIGHT > Frame.teilesArray.get(i).getBounding().OUT_LEFT && richtung == 1) { //richtung rechts
                ply_x -= intersection.getWidth();
            }
                }
            }
        }

the hole code:

 package main;

import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Player {

    private Rectangle bounding;

    private float ply_x;
    private float ply_y;

    private float f_dx;
    private float f_dy;
    private float f_gravity;

    private float f_counter;
    private float neededTime;

    private boolean b_air;
    private boolean b_airimg;
    private boolean letzterichtung;

    private int richtung;

    private BufferedImage playerImage;
    private BufferedImage[] laufr; 
    private BufferedImage[] laufl; 


    public Player() {

        laufr = new BufferedImage[3];
        laufl = new BufferedImage[3];

        ply_x = 0;
        ply_y = 640;

        f_counter = 0;
        neededTime = 10;

        f_dx = 5;
        f_dy = -9;
        f_gravity = 0.4f;

        b_air = false;
        b_airimg = false;
        letzterichtung = true;

        richtung = 0;

        try {
            playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 0, 64, 64);
            laufr[0] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(64, 0, 64, 64);
            laufr[1] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(128, 0, 64, 64);
            laufr[2] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(192, 0, 64, 64);
            laufl[0] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(64, 64, 64, 64);
            laufl[1] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(128, 64, 64, 64);
            laufl[2] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(192, 64, 64, 64);
        } catch (IOException e) {e.printStackTrace();}

        bounding = new Rectangle((int) ply_x, (int) ply_y, playerImage.getWidth(), playerImage.getHeight());

    }

    @SuppressWarnings("static-access")
    public void update() {
        for(int i = 0; i < Frame.teilesArray.size();i++){ //teilesArray is a Array with all tiles from the map (every block has 64x64 px)
            if(Frame.teilesArray.get(i).getimgInt()== 0 || Frame.teilesArray.get(i).getimgInt()== 1){ // means that only the two special blocks will check of collision not all blocks only this blocks
                if(bounding.intersects(Frame.teilesArray.get(i).getBounding())){ // bounding is the rectangle of the Player and Frame.teilesArray.get(i).getBounding() is the rectangle of one block in the Array list
                    Rectangle intersection = (Rectangle) bounding.createIntersection(Frame.teilesArray.get(i).getBounding());  // made a new rectangle out off the intersection

                 // adding variables for the object the player can collide with
                    double minX = Frame.teilesArray.get(i).getBounding().getMinX();
                    double maxX = Frame.teilesArray.get(i).getBounding().getMaxX();
                    double minY = Frame.teilesArray.get(i).getBounding().getMinY();
                    double maxY = Frame.teilesArray.get(i).getBounding().getMaxY();

                    if (bounding.getMinY() < maxY && bounding.getMaxY() > minY ) {
                        // collision is vertical
                        if (bounding.getMaxY() > minY) {
                            System.out.println("Player auf Block: Block:       MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Höhe="+intersection.getHeight());
                            // bottom of player has passed the top border of collision object, move up
                            ply_y -= intersection.getHeight();
                            bounding.x = (int) ply_x;
                            bounding.y = (int) ply_y;
                        } else {
                            System.out.println("Player unter Block: Block:     MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Höhe="+intersection.getHeight());
                            // opposite case, move down
                            ply_y += intersection.getHeight();
                            bounding.x = (int) ply_x;
                            bounding.y = (int) ply_y;
                        }
                    }

                    if (bounding.getMinX() < maxX && bounding.getMaxX() > minX ) {
                        // collision is horizontal
                        if (bounding.getMaxX() > minX) {
                            System.out.println("Player Links von Block: Block: MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Breite="+intersection.getWidth());
                            // right border of player has passed the left border of collision object, move left
                            ply_x -= intersection.getWidth();
                            bounding.x = (int) ply_x;
                            bounding.y = (int) ply_y;
                        } else {
                            System.out.println("Player Rechts von Block: Block: MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Breite="+intersection.getWidth());
                            // opposite case, move right
                            ply_x += intersection.getWidth();
                            bounding.x = (int) ply_x;
                            bounding.y = (int) ply_y;
                        }
                    }

                }
            }

        }

        if(getBounding().x >= 400){
            ply_x = 399;
        }

        if(getBounding().x <=0 ){
            ply_x = 0;
        }

        if(keyCheck.keysCheck(KeyEvent.VK_A)){
            ply_x -= f_dx;
            if(b_air == false){
                if(f_counter >= neededTime*1.5f){
                    f_counter = 0;
                }
                if(f_counter == 0) {
                    playerImage = laufl[0];
                    f_counter++;
                }else if(f_counter == 5){
                    playerImage = laufl[1];
                    f_counter++;
                }else if(f_counter == 10){
                    playerImage = laufl[2];
                    f_counter++;
                }else {
                    f_counter++;
                }
            richtung = 2;
            letzterichtung = false;
            }
        }
        if(keyCheck.keysCheck(KeyEvent.VK_D)){
            ply_x += f_dx;
            if(b_air == false){
                if(f_counter >= neededTime*1.5f){
                    f_counter = 0;
                }
                if(f_counter == 0) {
                    playerImage = laufr[0];
                    f_counter++;
                }else if(f_counter == 5){
                    playerImage = laufr[1];
                    f_counter++;
                }else if(f_counter == 10){
                    playerImage = laufr[2];
                    f_counter++;
                }else {
                    f_counter++;
                }
            }
            richtung = 1;
            letzterichtung = true;
        }

        if(keyCheck.keysCheck(KeyEvent.VK_D)==false && keyCheck.keysCheck(KeyEvent.VK_A)==false){
            if(richtung == 1 || letzterichtung == true && b_air == false && b_airimg ==false){
                try {
                    playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 0, 64, 64);
                } catch (IOException e) {e.printStackTrace();}
            }
            else if(richtung == 2 || letzterichtung == false && b_air == false && b_airimg ==false){
                try {
                    playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 64, 64, 64);
                } catch (IOException e) {e.printStackTrace();}
            }
            richtung = 0;
        }

        if(keyCheck.keysCheck(KeyEvent.VK_SPACE)){
            b_air = true;
        }   

        if(b_air ==true){
            if(b_airimg == false){
                if (letzterichtung == true)
                {
                    try {
                        playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(256, 0, 64, 64);
                    } catch (IOException e) {e.printStackTrace();}
                }else {
                    try {
                        playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(256, 64, 64, 64);
                    } catch (IOException e) {e.printStackTrace();}
                }

                b_airimg = true;
            }
            if(ply_y >= 646){
                f_dy = -9;
                b_air = false;
                b_airimg = false;
            }
            f_dy += f_gravity;
            ply_y += f_dy;
        }

        bounding.x = (int) ply_x;
        bounding.y = (int) ply_y;
    }

    public Rectangle getBounding() {
        return bounding;
    }

    public BufferedImage getPlayerimage() {
        return playerImage;
    }

    public void setPlayerLocation(int ply_x, int ply_y){
        this.ply_x = ply_x;
        this.ply_y = ply_y;
        bounding.x = (int) this.ply_x;
        bounding.y = (int) this.ply_y;
        System.out.println(""+this.ply_x+" "+this.ply_y);
    }
}
Sam
  • 7,252
  • 16
  • 46
  • 65
TheSorm
  • 1
  • 2
  • 3
    For better help sooner, post an [SSCCE](http://sscce.org/). See [this SSCCE](http://stackoverflow.com/a/14575043/418556) for tips.. – Andrew Thompson Jan 02 '14 at 16:52
  • 1
    In what way is it not working? Do you get a compilation error? – David Jan 02 '14 at 17:00
  • This page do not help becorse i can see when the player colide a block but i do not can react right. no i only fall thruth the bottom, but the right and left collision and reaction works somtimes but mometimes the player can go thruth a wall. – TheSorm Jan 02 '14 at 17:09
  • @TheSorm Did you write > instead of < for the vertical cases? It seems to me that the player coordinates should be in the if cases. If you want better suggestions, tell us what the stuff in your code means. What is "bounding"? What is "teilesArray"? – David Jan 02 '14 at 17:12
  • @David hope you understant my comments in the edited question ;) – TheSorm Jan 02 '14 at 17:36

1 Answers1

0

Try this instead of your 4 if cases:

// adding variables for the object the player can collide with
double minX = Frame.teilesArray.get(i).getBounding().getMinX();
double maxX = Frame.teilesArray.get(i).getBounding().getMaxX();
double minY = Frame.teilesArray.get(i).getBounding().getMinY();
double maxY = Frame.teilesArray.get(i).getBounding().getMaxY();

if (bounding.getMinY() < maxY && bounding.getMaxY() > minY ) {
    // collision is vertical
    if (bounding.getMaxY() > minY) {
        // bottom of player has passed the top border of collision object, move up
        ply_y -= intersection.getHeight();
    } else {
        // opposite case, move down
        ply_y += intersection.getHeight();
    }
}

if (bounding.getMinX() < maxX && bounding.getMaxX() > minX ) {
    // collision is horizontal
    if (bounding.getMaxX() > minX) {
        // right border of player has passed the left border of collision object, move left
        ply_x -= intersection.getWidth();
    } else {
        // opposite case, move right
        ply_x += intersection.getWidth();
    }
}
David
  • 943
  • 1
  • 10
  • 26
  • thx but do not work at all -.- i fall truth the grund an do not stay on the grund – TheSorm Jan 02 '14 at 20:25
  • Ok. Even if I did everything correctly, there's still a number of ways you could fail. You need to solve this with debugging. Focus on the vertical boundary checks. Print the coordinates being compared. Add debug messages inside every if block so you can see if you reach the code. Don't take anything for granted. – David Jan 02 '14 at 20:37
  • the problem is that when the player is on top he is the same time right or left , and this made it dont work – TheSorm Jan 02 '14 at 21:51
  • What i only get when starting the game without pressing a key http://i.stack.imgur.com/tUBvk.jpg – TheSorm Jan 02 '14 at 22:33
  • From your image, it appears the player character falls 64 pixels per frame. That's probably way more than you want. If the ground is thin (height < 64) it might be possible for the player to fall through it. Have you added code to print a message when any of the if cases execute? – David Jan 02 '14 at 22:46
  • you can ride it in the image under the game frame, first the if case for left then for top then for left then for top and then for left – TheSorm Jan 02 '14 at 22:54
  • What does it look like exactly when the player falls through? What is the coordinates and size/dimension of the ground block? – David Jan 02 '14 at 22:57
  • What is the coordinates and size/dimension of the ground block? – David Jan 02 '14 at 23:14
  • the cordinates are in the consol after "Block:" and every block is 64x64 px – TheSorm Jan 02 '14 at 23:15
  • The player is jumping between MaxY= 704 and 739 every other row. Why do you think that happens? – David Jan 02 '14 at 23:20
  • Seems like the block is moving in the first picture? I can't find the error. You can try and add more debug messages. Try to change stuff systematically. Good luck. – David Jan 03 '14 at 00:00