Always a Collision

65 views Asked by At

I created a tilemap and a player (Which is just a squiggle right now). I put the tilemap and the player on the screen, but the player is in the upper left corner, and is always stuck in the tile. I can't figure out how to:

  1. Get it out of the corner and
  2. stop the collision from happening because it's a non-blocked block.

A few things you need to know before the code:

The tilesheet is the sheet with the different tiles on it and it goes

[non-blocked, nonblocked]
[blocked, blocked]

Here's the collision part of my object superclass (I know it looks like a lot but after the beginning you can get the drift):

public void calculateCorners(double x, double y) {

        int leftTile = (int)(x - cwidth / 2) / tileSize;
        int rightTile = (int)(x + cwidth / 2 - 1) / tileSize;
        int topTile = (int)(y - cheight / 2) / tileSize;
        int bottomTile = (int)(y + cheight / 2) / tileSize;

        int tl = tileMap.getType(topTile, leftTile);
        int tr = tileMap.getType(topTile, rightTile);
        int bl = tileMap.getType(bottomTile, leftTile);
        int br = tileMap.getType(bottomTile, rightTile);

        topLeft = tl == Tile.BLOCKED;
        topRight = tr == Tile.BLOCKED;
        bottomLeft = bl == Tile.BLOCKED;
        bottomRight = br == Tile.BLOCKED;


    }
public void checkTileMapCollision() {
    currCol = (int) x / tileSize;
    currRow = (int) y / tileSize;
    x += velX;
    y += velY;
    calculateCorners(x, ydest);

    if (velY < 0) {
        if (topLeft || topRight) {
            velY = 0;
            y = currRow * tileSize + cheight / 2;
            System.out.println("Collision");
        } else {
            y += velY;
        }
    }


    if (velY > 0) {
        if (bottomLeft || bottomRight) {
            velY = 0;
            falling = false;
            y = (currRow + 1) * tileSize - cheight / 2;
            System.out.println("Collision");
        } else {
            y += velY;
        }
    }

    calculateCorners(xdest, y);

    if (velX < 0) {
        if (topLeft || bottomLeft) {
            velX = 0;
            System.out.println("Collision");
            x = currCol * tileSize + cwidth / 2;
        } else {
            x += velX;
        }
    }

    if (velX > 0) {
        if (topRight || bottomRight) {
            velX = 0;
            System.out.println("Collision");
            x = (currCol + 1) * tileSize - cwidth / 2;
            System.out.println("Collision");
        } else {
            x += velX;
        }
    }
}

And here's the most important stuff in my Player class:

public void update() {
    System.out.println("update");
    getNextPosition();
    checkTileMapCollision();
    setPosition((int) xtemp, (int) ytemp);
}

public void setLeft(boolean b) {
    left = b;
}

public void setRight(boolean b) {
    right = b;
}

public void getNextPosition() {
    if (left) {
        velX = -2;
        System.out.println("Left");
    } else if (right) {
        velX = 2;
        System.out.println("Right");
    }
}

public void draw(Graphics2D g) {
    x += velX;
    y += velY;
    g.drawImage(spriteSheet, (int) x, (int) y, null);
}

public void setPosition(int x, int y) {
    this.x = x;
    this.y = y;
}

public void setVel(int velX, int velY) {
    this.velX = velX;
    this.velY = velY;
}

If you need anything else, just tell me and I'll give it to you.

0

There are 0 answers