public Cube getCube(int x, int y, int z) { // returns a cube if x,y,z lie within it Cube returnCube = null; for (Cube cube : cubes) { if (cube.collisionLooking(-x, -y, -z)) { returnCube = cube; break; } } return returnCube; }
public boolean collisionLooking(float x, float y, float z) { // collision for selecting a cube being looked at boolean collision = false; for (Cube cube : cubes) { if (cube.collisionLooking(-x, -y, -z)) { collision = true; break; } } return collision; }
public boolean collisionJump(float x, float y, float z) { boolean collision = false; for (Cube cube : cubes) { if (cube.collision(-x, -y, -z, controller.playerHeight, controller.playerBox)) { collision = true; controller.jumping = false; break; } } return collision; }
public boolean collision(float x, float y, float z) { // collision detection for a player walking into a cube boolean collision = false; for (Cube cube : cubes) { // a slightly larger bounding box is given to the player // this makes it seem like the player is 3D if (cube.collision(-x, -y, -z, controller.playerHeight, controller.playerBox)) { collision = true; break; } } return collision; }
public void checkSelected() { // this method highlights a cube if it is being looked at // I use a method of ray tracing to check along the players line of sight if (controller.getSelectedWeapon() != 1) return; boolean anythingSelected = false; int maxIterations = 500; float newX = 0, newY = 0, newZ = 0; for (int i = 0; i < maxIterations; i += 1) { newX = -(controller.getX() + (float) (-(i) * (Math.sin(Math.toRadians(controller.getAngleX()))) * (Math.cos(Math.toRadians(controller.getAngleY()))))); newY = -(controller.getY() + (float) (-(i) * (Math.sin(Math.toRadians(controller.getAngleY()))))); newZ = -(controller.getZ() + (float) ((i) * (Math.cos(Math.toRadians(controller.getAngleX()))) * (Math.cos(Math.toRadians(controller.getAngleY()))))); if (collisionLooking(-newX, -newY, -newZ)) { Cube selected = getCube(-(int) newX, -(int) newY, -(int) newZ); int[][] sides = new int[6][3]; float[] differences = new float[6]; for (int j = 0; j < sides.length; j++) { sides[j] = selected.getSideMid(j); } for (int j = 0; j < sides.length; j++) { differences[j] = (float) Math.sqrt( Math.pow((sides[j][0] - newX), 2) + Math.pow((sides[j][1] - newY), 2) + Math.pow((sides[j][2] - newZ), 2)); } float smallest = differences[0]; for (int j = 0; j < differences.length; j++) { if (differences[j] < smallest) smallest = differences[j]; } int side = -1; for (int j = 0; j < differences.length; j++) { if (differences[j] == smallest) side = j; } selected.selectedSide[side] = true; selectedCube = selected; selectedSide = side; anythingSelected = true; break; } } if (!anythingSelected) { selectedCube = null; selectedSide = -1; } }
public boolean collision(float x, float y, float z, Cube cube) { // collision detection for a given cube return cube.collision(-x, -y, -z, controller.playerHeight, controller.playerBox); }
public void renderCubes() { for (Cube cube : cubes) { cube.render(); cube.renderDecals(); } }