-1

I am trying to make a copy of the breakout game, but I have problems with checking if two objects (the ball and paddle) intersects.

I have this method for collision detection for now:

public static void handleCollisions() {

    System.out.println(ball.getBounds());        // just to check that they                  
    System.out.println(paddle.getBounds());      //are there and moving correct

    if (ball.getBounds().intersects(paddle.getBounds())) {
        System.out.println("collision");
    }
}

But even though the rectangles for sure intersect at some points, the method never detects anything. I suppose I have done something wrong with some of the classes. Here is the main classes:

package assignment1;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;
import java.util.Random; 



public class Breakout extends TimerTask implements KeyListener {            

public static Vector bricks;
public static Ball ball;
public static Paddle paddle;
public static PaintingPanel panel;
public static boolean gameRunning;
public static Breakout game;

public Breakout()
{

}


public static void setupGame()
{
    Paddle paddle = new Paddle(350, 790, 100, 10);  //initial paddle creation                           
    Ball ball = new Ball(393, 790, 7);

    showGUI();
}

public static void beginGame()
{
    gameRunning = true;  

}

public void run()
{
    if(gameRunning == true)
    {


        Ball.move();
        Paddle.move();
        handleCollisions();




        if(Ball.x < 0 || Ball.x > 800 || Ball.y < 0 || Ball.y > 790){
            gameRunning = false;
            System.out.println("Game over");    
        }


    }


}

public static void stopGame()
{   

}



public static void handleCollisions()
{

    System.out.println(ball.getBounds());
    System.out.println(paddle.getBounds());

    if (ball.getBounds().intersects(paddle.getBounds()))
        {
    System.out.println("Yay");
        }

}


public static void main(String[] args)
{   
    Timer t = new Timer();  
    t.schedule(new Breakout(), 0, 40);

    panel = new PaintingPanel(PaintingPanel.contents);
    setupGame();
    beginGame();





    while (gameRunning == true)
    {
            panel.repaint();
    }       

    // TODO a lot
}

private static void showGUI()
{

    JFrame frame = new JFrame("Breakout");  
    game = new Breakout();                   

    frame.addKeyListener(game);

    frame.add(panel);

    frame.setSize(1000,1000);
    frame.setLocationRelativeTo(null);                          //  create game window
    frame.setVisible(true);                                 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   


}


public void keyPressed(KeyEvent e) {
    Paddle.keyPressed(e);   
}


public void keyReleased(KeyEvent e) {
    Paddle.keyReleased(e);
}


public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

}

package assignment1;

import java.awt.*;
import java.util.Vector;
import javax.swing.*;

public class PaintingPanel extends JPanel{





public static Vector contents;


public PaintingPanel(Vector contents)
{   
    contents = new Vector();
    contents.addElement(Breakout.paddle);
    contents.addElement(Breakout.ball);



    System.out.println(contents);

}

public void paintComponent(Graphics g)
{

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setBackground(Color.GREEN);
    g2d.drawRect(2, 2, 800, 800);
    Breakout.paddle.paintThis(g2d);
    Breakout.ball.paintThis(g2d);       
}

}

package assignment1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Paddle {



public static int PADDLE_SIZE = 100;
public static int PADDLE_THICKNESS = 10;
public static int centreX = 400;                                // X Centre of the paddle
public static int centreY = 795;                                // Y Centre of the paddle
public static int paddleX = centreX - (PADDLE_SIZE/2);          // Top left X coordinate
public static int paddleY = centreY -(PADDLE_THICKNESS/2);      // Top left Y coordinate    
public static int dirX;


public Paddle(int paddleX, int paddleY, int PADDLE_SIZE, int PADDLE_THICKNESS)
{
    this.paddleX = paddleX;
    this.paddleY = paddleY;
    this.PADDLE_SIZE = PADDLE_SIZE;
    this.PADDLE_THICKNESS = PADDLE_THICKNESS;

    System.out.println("Paddle created at " + paddleX + ", " + paddleY + ", " + PADDLE_SIZE + ", " + PADDLE_THICKNESS);


}

public static void paintThis(Graphics g)
{
    g.setColor(Color.BLACK);
    g.fillRect(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS);
    g.drawRect(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS);

}

public static Rectangle getBounds()
{
    return new Rectangle(paddleX, paddleX, PADDLE_SIZE, PADDLE_THICKNESS);

}

public static void move()
{
    paddleX = paddleX + dirX;
}

public static void keyPressed(KeyEvent e) 
{

    if (e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        dirX = -10;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        dirX = 10;
    }
    else {}
}

public static void keyReleased(KeyEvent e) 
{
    dirX = 0;
}

}

package assignment1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class Ball {

public static int x;
public static int y;
public static int radius;
public final static Color colour = Color.RED;
public static double dirX;
public static double dirY;
public static int speed = 0;


public Ball(int x, int y, int radius)
{       
    this.x = x;
    this.y = y;
    this.radius = radius;
    dirX = 0;
    dirY = -Math.sqrt(1 - dirX*dirX);
    System.out.println("Ball created at " + x + ", " + y + ", " + dirX + ", " + dirY);
}


public static void paintThis(Graphics g)
{
    g.setColor(colour);
    int width = radius*2;
    g.fillOval(x, y, width, width);
    g.drawOval(x, y, width, width);
}


public static Rectangle getBounds()
{
    return new Rectangle(x, y, radius*2, radius*2);                     
}



public static void move()
{
    x = (int) (x-dirX*speed);           //check for inaccuracy
    y = (int) (y-dirY*speed);
    System.out.println("Moved, New X = " + x + ", new Y = "+ y);
}




public void reflect(boolean horizontal, boolean vertical)
{
    if (vertical == true)
    {
        dirX = 0-dirX;
    }
    if (horizontal == true)
    {
        dirY = 0-dirY;
    }
}


public boolean intersects(Paddle paddle) {
    if (getBounds().intersects(Paddle.getBounds())){
        return true;
    }
    else return false;
}

}

https://dl.dropboxusercontent.com/u/65678182/assignment1.rar

user3483682
  • 51
  • 1
  • 7
  • 1
    Post the code for `getBounds()` and `intersects()`. – takendarkk Apr 01 '14 at 22:49
  • 1
    possible duplicate of [How to make my collision check with Intersect between rectangles to work?](http://stackoverflow.com/questions/22777498/how-to-make-my-collision-check-with-intersect-between-rectangles-to-work) – takendarkk Apr 01 '14 at 22:52
  • I did post a link to the whole code there so now you can check it all – user3483682 Apr 01 '14 at 23:11
  • I see that @yannis removed your link for some reason. Forgive me, but I'm a bit leary about downloading and opening files when I'm not sure of their origins. Besides, you shouldn't make us browse through all your code when the problem is in only a small portion of it. Take the _relevant_ sections, and post them here. – takendarkk Apr 01 '14 at 23:14
  • The link you posted only includes the class files, we'll need to see the source files. – turingcomplete Apr 01 '14 at 23:23
  • just pasted the code in now, sorry for the confusions :) – user3483682 Apr 01 '14 at 23:24
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) See [this example](http://stackoverflow.com/a/14575043/418556) for an MVCE that uses the Java-2D API to do the hard work. – Andrew Thompson Apr 02 '14 at 06:26

1 Answers1

1

The getBounds() method in the Paddle class seems to be the problem. You are returning a Rectangle with upper left coordinates of paddleX and paddleX. I think you meant return new Rectangle(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS); instead.

turingcomplete
  • 2,128
  • 3
  • 16
  • 25
  • Omg, i have been sitting for many hours over such a stupid mistake.... thank you :D – user3483682 Apr 01 '14 at 23:49
  • Any headers on how to determine which side of the rectangle the ball would be hitting an object? obviously no need for the intersection with the paddle, but for the walls + bricks i need to know which side they are hitting so i reflect the ball the correct way. cant seem to find a good tutorial yet on this :P – user3483682 Apr 02 '14 at 01:35
  • You already know the position of the ball, coordinates of the walls, and the positions of the blocks. You can determine if the ball hits a side wall by seeing if its x-coordinate reaches 0 or the board width. To determine if it has hit the ceiling, then check if its y-coordinate reaches the top of the board. For the blocks, you can use the intersect method to determine if the ball intersects with a block. In any case of the above, if the ball hits anything, then just reverse the direction of movement of the ball by reversing the sign of the displacement amount. – turingcomplete Apr 02 '14 at 02:01
  • but just reversing the movement wont really work will it? if the ball moves in the direction North-East, and then hit the underside of a brick, you would only want change the vertical direction and make it go South-East, but if it hits the side of a brick, you want to only revert the Horizontal movement so that it will continue going North-West. if you would just reverse all the direction of movement wouldn't the ball just bounce back exact the same way it came? – user3483682 Apr 02 '14 at 02:24
  • Yes, you are correct. My bad, it's 4:30 AM over here. Well, resolving the collisions with respect to the walls is straightforward. As for the blocks, you can check for collisions using intersects method. If there's an intersection, then you can manually check which side you hit the block from by comparing the coordinates of the ball and the block. – turingcomplete Apr 02 '14 at 02:38
  • I can come up a way to check this for all case except when it hits the corners (as in it moves into the rectangle with only a corner and not the whole side, then i cant determine which one is the right) I bet there is some forumla out there, but it cant come to my mind yet haha :P – user3483682 Apr 02 '14 at 09:10