I am working on a voxel engine, currently I am stuck on the creation of blocks. You can ignore the part about LWJGL/OpenGL, that is not the problem. When I try to create the blocks I get a bunch of java.lang.ArrayIndexOutOfBoundsException errors.
package LWJGL.TESTS.WORLS;
import java.util.Random;
public class Chunck {
private int x, y, z, id;
Chunck(int id, int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
this.id = id;
}
This is the part that doesn't work which no post have been able to answer the fix:
` void loadChunck(){
//x
for(int x = 1; x < 16; x++){
//z
for(int z = 1; z < 16; z++){
//y
for(int y = 1; y < 128; y++){
try{
Block[][][][][][] blockObject = new Block[16][16][128][0][0][0];
blockObject[x][y][z][this.x][this.y][this.z] = new Block(x, y, z, this.x, this.y, this.z, String.valueOf(x)+String.valueOf(y)+String.valueOf(z), this.id);
}catch(java.lang.ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
}
}
}
}
This is the block class:
package LWJGL.TESTS.WORLS;
import org.lwjgl.opengl.GL11;
public class Block {
private int x, y, z, cx, cy, cz, cID;
String ID;
Block(int x, int y, int z, int cx, int cy, int cz, String ID, int cID){
this.x = x;
this.y = y;
this.z = z;
this.cx = cx;
this.cy = cy;
this.cz = cz;
this.ID = ID;
this.cID = cID;
System.out.println("Block ID: " + ID);
System.out.println("Block Chunck ID: " + cID);
System.out.println("Block X: " + x);
System.out.println("Block Chunck X: " + cx);
System.out.println("Block Y: " + y);
System.out.println("Block Chunck Y: " + cy);
System.out.println("Block Z: " + z);
System.out.println("Block Chunck Z: " + cz);
}
private Block[][][][][][] InitBlock(){
return null;
}
public void render(){
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f(this.x, this.y,-1*this.z);
GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
GL11.glVertex3f(-1*this.x, this.y, this.z);
GL11.glVertex3f(this.x, this.y, this.z);
GL11.glColor3f(this.x,0.5f,0.0f);
GL11.glVertex3f(this.x,-1*this.y, this.z);
GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
GL11.glColor3f(1.0f,0.0f,0.0f);
GL11.glVertex3f(this.x, this.y, this.z);
GL11.glVertex3f(-1*this.x, this.y, this.z);
GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
GL11.glVertex3f(this.x,-1*this.y, this.z);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
GL11.glVertex3f(this.x, this.y,-1*this.z);
GL11.glColor3f(0.0f,0.0f,1.0f);
GL11.glVertex3f(-1*this.x, this.y, this.z);
GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
GL11.glColor3f(1.0f,0.0f,1.0f);
GL11.glVertex3f(this.x, this.y,-1*this.z);
GL11.glVertex3f(this.x, this.y, this.z);
GL11.glVertex3f(this.x,-1*this.y, this.z);
GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
GL11.glEnd();
}
}
This is the Saves class (ignore this):
package LWJGL.TESTS.WORLS;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class Saves {
void Save(){
PrintWriter writer = null;
try {
writer = new PrintWriter("data.txt", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.print("");
writer.close();
}
void Load() throws FileNotFoundException{
Scanner scanner = new Scanner(new File("data.txt"));
while(scanner.hasNextInt()){
System.out.println(scanner.nextInt());
}
}
}
And finally this is the main class, sorry if this post is really long:
package LWJGL.TESTS.WORLS;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
Saves load = new Saves();
Chunck chunck = new Chunck(1, 0, 0, 0);
chunck.loadChunck();
load.Save();
load.Load();
}
}
new Block[16][16][128][0][0][0]
, will have zero room for any elements, and you can never assign anything into it. What were you trying to do? – ajb Jun 7 '15 at 1:44