LWJGL: Collision Detection
Hello! I'm learning java and I'm trying to do a collision detection of camera class and walls of room class. But I have completely no clue how to do that. Please Help me! Heres my code
Game Display Class
Code:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class GameDisplay {
public float speed = 0.01F;
public GameDisplay(){
try{
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle("My 3D Game");
Display.setFullscreen(true);
Display.create();
} catch (LWJGLException e){
e.printStackTrace();
}
//Initialization code OpenGL
Mouse.setGrabbed(true);
camera cam = new camera(70, (float)Display.getWidth()/(float)Display.getHeight(), 0.3f, 1000);
room s = new room();
while(!Display.isCloseRequested()){
//Player Controls
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
System.exit(0);
if(Keyboard.isKeyDown(Keyboard.KEY_W))
cam.moveZ(speed);
if(Keyboard.isKeyDown(Keyboard.KEY_S))
cam.moveZ(-speed);
if(Keyboard.isKeyDown(Keyboard.KEY_A))
cam.moveX(speed);
if(Keyboard.isKeyDown(Keyboard.KEY_D))
cam.moveX(-speed);
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
cam.jumping = true;
cam.rotateYX(Mouse.getDX(), Mouse.getDY());
//Render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
cam.useView();
s.drawCube(0,0,0, 1,1,10);
cam.physics();
Display.update();
}
Display.destroy();
}
public static void main(String[] args) {
new GameDisplay();
}
}
Camera Class
Code:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
public class camera {
private float x;
private float y;
public float z;
private float rx;
private float ry;
private float rz;
private float fov;
private float aspect;
private float near;
private float far;
public boolean jumping = false;
private boolean faling = false;
public camera(float fov, float aspect, float near, float far){
x = 0;
y = 0;
x = 0;
rx = 0;
ry = 0;
rz = 0;
this.fov = fov;
this.aspect = aspect;
this.near = near;
this.far = far;
initProjection();
}
private void initProjection(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
}
public void useView(){
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
glTranslatef(x, y, z);
}
// Initiate Get Set for floaters
public float getX(){
return x;
}
public float getY(){
return y;
}
public float getZ(){
return z;
}
public void setX(float x){
this.x = x;
}
public void setY(float y){
this.y = y;
}
public void setZ(float z){
this.z = z;
}
public float getRX(){
return rx;
}
public float getRY(){
return ry;
}
public float getRZ(){
return rz;
}
public void setRX(float rx){
this.rx = rx;
}
public void setRY(float ry){
this.ry = ry;
}
public void setRZ(float rz){
this.rz = rz;
}
//Movement of player
public void moveZ(float amt){
z += amt * Math.sin(Math.toRadians(ry + 90));
x += amt * Math.cos(Math.toRadians(ry + 90));
}
public void moveX(float amt){
z += amt * Math.sin(Math.toRadians(ry));
x += amt * Math.cos(Math.toRadians(ry));
}
public void rotateYX(float gy, float gx){
ry += gy;
rx -= gx;
}
public void physics(){
if(jumping == true){
if(y > -5 && faling == false){
y -= 0.01;
}else{
faling = true;
}
if(faling == true && y < 0){
y += 0.01;
}else if(y > 0){
y = 0;
jumping = false;
faling = false;
}
}
}
}
Room Class
Code:
import static org.lwjgl.opengl.GL11.*;
public class room{
public float z;
public void drawCube(float cubeX, float cubeY, float cubeZ, float cubeW, float cubeH, float cubeL) {
glColor3f(1.0f, 0.5f, 0f);
glTranslatef(cubeX, cubeY, cubeZ);
glPushMatrix();
{
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glBegin(GL_QUADS);
glColor3f(1f, 0f, 0f);
glVertex3f(-cubeW,-cubeH,cubeL);
glVertex3f(-cubeW,cubeH,cubeL);
glVertex3f(cubeW,cubeH,cubeL);
glVertex3f(cubeW,-cubeH,cubeL);
glEnd();
glCullFace(GL_FRONT);
glBegin(GL_QUADS);
glColor3f(0f, 1f, 0f);
glVertex3f(-cubeW,-cubeH,-cubeL);
glVertex3f(-cubeW,cubeH,-cubeL);
glVertex3f(cubeW,cubeH,-cubeL);
glVertex3f(cubeW,-cubeH,-cubeL);
glEnd();
glBegin(GL_QUADS);
glColor3f(0f, 0f, 1f);
glVertex3f(-cubeW,-cubeH,-cubeL);
glVertex3f(-cubeW,-cubeH,cubeL);
glVertex3f(-cubeW,cubeH,cubeL);
glVertex3f(-cubeW,cubeH,-cubeL);
glEnd();
glBegin(GL_QUADS);
glColor3f(1f, 0f, 1f);
glVertex3f(-cubeW,-cubeH,-cubeL);
glVertex3f(cubeW,-cubeH,-cubeL);
glVertex3f(cubeW,-cubeH,cubeL);
glVertex3f(-cubeW,-cubeH,cubeL);
glEnd();
glCullFace(GL_BACK);
glBegin(GL_QUADS);
glColor3f(1f, 1f, 0f);
glVertex3f(cubeW,-cubeH,-cubeL);
glVertex3f(cubeW,-cubeH,cubeL);
glVertex3f(cubeW,cubeH,cubeL);
glVertex3f(cubeW,cubeH,-cubeL);
glEnd();
glBegin(GL_QUADS);
glColor3f(0f, 1f, 1f);
glVertex3f(-cubeW,cubeH,-cubeL);
glVertex3f(cubeW,cubeH,-cubeL);
glVertex3f(cubeW,cubeH,cubeL);
glVertex3f(-cubeW,cubeH,cubeL);
glEnd();
glBegin(GL_QUADS);
glColor3f(0f, 1f, 1f);
glVertex3f(-cubeW,cubeH,-cubeL);
glVertex3f(cubeW,cubeH,-cubeL);
glVertex3f(cubeW,cubeH,cubeL);
glVertex3f(-cubeW,cubeH,cubeL);
glEnd();
glDisable(GL_CULL_FACE);
}
glPopMatrix();
}
public float getZ(){
return z;
}
public void setZ(float z){
this.z = z;
}
}
Re: LWJGL: Collision Detection
Well... that is not an easy or short task. You need to look if any vertex of a body is inside the geometry of another body... pure Math.
Let me point you to a engine which implements all this on a professional level - I used it some time ago: jMonkeyEngine 3.0 | Java OpenGL Game Engine
Of course if you need it to do yourself - there is no way around... :)
Re: LWJGL: Collision Detection
Re: LWJGL: Collision Detection
Will I still be able to use code above?
Re: LWJGL: Collision Detection
No, it is a different design - you can use parts of it but usually you would not need it.