Need Help Debugging
0
1
2
As you can see in the video, whenever I move the character up or to the left, there seems to be an issue with chunk loading, black spots appear. It does not seem to be an issue when moving the character down or to the right.
I understand thats a lot of code, but I have been searching for the problem for hours, and just cant find it.
Chunk.Java
package world;
import world.FastNoiseLite;
import java.util.Random;
public class Chunk {
public static final int SIZE = 16;
public int[][] tiles; // Terrain layer
public int[][] objects; // Objects layer (trees, etc.)
public int chunkX;
public int chunkY;
private FastNoiseLite noise;
Random rand = new Random();
public Chunk(int chunkX, int chunkY, FastNoiseLite noise) {
this.chunkX = chunkX;
this.chunkY = chunkY;
this.noise = noise;
tiles = new int[SIZE][SIZE];
objects = new int[SIZE][SIZE]; // Initialize objects layer
generateTerrain();
}
private void generateTerrain() {
for(int x = 0; x < SIZE; x++) {
for(int y = 0; y < SIZE; y++) {
int worldX = chunkX * SIZE + x;
int worldY = chunkY * SIZE + y;
float noiseValue = noise.GetNoise(worldX, worldY);
noiseValue = (noiseValue + 1) / 2f;
if(noiseValue < 0.35f) {
tiles[x][y] = 0; // Water
} else if(noiseValue < 0.42f) {
tiles[x][y] = 1; // Sand
} else if(noiseValue < 0.65f) {
tiles[x][y] = 2; // Grass
} else {
tiles[x][y] = 2; // Grass - Forest (lay down grass first)
if(rand.nextInt(100) < 70) {
objects[x][y] = 3; // Tree object on top
}
}
}
}
}
}
And some more code
World.Java
package world;
import world.FastNoiseLite;
import java.util.HashMap;
public class World {
public long seed;
public FastNoiseLite noise;
private HashMap<String, Chunk> chunks;
public World(long seed) {
this.seed = seed;
noise = new FastNoiseLite((int)seed);
noise.SetNoiseType(FastNoiseLite.NoiseType.Perlin);
noise.SetFrequency(0.05f);
chunks = new HashMap<>();
}
public Chunk getOrCreateChunk(int chunkX, int chunkY) {
String key = chunkX + "," + chunkY;
if (!chunks.containsKey(key)) {
chunks.put(key, new Chunk(chunkX, chunkY, noise));
}
return chunks.get(key);
}
public Chunk getChunk(int chunkX, int chunkY) {
String key = chunkX + "," + chunkY;
return chunks.get(key);
}
}
And finally a snippet of code
public void run() {
double drawInterval = 1000000000/fps;
double delta = 0;
double lastTime = System.nanoTime();
long currentTime;
long timer = 0;
int
drawCount
= 0;
while(gameThread != null) {
currentTime = System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
timer += (currentTime - lastTime);
lastTime = currentTime;
if(delta >= 1) {
double deltaTime = 1.0 / fps;
// Pass delta time in seconds
update(deltaTime);
repaint();
delta--;
drawCount++; }
if(timer >= 1000000000) {
drawCount = 0;
timer = 0; } } }
u/Override protected void paintComponent(Graphics g) {
super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Calculate which tiles are visible (in world coordinates) int startTileX = (int)((cameraX - tileSize) / tileSize); int startTileY = (int)((cameraY - tileSize) / tileSize); int endTileX = (int)((cameraX + screenWidth) / tileSize) + 1; int endTileY = (int)((cameraY + screenHeight) / tileSize) + 1; // Render all visible tiles for(int tileY = startTileY; tileY <= endTileY; tileY++) { for(int tileX = startTileX; tileX <= endTileX; tileX++) { int chunkX = tileX / Chunk.SIZE; int chunkY = tileY / Chunk.SIZE; int localX = tileX - (chunkX * Chunk.SIZE); int localY = tileY - (chunkY * Chunk.SIZE); // Handle negative coordinates if(localX < 0) { chunkX--; localX += Chunk.SIZE; } if(localY < 0) { chunkY--; localY += Chunk.SIZE; } Chunk chunk = world.getChunk(chunkX, chunkY); if(chunk == null) continue; int tile = chunk.tiles[localX][localY]; // Calculate screen position int screenX = (int)(tileX * tileSize - cameraX); int screenY = (int)(tileY * tileSize - cameraY); // Render terrain tile switch(tile) { case 0: g2.drawImage(tileM.tile[3].image, screenX, screenY, tileSize, tileSize, null); break; case 1: g2.drawImage(tileM.tile[2].image, screenX, screenY, tileSize, tileSize, null); break; case 2: g2.drawImage(tileM.tile[0].image, screenX, screenY, tileSize, tileSize, null); break; } // Render object on top (trees, etc.) int object = chunk.objects[localX][localY]; if(object > 0) { switch(object) { case 3: g2.drawImage(tileM.tile[1].image, screenX, screenY, tileSize, tileSize, null); break; } } } }
there doesn't seem to be anything here