Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Furyright

Thread: LWJGL Collision Detection

  1. #1
    Furyright is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default LWJGL Collision Detection

    Plain and simple - I have NO idea what I have done wrong. I am checking if the rectangle p intersects rectangle e with an outside class. I am believe the problem is matching up the rectangle to the drawn quad. It must be a problem with the way I declared the rectangles or something with my collision class. Anyway, here's the code:

    Main class:
    Java Code:
    package epicquest;
    
    import org.lwjgl.util.Rectangle;
    import org.lwjgl.LWJGLException;
    import org.lwjgl.opengl.*;
    
    import engine.Collision;
    
    public class RunGame {
    	int fx = 100, sx = 200;
    	int fy = 100, sy = 200;
    	int efx = 400, esx = 200;
    	int efy = 100, esy = 200;
    	Rectangle p = new Rectangle(fx, fy, sx, sy);
    	Rectangle e = new Rectangle(efx, efy, esx, esy);
    	public RunGame() {
    		createScreen();
    		drawQuad();
    	}
    	
    	public void createScreen() {
    		try {
    			Display.setDisplayMode(new DisplayMode(600, 400));
    			Display.create();
    		} catch(LWJGLException e) {
    			e.printStackTrace();
    			System.exit(0);
    		}
    		
    		while(!Display.isCloseRequested()) {
    			drawQuad();
    			fx = KeyboardInput.checkLeft(fx);
    			fx = KeyboardInput.checkRight(fx);
    			fy = KeyboardInput.checkForward(fy);
    			fy = KeyboardInput.checkBackward(fy);
    			if(Collision.quadIntersect(p, e)) {
    				System.out.println("True!!");
    			}
    			System.out.println("FX: " + fx);
    			System.out.println("FY: " + fy);
    			System.out.println("SX: " + sx);
    			System.out.println("SY: " + sy);
    			System.out.println("EFX: " + efx);
    			System.out.println("EFY: " + efy);
    			System.out.println("ESX: " + esx);
    			System.out.println("ESY: " + esy);
    			Display.update();
    		}
    	}
    	
    	public void drawQuad() {
    		GL11.glMatrixMode(GL11.GL_PROJECTION);
    		GL11.glLoadIdentity();
    		GL11.glOrtho(0, 800, 600, 0, 1, -1);
    		GL11.glMatrixMode(GL11.GL_MODELVIEW);
    		
    		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    		GL11.glColor3f(0.5f, 0.5f, 1.0f);
    	    GL11.glBegin(GL11.GL_QUADS);
            	GL11.glVertex2f(fx, fy);
            	GL11.glVertex2f(fx + sx, fy);
            	GL11.glVertex2f(fx + sx, fy + sy);
            	GL11.glVertex2f(fx, fy + sy);
            GL11.glEnd();
    	    GL11.glBegin(GL11.GL_QUADS);
        		GL11.glVertex2f(efx, efy);
        		GL11.glVertex2f(efx + esx, efy);
        		GL11.glVertex2f(efx + esx, efy + esy);
        		GL11.glVertex2f(efx, efy + esy);
        	GL11.glEnd();
    	}
    	
    	public static void main(String[] args) {
    		new RunGame();
    	}
    }
    Keyboard Input:
    Java Code:
    package epicquest;
    
    import org.lwjgl.input.Keyboard;
    
    public class KeyboardInput {
    	public KeyboardInput() {
    		
    	}
    	
    	public static int checkForward(int y) {
    		if(y >= 0) {
    			if(Keyboard.isKeyDown(Keyboard.KEY_W)) {
    				y -= 1;
    			}
    		}
    		
    			return y;
    	}
    	
    	public static int checkBackward(int y) {
    		if(y <= 400) {
    			if(Keyboard.isKeyDown(Keyboard.KEY_S)) {
    				y += 1;
    			}
    		}
    		
    		return y;
    	}
    	
    	public static int checkLeft(int x) {
    		if(x >= 0) {
    			if(Keyboard.isKeyDown(Keyboard.KEY_A)) {
    				x -= 1;
    			}
    		}
    		
    		return x;
    	}
    	
    	public static int checkRight(int x) {
    		if(x <= 600) {
    			if(Keyboard.isKeyDown(Keyboard.KEY_D)) {
    				x += 1;
    			}
    		}
    			
    		return x;
    	}
    }
    Collision Class:
    Java Code:
    package engine;
    
    import org.lwjgl.util.Rectangle;
    
    public class Collision {
    	static boolean isIntersecting = false;
    	public static boolean quadIntersect(Rectangle i, Rectangle j) {
    		if(i.intersects(j)) {
    			isIntersecting = true;
    		}
    		return isIntersecting;
    	}
    }

  2. #2
    Furyright is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: LWJGL Collision Detection

    Alright, I figured out what I was doing wrong. After creating the rectangles, I was never updating them. I fixed it by adding these two lines between lines 35 and 36 of the main class:
    Java Code:
    p.setLocation(fx, fy);
    e.setLocation(sx, sy);
    m93a likes this.

Similar Threads

  1. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  2. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  3. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 01:38 AM
  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

Posting Permissions

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