New 3D TPP LWJGL Game

Image

Hello, I am starting new LWJGL(Lightweight java game library) game project in third person perspective (TPP).

I would like to present first step in creating it. So yeah I made so far simple walls, cube in center of coordinate system, and there is FPP camera view(I made it for testing), You can move and rotate (move with WASD and Q,E – rotate)

Here You can see how it looks:

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 org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector3f;

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

public class Main {

private boolean running = false;
private Box box; //simple wall for tests
private Box box2; //small cube in middle
private float fov = 70; // field of view
private Vector3f pos = new Vector3f(0, 0, 0); //position vector
private float rotY = 0; //rotation along Y axis

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.5f, 50.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

running = true;

float l = 20.0f; // need for Box

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

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

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

float l2 = 1.0f;
// Box2 vertices

//creating small cube
box2 = new Box(new Vector3f(-l2, l2, -l2), new Vector3f(l2, l2, -l2),
new Vector3f(l2, -l2, -l2), new Vector3f(-l2, -l2, -l2),
new Vector3f(-l2, l2, l2), new Vector3f(l2, l2, l2),
new Vector3f(l2, -l2, l2), new Vector3f(-l2, -l2, l2));
}

public void start() {
// start method

while (running) {
// game loop

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clearing screen buffer
glEnable(GL_DEPTH_TEST);

glColor3f(1, 0.8f, 0.8f);

box.build(); //make walls
box2.build(); //make cube

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();

glBegin(GL_QUADS); // world floor
glColor3f(0.4f, 1.1f, 0.1f);
glVertex3d(-400, -3.1f, 400);
glVertex3d(-400, -3.1f, -400);
glVertex3d(400, -3.1f, -400);
glVertex3d(400, -3.1f, 400);
glEnd();

glLoadIdentity();

glRotatef(0, 0, 0, 1.0f);
glRotatef(rotY, 0, 1.0f, 0);
glRotatef(1.0f, 0, 0, 0);
glTranslatef(pos.x, 0, pos.z);

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

readinput(); // read keys
Display.update();
Display.sync(60);
}
box.destroy();
box2.destroy();
Display.destroy();

}

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

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

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

if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
//moving code for key A
pos.x -= 0.1 * Math.cos(Math.toRadians(180 – rotY));
pos.z += 0.1 * Math.sin(Math.toRadians(180 – rotY));
}

if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
//moving code for key S

pos.z -= 0.1 * Math.sin(Math.toRadians(90 – rotY));
pos.x += 0.1 * Math.cos(Math.toRadians(90 – rotY));
}
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
//moving code for key W

pos.z += 0.1 * Math.sin(Math.toRadians(90 – rotY));
pos.x -= 0.1 * Math.cos(Math.toRadians(90 – rotY));

}

if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
//moving code for key D

pos.x += 0.1 * Math.cos(Math.toRadians(180 – rotY));
pos.z -= 0.1 * Math.sin(Math.toRadians(180 – rotY));

}

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;

}

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

}

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

}

}

class Box:

package main;

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

import org.lwjgl.util.vector.Vector3f;

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

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

public Box(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;
}

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);

glBegin(GL_QUADS);
glColor3f(0.4f, 0.1f, 0.1f);
glVertex3d(a.x, a.y, a.z);
glVertex3d(b.x, b.y, b.z);
glVertex3d(c.x, c.y, c.z);
glVertex3d(d.x, d.y, d.z);
glEnd();

glBegin(GL_QUADS);
glColor3f(1, 0.8f, 0.8f);

glVertex3d(b.x, b.y, b.z);
glVertex3d(f.x, f.y, f.z);
glVertex3d(g.x, g.y, g.z);
glVertex3d(c.x, c.y, c.z);
glEnd();

glBegin(GL_QUADS);
glColor3f(1, 0.1f, 0.1f);

glVertex3d(a.x, a.y, a.z);
glVertex3d(e.x, e.y, e.z);
glVertex3d(h.x, h.y, h.z);
glVertex3d(d.x, d.y, d.z);
glEnd();

glBegin(GL_QUADS);
glColor3f(0.6f, 0.3f, 0.3f);

glVertex3d(b.x, b.y, b.z);
glVertex3d(a.x, a.y, a.z);
glVertex3d(e.x, e.y, e.z);
glVertex3d(f.x, f.y, f.z);

glEnd();
glBegin(GL_QUADS);

glColor3f(0.2f, 0.8f, 0.8f);

glVertex3d(c.x, c.y, c.z);
glVertex3d(g.x, g.y, g.z);
glVertex3d(h.x, h.y, h.z);
glVertex3d(d.x, d.y, d.z);
glEnd();

glBegin(GL_QUADS);
glColor3f(0.4f, 0.4f, 0.4f);

glVertex3d(e.x, e.y, e.z);
glVertex3d(f.x, f.y, f.z);
glVertex3d(g.x, g.y, g.z);
glVertex3d(h.x, h.y, h.z);
glEnd();

glEndList();
}

public void move(Vector3f a, Vector3f b, Vector3f c, Vector3f d,
Vector3f e, Vector3f f, Vector3f g, Vector3f h) {

//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;

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

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

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

this.h.x += h.x;
this.h.y += h.y;
this.h.z += h.z;
}

}

Advertisements
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

morrky

morrky

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

Personal Links

Verified Services

View Full Profile →

Ad time
prywatne pożyczki pod zastaw nieruchomości bez BIK BIG i KRD
%d bloggers like this: