Results 1 to 5 of 5
  1. #1
    Fed0r is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default 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
    Java 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
    Java 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
    Java 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;
    	}
    	
    }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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... :)
    I like likes!

  3. #3
    Fed0r is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: LWJGL: Collision Detection

    Thanks! I will try it.

  4. #4
    Fed0r is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: LWJGL: Collision Detection

    Will I still be able to use code above?

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: LWJGL: Collision Detection

    No, it is a different design - you can use parts of it but usually you would not need it.
    I like likes!

Similar Threads

  1. LWJGL Collision Detection
    By Furyright in forum Java 2D
    Replies: 1
    Last Post: 10-09-2011, 07:48 AM
  2. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  3. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  4. LWJGL: Camera + Block Collision
    By Aaron in forum New To Java
    Replies: 6
    Last Post: 11-14-2010, 10:53 PM
  5. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •