dimanche 31 mai 2015

Issues with If/Then statements

I am using a home made collision detection system for a game I am making, and it works alright- if there is only one object it collides with. It will only give collision detection for the bottom most object. I tested and it registers collision between other objects, but it doesn't limit the movement speed. I think this is because of the nature of if/then statements.

How can I make it check all instanced of blocks with id of 1 for collision?

To explain my variables:

coll is an ArrayList of rectangles that I thought could be used to check for collision.

mU is moveUp, mL is moveLeft, and so on.

xOffset and yOffset are used in drawing the map. They control how far the map is drawn form (0, 0)

vel is the player speed. It equals one in the case.

The relevant code:

    private void blockCheck() {

    for(int i = 0; i < numCols; i++ ) {
        for(int c = 0; c < numRows; c++) {
            if(World[c][i] == 0) {
                solid = false;
            }
            if(World[c][i] == 1)
                solid = true;
            if (solid == true)
                collision(c, i);
            }
        }
    }

private void collision(int c, int i) {
    checkCollisionTop(c, i);
    checkCollisionLeft(c, i);
    checkCollisionRight( c, i);
    checkCollisionBottom(c, i);
}

private void checkCollisionBottom(int c, int i) {

        Rectangle t = new Rectangle(c * 16 + (int)xOffset, i * 16 + 4 + (int)yOffset, pR.width, pR.height);
        if(!coll.contains(t))
            coll.add(t);

        if (pR.intersects(t)) {
            mU = false;
            System.out.println("Collision on Botton");
        } else mU = true;

}

private void checkCollisionRight(int c, int i) {

        Rectangle t = new Rectangle(c * 16 + 4 + (int)xOffset, i * 16 + (int)yOffset, pR.width, pR.height);
        if(!coll.contains(t))
            coll.add(t);
        if (pR.intersects(t)) {
            mL = false;
        } else mL = true;


}

private void checkCollisionLeft(int c, int i) {



    Rectangle t = new Rectangle(c * 16 - 4 + (int)xOffset, i * 16 + (int)yOffset, pR.width, pR.height);
    if(!coll.contains(t))
        coll.add(t);
    if (pR.intersects(t)) {
        mR = false;
    } else mR = true;


}

private void checkCollisionTop(int c, int i) {


    Rectangle t = new Rectangle(c * 16 + (int)xOffset, i * 16 - 4 + (int)yOffset, pR.width, pR.height);
    if(!coll.contains(t))
        coll.add(t);
    if (pR.intersects(t)) {
        mD = false;
    } else mD = true;


}

private void moveCamera() {


    if(keysDown.contains(KeyEvent.VK_W) && mU == true) {

        yOffset = yOffset + vel;
        for (int i = 0; i < coll.size(); i++) {
            coll.remove(i);
            i--;
        }

    }
    if(keysDown.contains(KeyEvent.VK_D) && mR == true) {

        xOffset = xOffset - vel;
        for (int i = 0; i < coll.size(); i++) {
            coll.remove(i);
            i--;
        }
    }
    if(keysDown.contains(KeyEvent.VK_S) && mD == true) {

        yOffset = yOffset - vel;
        for (int i = 0; i < coll.size(); i++) {
            coll.remove(i);
            i--;
        }
    }
    if(keysDown.contains(KeyEvent.VK_A) && mL == true) {

        xOffset = xOffset + vel;
        for (int i = 0; i < coll.size(); i++) {
            coll.remove(i);
            i--;
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire