Results 1 to 5 of 5
  1. #1
    scotthdez is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default "Java Collision Detection With Rectangles" Help!!!!

    Hi im new to java and i have been making a ping pong game, to start off java, and i have run into a problem that when i run it the collision detection for rectangles doesnt always work only at certain points of it being run. Please tell me what i have done wrong with the collision detection so i can move on with java.(p.s the collision method is in the Ball.java class)

    Main.java

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JFrame;
    
    public class Main extends JFrame implements Runnable {
    	private static final long serialVersionUID = 1L;
    	
    	public static String title = "Ping Pong v1.0";
    	public static Dimension size = new Dimension(600, 500);
    	
    	public static boolean running = false;
    	public static boolean isOp = false;
    	
    	Ball ball = new Ball(startY, startY);
    	Paddle p = new Paddle();
    	MouseHandler mh = new MouseHandler();	
    	
    	Thread thread;
    	
    	private Image dbImage;
    	private Graphics dbg;
    		
    	public static int startY = size.width / 2, startX = size.height / 2;
    			
    	public Main() {
    		thread = new Thread(this, "Display");
    		setPreferredSize(size);
    		setMaximumSize(size);
    		setMinimumSize(size);
    
    		setTitle(title);
    		pack();
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setFocusable(true);		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.BLACK);
    		setVisible(true);
    		addKeyListener(new AL());
    		addMouseListener(new MouseHandler());
    		addMouseMotionListener(new MouseHandler());
    		
    		start();
    		
    	}
    	
    	public synchronized void start() {
    		running = true;
    		thread.start();
    		
    	}
    	
    	public synchronized void stop() {
    		running = false;
    		try {
    			thread.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	@Override
    	public void run() {
    		try {
    			while(running) {
    				tick();				
    				Thread.sleep(9);
    			}			
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public void tick() {
    		ball.tick();
    		p.tick();
    	}
    	
    	public void paint(Graphics g) {
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		render(dbg);
    		g.drawImage(dbImage, 0, 0, this);
    	}
    
    	public void render(Graphics g) {
    		p.render(g);
    		ball.render(g);
    		
    		repaint();
    	}
    	
    	public static void main(String[] args) {
    		Main main = new Main();
    		
    	}
    	
    	public class AL extends KeyAdapter {
    		@Override
    		public void keyPressed(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_W) {
    				p.setP1YDirection(-1);			
    			} else if (e.getKeyCode() == KeyEvent.VK_S) {
    				p.setP1YDirection(+1);
    			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
    				p.setP2YDirection(-1);
    			} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    				p.setP2YDirection(+1);
    			}
    			
    		}
    		@Override
    		public void keyReleased(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_W) {
    				p.setP1YDirection(0);
    			} else if (e.getKeyCode() == KeyEvent.VK_S) {
    				p.setP1YDirection(0);
    			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
    				p.setP2YDirection(0);
    			} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    				p.setP2YDirection(0);
    			}
    			
    		}
    		
    	}
    	public class MouseHandler extends MouseAdapter {
    				
    		@Override
    		public void mouseMoved(MouseEvent e) {
    			int mx = e.getX();
    			int my = e.getY();
    			
    			if (Main.isOp) {
    				System.out.println("(X: " + mx + ", Y: " + my + ")");
    			}
    		}
    		
    		@Override
    		public void mousePressed(MouseEvent e) {
    			int mx = e.getX();
    			int my = e.getY();
    			
    			
    		}
    		
    	}
    	
    }
    Ball.java

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.util.Random;
    
    public class Ball {
    		
    	Rectangle Ball;
    	
    	int x, y;
    	int XDirection, YDirection;
    	
    	Paddle p = new Paddle();
    	
    	public Ball(int x, int y) {
    		this.x = x;
    		this.x = x;
    		
    		Ball = new Rectangle(x, y, 5, 5);
    		Random r = new Random();
    		int rDir = r.nextInt(1);
    		if (rDir == 0) {
    			rDir--;
    			setXDirection(rDir);
    		}
    		int yDir = r.nextInt(1);
    		if (yDir == 0) {
    			yDir++;
    			setYDirection(yDir);
    		}
    		
    	}
    	
    	public void tick() {
    		physics();
    	}
    	
    	public void setXDirection(int xdir) {
    		XDirection = xdir;
    	}
    	
    	public void setYDirection(int ydir) {
    		YDirection = ydir;
    	}
    		
    	public void physics() {
    		collision();
    		Ball.x += XDirection;
    		Ball.y += YDirection;
    		
    		if (Ball.x <= 0) {
    			setXDirection(+1);
    		}
    		if (Ball.x >= 592) {
    			setXDirection(-1);
    		}
    		if (Ball.y >= 492) {
    			setYDirection(-1);
    		}
    		if (Ball.y <= 25) {
    			setYDirection(+1);
    		}
    	}
    
    	public void collision() {
    		if (Ball.intersects(p.p1)) {
    			setXDirection(+1);
    			System.out.println("fd");
    		}
    		if (Ball.intersects(p.p2)) {
    			setXDirection(-1);
    			System.out.println("df");
    		}
    	}
    	
    	public void render(Graphics g) {
    		g.setColor(Color.WHITE);
    		g.fillRect(Ball.x, Ball.y, Ball.width, Ball.height);
    		
    	}
    		
    }
    Paddle.java

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    
    public class Paddle {
    	
    	Rectangle p1 = new Rectangle(40, Main.size.width / 2 - 70, 20, 54);
    	Rectangle p2 = new Rectangle(539, Main.size.width / 2 - 70, 20, 54);
    	
    	int P1YDirection;
    	int P2YDirection;
    		
    	public Paddle() {
    		
    	}
    	
    	public void tick() {
    		move();
    	}
    	
    	public void move() {
    		p1.y += P1YDirection;
    		p2.y += P2YDirection;
    		if (p1.y >= 442) {
    			p1.y = 442;
    		} else if (p1.y <= 25) {
    			p1.y = 25;
    		}
    		if (p2.y >= 442) {
    			p2.y = 442;
    		} else if (p2.y <= 25) {
    			p2.y = 25;
    		}
    		
    	}
    	
    	public void setP1YDirection(int ydir) {
    		P1YDirection = ydir;
    	}
    	
    	public void setP2YDirection(int ydir) {
    		P2YDirection = ydir;
    	}
    	
    	public void render(Graphics g) {
    		g.setColor(Color.BLUE);
    		g.fillRect(p1.x, p1.y, p1.width, p1.height);
    		g.fillRect(p2.x, p2.y, p2.width, p2.height);
    	}
    	
    }
    Last edited by scotthdez; 07-24-2013 at 03:38 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: "Java Collision Detection With Rectangles" Help!!!!

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: "Java Collision Detection With Rectangles" Help!!!!

    Debugging 101: sprinkle your code with System.out.println(...) statements or otherwise display the value of variables. Then try to figure out which value isn't what you had expected it to be.

    Could be a concurrency issue.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    scotthdez is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: "Java Collision Detection With Rectangles" Help!!!!

    i have and the collision() method seems to be in the game loop but the detection for collision seems to not want to work how its supposed to.

  5. #5
    bakriawad2000 is offline Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Default Re: "Java Collision Detection With Rectangles" Help!!!!

    Your declration is wrong,

    you have to do it this way:

    Java Code:
    Rectangle r1, r2, r3;
    BRec = new Rectangle();  //rectangle for ball
    P1Rec = new Rectangle();//rectangle for pad 1
    P2Rec = new Rectangle();//rectangle for pad2
    
    //everty tick do:
    BRec.setBounds((ballX, ballY, ballWidth, ballHeight);
    P1Rec.setBounds(pad1X, pad1Y, pad1Width, pad1Height);
    P2Rec.setBounds(ballX, pad2Y, pad2Width, pad2Height);
    
    /* Declration: I have never used rectangle.setBounds(); so it might 
        be in the following syntax:
           rectangle.setBounds(x,y,x+width,y+height);
        creating a new rectangle is done like this:
           Rectangle rectangle = new Rectangle(x,y,width,height);
    
    */
    if(r1.intersect(r2){};
    if(r1.intersect(r3){};
    if you can also do this if u want to make the rectangle at another class and call it to where the collision will be tested:

    Rec.java
    Java Code:
    public Rectangle getRec(){return new Rectangle(x,y,width,height);}
    Call.java
    Java Code:
    Rec rec = new Rec();
    Rectangle r = new rectangle();
    
    r = rec.getRec();
    Last edited by bakriawad2000; 08-10-2013 at 05:31 PM.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  3. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  4. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

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
  •