2 Replies - 358 Views - Last Post: 23 January 2013 - 07:25 PM Rate Topic: -----

#1 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Collision detection not working!

Posted 22 January 2013 - 11:14 PM

For some reason my collision detection isn't working: The ball just clips through the paddle. Here is my code, the important methods are checkForCollisions in the ball class, and getBounds in the Paddle class.
Pong class:
import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Pong {
	private Ball ball;
	public Paddle user;
	public Paddle comp;
	public static Pong pong;
	private Pong(){
		try {
			Display.setDisplayMode(new DisplayMode(640,480));
			Display.create();
		} catch (LWJGLException e) {
			Display.destroy();
			e.printStackTrace();
			System.exit(1);
		}
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,640,480,0,-1,1);
        glMatrixMode(GL_MODELVIEW);
        user=new Paddle(0,280,0);
        comp=new Paddle(640,280,1);
	}
	private void startGameLoop() {
		while(!Display.isCloseRequested()){
			glClear(GL_COLOR_BUFFER_BIT);
			user.draw();
			comp.draw();
			updateBall();
			pollInput();
			doAI();
			Display.update();
			Display.sync(60);
		}
		Display.destroy();
	}
	private void doAI() {
		// TODO Auto-generated method stub
		
	}
	private void pollInput() {
		// TODO Auto-generated method stub
		
	}
	private void updateBall() {
		if(ball==null){
			ball=new Ball(Display.getDisplayMode().getWidth()/2,Display.getDisplayMode().getHeight()/2);
		}
		ball.updateBall();
		ball.draw();
	}
	public static void main(String[] args) {
		pong=new Pong();
		pong.startGameLoop();
	}

}


Ball class:
import java.awt.Rectangle;
import java.util.Random;

import org.lwjgl.opengl.GL11;


public class Ball {
	private int x;
	private int y;
	private int xMotion;
	private int yMotion;
	private Random random=new Random();
	private static  final int RADIUS=5;
	public Ball(int x, int y){
		this.x=x;
		this.y=y;
		xMotion=-2;
		yMotion=(random.nextInt(5)-2)/2;
		yMotion=0;
	}
	public void draw(){
		GL11.glPushMatrix();
		GL11.glTranslatef(x, y, 0);
		GL11.glScalef(RADIUS, RADIUS, 1);

		GL11.glBegin(GL11.GL_TRIANGLE_FAN);
		GL11.glVertex2f(0, 0);
		for(int i = 0; i <= 5000; i++){
		    double angle = Math.PI * 2 * i / 5000;
		    GL11.glVertex2f((float)Math.cos(angle), (float)Math.sin(angle));
		}
		GL11.glEnd();

		GL11.glPopMatrix();
	}
	public void updateBall(){
		x+=xMotion;
		y+=yMotion;
		checkForCollisions();
	}
	private void checkForCollisions() {
		Rectangle ballBounds=new Rectangle(x,y,RADIUS,RADIUS);
		if(ballBounds.intersects(Pong.pong.user.getBounds())||ballBounds.intersects(Pong.pong.comp.getBounds()){
			xMotion=-xMotion;
			yMotion=(random.nextInt(5)-2)/2;
		}
	}
}


Paddle class:
import static org.lwjgl.opengl.GL11.*;

import java.awt.Rectangle;
public class Paddle {
	private int x;
	private int y;
	private int width=20;
	private int height=100;
	private int mode;
	public Paddle(int x, int y, int mode){
		this.x=x;
		this.y=y;
		this.mode=mode;
	}
	public void draw(){
		glBegin(GL_QUADS);
		if(mode==0){
			glVertex2i(x,y);
			glVertex2i(x,y-height);
			glVertex2i(x+width,y-height);
			glVertex2i(x+width,y);
		}else{
			glVertex2i(x,y);
			glVertex2i(x,y-height);
			glVertex2i(x-width,y-height);
			glVertex2i(x-width,y);
		}
		glEnd();
	}
	public Rectangle getBounds(){
		if(mode==0){
			return new Rectangle(x,y,width,height);
		}else{
			return new Rectangle(x-width,y,width,height);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Collision detection not working!

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Collision detection not working!

Posted 23 January 2013 - 09:59 AM

You need to use your breakpoints to work out what's happening.
Was This Post Helpful? 0
  • +
  • -

#3 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Collision detection not working!

Posted 23 January 2013 - 07:25 PM

View PostButchDean, on 23 January 2013 - 04:59 PM, said:

You need to use your breakpoints to work out what's happening.

I have already solved this problem by myself, just forgot to make an operation on the x variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1