3D TPP LWJGL Game #2

javaw 2013-01-03 15-32-27-48

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

Finally third person view ! , You can move (WASD keys) with this cube that imitate character and also rotate (Q and e keys). Using LEFT and RIGHT You can rotate around character. And more, there is option to enable close view from top of cube. Yeah, it looks pretty damm primitive, but for me great step 😀 Can’t wait until I will start adding graphic, textures, light etc hehe.

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;
import static org.lwjgl.util.glu.GLU.gluLookAt;

public class Main {

private boolean running = false;
private Box box; // simple wall for tests
private Box box2; // cube = character(for tests)
private Box box3; // small cube in middle

private float fov = 60; // field of view
private Vector3f pos = new Vector3f(0, 0, 0); // position vector
private float rotY = 0; // rotation along Y axis
private float rotY2 = 0; // rotation along Y axis
private float view = 0; //for character view transformations (along z 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.3f, 50.0f);
glViewport(0, 0, 800, 600);
gluLookAt(0.0f, 1.5f, 10.0f, 0.0f, 0, 0, 0.0f, 1.0f, 0.0f); //shows character from behind
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 = 0.8f;
// Box2 vertices

// creating cube = character atm
box2 = new Box(new Vector3f(-l2, l2, -0.5f),
new Vector3f(l2, l2, -0.5f), new Vector3f(l2, -3, -0.5f),
new Vector3f(-l2, -3, -0.5f), new Vector3f(-l2, l2, 0.5f),
new Vector3f(l2, l2, 0.5f), new Vector3f(l2, -3, 0.5f),
new Vector3f(-l2, -3, 0.5f));

//small cube in middle
box3 = 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

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

glTranslatef(0, 0, view);
glRotatef(rotY2, 0, 1.0f, 0);
box2.build();

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();
box3.destroy();
Display.destroy();

}

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

public void readinput() {
// 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 (Keyboard.isKeyDown(Keyboard.KEY_A)) {
// moving code for key A
float x = (float) (0.1 * Math.cos(Math.toRadians(180 – rotY)));
float z = (float) (0.1 * Math.sin(Math.toRadians(180 – rotY)));
pos.x -= x;
pos.z += z;

}

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

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

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

}

if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
// moving code for key W

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

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

}

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

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

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;

}

//change view close or third person
if (Keyboard.next()){
if (Keyboard.isKeyDown(Keyboard.KEY_V)) {
// gluLookAt(0.0f, 1.0f, 0.0f, 0.0f, 0.2f, 0, 0.0f, 1.0f, 0.0f);
if (view == 9.5f)
view = 0;
else view = 9.5f;
}
}

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: