3D TPP LWJGL Game – Level Generator #5

javaw 2013-01-12 15-25-11-29

LWJGL(Lightweight java game library) game project in third person perspective (TPP) #5

What’s new with that update: Fixed collision detection (Not in 100%) , sometimes player can stuck or go through cube. – Generator for cubes – they are created randomly, using simple loop and random values. Also Player can fly/jump to them – I still need to work on it.

I had problems with collision detection for them, or should I say more with collision response.

Here You can see how it looks:

Btw I should post some usefull sites:

LWJGL wiki – there are a lot usefull tutorials : http://www.lwjgl.org/wiki/index.php?title=Main_Page

OpenGL “Red” Book – very nice to learn OpenGL 1.1 : http://www.glprogramming.com/red/index.html

For people that are intersted with code I will show it below. Remeber, to run this u need to have LWJGL library added to your project.

Class Main:

package main;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.Color;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import static org.lwjgl.util.glu.GLU.gluLookAt;

public class Main {

private boolean running = false;
private Walls walls; // simple wall for tests
private Box box; // small cube in middle’
private Character character;

private float fov = 60; // field of view
private Vector3f pos = new Vector3f(0, 0, 0); // position vector
private Vector3f posfix = new Vector3f(0, 0, 0);
private float rotY = 0; // rotation along Y axis
private float rotY2 = 0; // rotation along Y axis
private float rotX = 0; // rotation along X axis
private float view = 0; // for character view transformations (along z axis)
private float posYheight = 0.0f; // Y pos of character (when he flys by
// pressing Shift)
float z; //variable to move in z axis
float x; //variable to move in x axis

// fog parameters
private float fogNear = 10f;
private float fogFar = 30f;
private Color fogColor = new Color(0f, 0f, 0f, 1f);

String textureloc = “res/wall.png”; //texture for small box
private LevelGenerator level; //object level will create random cubes in scene
private int countL = 32; // number of cubes (and aparently also height
boolean collide; //if coolide then stop gravity

long lastFrame; // time at last frame

int fps; //fps

long lastFPS; //last fps

public Main() {
// set Display

try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle(“twolwjgl”);
Display.create();

} catch (LWJGLException e) {
System.err.println(“LWJGLException! Can’t create Display!”);
e.printStackTrace();
System.exit(0);
}
}

public void init() {
// initialize stuff
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, 800 / 600, 0.3f, 50.0f);
glViewport(0, 0, 800, 600);

// view behind character
gluLookAt(0.0f, 2.0f, 10.0f, 0.0f, 0, 0, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_FOG);

// buffer for colors
FloatBuffer fogColours = BufferUtils.createFloatBuffer(4);
fogColours.put(new float[] { fogColor.r, fogColor.g, fogColor.b,
fogColor.a });
glClearColor(fogColor.r, fogColor.g, fogColor.b, fogColor.a);
fogColours.flip();

// other fog parameters
glFog(GL_FOG_COLOR, fogColours);
glFogi(GL_FOG_MODE, GL_LINEAR);
glHint(GL_FOG_HINT, GL_NICEST);
glFogf(GL_FOG_START, fogNear);
glFogf(GL_FOG_END, fogFar);
glFogf(GL_FOG_DENSITY, 0.0005f);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

running = true;

float l = 20.0f; // helper parametr for Walls

// Box vertices
Vector3f a = new Vector3f(-l, 32, -l);
Vector3f b = new Vector3f(l, 32, -l);
Vector3f c = new Vector3f(l, -3, -l);
Vector3f d = new Vector3f(-l, -3, -l);

Vector3f e = new Vector3f(-l, 32, l);
Vector3f f = new Vector3f(l, 32, l);
Vector3f g = new Vector3f(l, -3, l);
Vector3f h = new Vector3f(-l, -3, l);

walls = new Walls(a, b, c, d, e, f, g, h); // creating Box (walls)

float l2 = 1.0f;
// Box3 vertices

// small cube in middle
box = new Box(new Vector3f(-l2, l2, -4f), new Vector3f(l2, l2, -4f),
new Vector3f(l2, -l2, -4f), new Vector3f(-l2, -l2, -4f),
new Vector3f(-l2, l2, -2f), new Vector3f(l2, l2, -2f),
new Vector3f(l2, -l2, -2f), new Vector3f(-l2, -l2, -2f),
textureloc);

// primitive character object
character = new Character(new Vector3f(-0.4f, 0.3f, 0.1f),
new Vector3f(0.4f, 0.3f, 0.1f),
new Vector3f(0.4f, -0.6f, 0.1f),
new Vector3f(0.0f, -0.6f, 0.1f), new Vector3f(-0.4f, -0.6f,
0.1f), new Vector3f(0.0f, -1.7f, 0.1f), new Vector3f(
0.8f, -1.7f, 0.1f), new Vector3f(-0.8f, -1.7f, 0.1f),
new Vector3f(-0.3f, -3.0f, 0.1f), new Vector3f(0.3f, -3.0f,
0.1f), new Vector3f(0.0f, -1.0f, 0.1f));

level = new LevelGenerator(countL); //create and generate level

getDelta();
lastFPS = getTime();

}

public int getDelta() {
//method to get delta value
long time = getTime();
int delta = (int) (time – lastFrame);
lastFrame = time;

return delta;
}

public long getTime() {
//actual time
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

public void updateFPS() {
//fps counter
if (getTime() – lastFPS > 1000) {
Display.setTitle(“FPS: ” + fps);
fps = 0;
lastFPS += 1000;

}
fps++;

}

public void start() {
// start method

while (running) {
// game loop
int delta = getDelta();
readinput(delta); // read keys
checkCollisions(); // check for collisions
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clearing
// screen buffer

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); // Thanks to this you see through wall when
// looking from other side
glCullFace(GL_BACK);

glColor3f(1, 0.8f, 0.8f);

walls.build(); // make walls
glDisable(GL_CULL_FACE);
box.build(); // make cube
level.generate();

glColor3f(1, 1.0f, 1.0f);
glBegin(GL_LINES); // x axis
glVertex3f(-700.0f, 0.0f, 0.0f);
glVertex3f(700.0f, 0, 0);
glEnd();

glColor3f(1, 0.1f, 1.0f);
glBegin(GL_LINES);// y axis
glVertex3f(0.0f, -500.0f, 0.0f);
glVertex3f(0.0f, 500.0f, 0);
glEnd();

glColor3f(1, 1.0f, 0.1f);
glBegin(GL_LINES);// z axis
glVertex3f(0.0f, 0.0f, -400.0f);
glVertex3f(0.0f, 0.0f, 400.0f);
glEnd();

glLoadIdentity();
// character translation and rotation
glTranslatef(0, 0, view);
glRotatef(rotY2, 0, 1.0f, 0);
glRotatef(rotX, 1.0f, 0.0f, 0);
glLineWidth(2.0f);
character.build();
posfix.x = -pos.x; //fixed value of pos (x axis)
posfix.z = -pos.z; //fixed value od pos (z axis)
glTranslatef(0, -posYheight, 0);
glLineWidth(1.0f);

// world translation and rotation
glRotatef(0, 0, 0, 1.0f);
glRotatef(rotY, 0, 1.0f, 0);
glTranslatef(pos.x, 0, pos.z);

posfix.y = posYheight;

// closing on “X”
if (Display.isCloseRequested()) {
stop();
}
// System.out.println(delta);

Display.update();
Display.sync(60);

}
walls.destroy();
box.destroy();
character.destroy();
for (int i = 0; i < level.boxNumber; i++) {
level.box[i].destroy();
}
Display.destroy();

}

public void stop() {
// stop method
running = false;
}

public void readinput(int delta) {
// reading input(keyboard, mouse) method

// rotation parameter max 360 degree
if (rotY == 360 | rotY == -360)
rotY = 0;

if (rotY2 == 360 | rotY2 == -360)
rotY2 = 0;

if (rotX == 360 | rotX == -360)
rotX = 0;

// System.out.println(“angle ” + rotY + ” z ” + (int) pos.z + ” x ” +
// (int) pos.x);

if (!Keyboard.isKeyDown(Keyboard.KEY_LEFT)
&& !Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
// rotate back(Y axis) when view keys not pressed
rotY2 = 0;
}

if (!Keyboard.isKeyDown(Keyboard.KEY_UP)
&& !Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
// rotate back(X axis) when view keys not pressed
rotX = 0;
}

if (Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key A and W
x = (float) (0.1 * Math.sin(Math.toRadians(45 – rotY)) * delta * 0.1f);
z = (float) (0.1 * Math.cos(Math.toRadians(45 – rotY)) * delta * 0.1f);

pos.x += x;
pos.z += z;

}
System.out.println(x + ” ” + z);
if (!Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& Keyboard.isKeyDown(Keyboard.KEY_D)
&& Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key D and W
x = (float) -(0.1 * Math.cos(Math.toRadians(45 – rotY)) * delta * 0.1f);
z = (float) (0.1 * Math.sin(Math.toRadians(45 – rotY)) * delta * 0.1f);

pos.x += x;
pos.z += z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key A
x = (float) -(0.1 * Math.cos(Math.toRadians(180 – rotY)) * delta * 0.1f);
z = (float) (0.1 * Math.sin(Math.toRadians(180 – rotY)) * delta * 0.1f);

pos.x += x;
pos.z += z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key S

z = (float) -(0.1 * Math.sin(Math.toRadians(90 – rotY)) * delta * 0.1f);
x = (float) (0.1 * Math.cos(Math.toRadians(90 – rotY)) * delta * 0.1f);

pos.z += z;
pos.x += x;

}

if (Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key W

z = (float) (0.1 * Math.sin(Math.toRadians(90 – rotY)) * delta * 0.1f);
x = (float) -(0.1 * Math.cos(Math.toRadians(90 – rotY)) * delta * 0.1f);

pos.x += x;
pos.z += z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_D)
&& !Keyboard.isKeyDown(Keyboard.KEY_A)
&& !Keyboard.isKeyDown(Keyboard.KEY_S)
&& !Keyboard.isKeyDown(Keyboard.KEY_W)
&& !Keyboard.isKeyDown(Keyboard.KEY_E)
&& !Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// moving code for key D

x = (float) (0.1 * Math.cos(Math.toRadians(180 – rotY)) * delta * 0.1f);
z = (float) -(0.1 * Math.sin(Math.toRadians(180 – rotY)) * delta * 0.1f);

pos.x += x;
pos.z += z;

}

if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
// clockwise rotation for key E
rotY += 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
// counter clockwise rotation for key Q

rotY -= 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
// clockwise rotation for key RIGHT (showing character’s side)
rotY2 += 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
// counter clockwise rotation for key LEFT (showing character’s
// side)

rotY2 -= 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
// counter clockwise rotation for key UP
rotX += 3.0f;

}

if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
// counter clockwise rotation for key DOWN (showing character’s
// side)

rotX -= 3.0f;

}
// if (Keyboard.next()){
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
// floating up

posYheight += 0.5f;

// }
}
if (Keyboard.isKeyDown(Keyboard.KEY_Z)) {
// floating up
posYheight -= (0.2);

}

// change view close or third person
if (Keyboard.next()) {
if (Keyboard.isKeyDown(Keyboard.KEY_V)) {
if (view == 8.0f)
view = 0;
else
view = 8.0f;
}
}

updateFPS();
}

public void checkCollisions() {
// check for collisions (wall and box)

// Wall checkers
if ((int) pos.x == 19)
pos.x = 19;

if ((int) pos.x == -19)
pos.x = -19;

if ((int) pos.z == 19)
pos.z = 19;

if ((int) pos.z == -19)
pos.z = -19;

// check collision for cube in middle -> thats very confusing way

if (posYheight >= box.getC().y + 1 && posYheight <= box.getA().y + 3) {
if (pos.x <= (box.getB().x + 1) && pos.x >= box.getA().x – 1) {
if (pos.z >= -box.getE().z && pos.z <= -box.getA().z) {
if ((int) pos.x == box.getB().x) {
pos.x = box.getB().x + 0.5f;
}

if ((int) pos.z == -box.getE().z && pos.x <= box.getB().x
&& pos.x >= box.getA().x) {
pos.z = -box.getE().z;
}

if ((int) pos.x == box.getA().x) {
pos.x = box.getA().x – 0.2f;

}

if ((int) pos.z == -box.getA().z – 1
&& pos.x <= box.getB().x && pos.x >= box.getA().x) {
pos.z = -box.getA().z;
}

}
}
}

//collision checks for level cubes, also collision response
for (int i = 0; i < level.boxNumber; i++) {
if (posYheight >= level.box[i].getC().y + 1
&& posYheight <= level.box[i].getA().y + 3) {
if (posfix.x >= level.box[i].getA().x
& posfix.x <= level.box[i].getB().x) {
if (posfix.z > level.box[i].getA().z
& posfix.z < level.box[i].getE().z) {

if ((int) posYheight >= level.box[i].getC().y) {
// posYheight += 0.25f ;
collide = true;

} else
collide = false;

if ((int) posYheight <= level.box[i].getA().y + 4) {
posYheight += 0.15f;
collide = true;
} else
collide = false;

if (posYheight < level.box[i].getC().y + 0.9f) {
collide = true;
posYheight -= 0.25f;
}
collide = false;

if ((int) posfix.x >= level.box[i].getA().x
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.x -= x;

} else {

if ((int) posfix.x <= level.box[i].getB().x
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.x -= x;
}
}

if ((int) posfix.z >= level.box[i].getA().z
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.z -= z;
} else {

if ((int) posfix.z <= level.box[i].getE().z
&& posYheight <= level.box[i].getA().y + 2
&& posYheight >= level.box[i].getC().y) {
pos.z -= z;
}
}

}
}
} else
collide = false;
}

//don’t fall when touching floor
if (posYheight <= 0 && !Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {

posYheight = 0.0f;

}
//if player not collide posYheight will drop = gravity
if (!collide) {
posYheight -= 0.15f;
}

}

public static void main(String[] args) {
Main m = new Main();
m.init();
m.start();

}

}

class Walls:

package main;

import static org.lwjgl.opengl.GL11.*;

import java.io.IOException;

import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Walls {
// Class for creating simple Box(Cube)

Vector3f a, b, c, d, e, f, g, h;
int displaylist;

Texture texture;
Texture texture2;

public Walls(Vector3f a, Vector3f b, Vector3f c, Vector3f d, Vector3f e,
Vector3f f, Vector3f g, Vector3f h) {
// constructor
this.a = a;
this.b = b;
this.c = c;
this.d = d;

this.e = e;
this.f = f;
this.g = g;
this.h = h;

loadTexture();
}

public void build() {

createlist();
glCallList(displaylist); // display list
}

public void destroy() {
glDeleteLists(displaylist, 1); // removing display list
}

public void createlist() {
// creating display list
displaylist = glGenLists(1);
glNewList(displaylist, GL_COMPILE);
texture.bind(); //bind wall texture
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(10, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(10, 10);
glVertex3d(d.x, d.y, d.z);
glTexCoord2f(0, 10);
glVertex3d(c.x, c.y, c.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(f.x, f.y, f.z);
glTexCoord2f(10, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(10, 10);
glVertex3d(c.x, c.y, c.z);
glTexCoord2f(0, 10);
glVertex3d(g.x, g.y, g.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(10, 0);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(10, 10);
glVertex3d(h.x, h.y, h.z);
glTexCoord2f(0, 10);
glVertex3d(d.x, d.y, d.z);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(b.x, b.y, b.z);
glTexCoord2f(10, 0);
glVertex3d(a.x, a.y, a.z);
glTexCoord2f(10, 10);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(0, 10);
glVertex3d(f.x, f.y, f.z);

glEnd();
texture2.bind(); //bind floor texture
glBegin(GL_QUADS);

glTexCoord2f(0, 0);
glVertex3d(c.x, c.y, c.z);
glTexCoord2f(20, 0);
glVertex3d(d.x, d.y, d.z);
glTexCoord2f(20, 20);
glVertex3d(h.x, h.y, h.z);
glTexCoord2f(0, 20);
glVertex3d(g.x, g.y, g.z);
glEnd();
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3d(e.x, e.y, e.z);
glTexCoord2f(10, 0);
glVertex3d(f.x, f.y, f.z);
glTexCoord2f(10, 10);
glVertex3d(g.x, g.y, g.z);
glTexCoord2f(0, 10);
glVertex3d(h.x, h.y, h.z);
glEnd();

glEndList();
}

 

public void loadTexture() {
//loader for textures (using slick2d helper)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

try {
// load texture from PNG file
texture = TextureLoader.getTexture(“PNG”,
ResourceLoader.getResourceAsStream(“res/wall.png”));

texture2 = TextureLoader.getTexture(“PNG”,
ResourceLoader.getResourceAsStream(“res/floor2.png”));
} catch (IOException e) {
e.printStackTrace();
}

}
}

class Character

package main;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.util.vector.Vector3f;

public class Character {
//Class for creating a Character

Vector3f a,b,c,d, e, f, g, h, i, j, k ;
int displaylistch;

public Character(Vector3f a, Vector3f b, Vector3f c, Vector3f d, Vector3f e, Vector3f f, Vector3f g, Vector3f h, Vector3f i, Vector3f j, Vector3f k) {
//constructor
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
this.h = h;
this.i = i;
this.j = j;
this.k = k;
}

public void build() {
createlist();
glCallList(displaylistch); //display list
}

public void destroy() {
glDeleteLists(displaylistch, 1); //removing display list
}

public void createlist() {
//creating display list
displaylistch = glGenLists(1);
glNewList(displaylistch, GL_COMPILE);

glBegin(GL_QUADS); //head
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(a.x, a.y, a.z);
glVertex3d(b.x, b.y, b.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(c.x, c.y, c.z);
glVertex3d(e.x, e.y, e.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glVertex3d(d.x, d.y, d.z);
glVertex3d(k.x, k.y, k.z);
glEnd();
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3d(k.x, k.y, k.z);
glVertex3d(f.x, f.y, f.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(k.x, k.y, k.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(g.x, g.y, g.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(k.x, k.y, k.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(h.x, h.y, h.z);
glEnd();
glColor3f(1.0f, 0.6f, 0.3f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(f.x, f.y, f.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(i.x, i.y, i.z);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex3d(f.x, f.y, f.z);
glColor3f(1.0f, 0.6f, 0.3f);
glVertex3d(j.x, j.y, j.z);
glEnd();

glEndList();
}

public void add(Vector3f a, Vector3f b, Vector3f c, Vector3f d) {

//method for changing Objects vertices coordinate (x,y,z)

this.a.x += a.x;
this.a.y = a.y;
this.a.z += a.z;

this.b.x += b.x;
this.b.y = b.y;
this.b.z += b.z;

this.c.x += c.x;
this.c.y = c.y;
this.c.z += c.z;

this.d.x += d.x;
this.d.y = d.y;
this.d.z += d.z;

}

}

Class LevelGenerator

package main;

import java.util.Random;

import org.lwjgl.util.vector.Vector3f;

public class LevelGenerator {
// class that generate Box objects

public int boxNumber;
public Box[] box;
String textureloc = “res/lvl.png”;
Random random = new Random();
Random random2 = new Random();

public LevelGenerator(int boxNumber) {

this.boxNumber = boxNumber;
box = new Box[boxNumber];

for (int i = 0; i < boxNumber; i++) {
// loop to create random coordinates
int x = random.nextInt(20); // random value for Vector
int z = random.nextInt(20);
int k = random2.nextInt(2); // values to also set random – values
int l = random2.nextInt(2);
if (k == 0)
x = -x;

if (l == 0)
z = -z;

// System.out.println(x);
box[i] = new Box(new Vector3f(x – 2, i, z – 2), new Vector3f(x, i,
z – 2), new Vector3f(x, i – 2, z – 2), new Vector3f(x – 2,
i – 2, z – 2), new Vector3f(x – 2, i, z), new Vector3f(x,
i, z), new Vector3f(x, i – 2, z), new Vector3f(x – 2,
i – 2, z), textureloc);
}
}

public void generate() {
// build cubes
for (int i = 0; i < boxNumber; i++) {

box[i].build();
}
}

}

About these ads
About

my interests: Game development (atm programming in java) 3D Graphics (Favourite program: Blender)

Tagged with: , , , , , , , , , , , ,
Posted in Java Game Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: