Results 1 to 2 of 2
Thread: LWJGL Collision Detection
- 10-09-2011, 03:53 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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:
Keyboard Input: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(); } }
Collision Class: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; } }
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; } }
- 10-09-2011, 06:48 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Collision Detection
By sunde887 in forum Java 2DReplies: 2Last Post: 10-07-2011, 11:40 PM -
Collision Detection?
By Alerhau in forum New To JavaReplies: 39Last Post: 09-07-2011, 04:55 PM -
Really Need help with some collision detection
By Harwad in forum New To JavaReplies: 1Last Post: 01-23-2011, 12:38 AM -
LWJGL: Camera + Block Collision
By Aaron in forum New To JavaReplies: 6Last Post: 11-14-2010, 09:53 PM -
Collision Detection
By dotabyss in forum Java GamingReplies: 0Last Post: 03-14-2010, 06:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks