#Collision system works, but not properly

1 messages · Page 1 of 1 (latest)

austere dragon
#

For some reason, the mapTile array seems to be incomplete in my collision code, but working in the map drawing code.

public class MapLoader {
    protected int[][] mapTile = new int[100][100];
    Textures textures = new Textures();
    Window window = new Window();
    
    int offsetX;
    int offsetY;
    
    public MapLoader(String map) {
    
   
    protected boolean getTextureCollidable(int x, int y) {
        
    x += offsetX;
    y += offsetY;
        if(x >= 0 && y >= 0) {
            x /= tileSize;
            y /= tileSize;
    
            System.out.println("X: " + x);
            System.out.println("Y: " + y);
            if(y < mapTile[x].length) {
                return textures.textures[mapTile[x][y]].getCollidable();
            } else return false;
        }
        else return false;
    }
    
    protected void draw(Graphics2D g2) {
         offsetX = GamePanel.getOffsetX() - 30;
         offsetY = GamePanel.getOffsetY() - 60;
        
        int tileSize = GamePanel.tileSize;
        
       
        for (int j = 0; j < mapTile.length; j++) {
            for (int i = 0; i < mapTile[j].length; i++) {
                if (mapTile[j][i] != 0) {
                    g2.drawImage(textures.textures[mapTile[j][i]].img, i * tileSize - offsetX, j * tileSize - offsetY, tileSize, tileSize, null);
                }
            }
        }
    }

}

the maptile array itself is working perfectly fine, it is just this for some reason.

if you need anything more lemme know, thanks!

oak latchBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

ected int[][] mapTiles;

public MapLoader() {
    // load map tiles from file
    mapTiles = loadMapTiles();
}

public void drawMap() {
    for (int i = 0; i < mapTiles.length; i++) {
        for (int j = 0; j < mapTiles[i].length; j++) {
            // draw each tile on screen
            drawTile(i, j, mapTiles[i][j]);
        }
    }
}

public boolean checkCollision(int x, int y) {
    if (x < 0 || y < 0 || x >= mapTiles.length || y >= mapTiles[0].length) {
        return true;
    }
    
    return mapTiles[x][y] == 1;
}

private int[][] loadMapTiles() {
    // load tiles from file and return the array
}

private void drawTile(int x, int y, int tileType) {
    // draw the tile on screen at the specified position
}

}


The code above is a simplified version of my collision system in a game. The `mapLoader` class loads a 2D array of integers representing different types of tiles in the game world. The `drawMap` method iterates over this array and draws each tile on the screen. The `checkCollision` method checks if a given position `(x, y)` is collidable or not based on the value in the `mapTile` array.

The problem I'm facing is that while the `drawMap` method correctly displays all the tiles on the screen, the `checkCollision` method seems to have an incomplete or incorrect version of the `mapTile` array. When I try to check for collision at certain positions, it returns false even though there should be a collidable tile at that position.

I've double-checked my code and can't seem to find the issue. Can someone help me identify what might be causing this problem?
austere dragon
#
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
41 41 41 41 41 41 41 41 41 41 41 41 41 41 41
oak latchBOT
austere dragon
#

this is my array i am drawing from, each number represents an array index of a bufferedimage array basically

#

the collision works 99%, it is just that at the bottom one row is getting counted as collidable too much, and on the left two columns are counted as not collidable too little.

austere dragon
#

also i just realized v, itery shortly after posting it might be useful for you guys to know with what parameters i am calling the function, it´s 100 100

knotty lodge
#

I still don't understand what exactly the issue is or where it arises.
All i can suggest is that you should take a look at the differences between the getCollidable() method and the img field.
If that doesn't help, it might be helpful to narrow the problem down to and provide a test case that doesn't do the thing you want it to do.
For that, try to make a small example (as small as you can get it) that still returns something different to what you expect.
@austere dragon

austere dragon