0

I created a java program to draw out 2 circle and using keyboard to move..and i need to test the collision...once the two ball go together..it will pop up the message "Collision detected"

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class BallObject {
private int x;
private int y;
private int radius;

BallObject() {
    x=0;
    y=0;
    radius=0;
}

BallObject (int x,int y,int radius) {
    this.x=x;
    this.y=y;
    this.radius=radius;
}
public void setX(int x) {this.x=x;}
public void setY(int y) {this.y=y;}
public void setRadius(int r) {radius=r;}

public int getX() {return x;}
public int getY() {return y;}
public int getRadius() {return radius;}


}

class Ball extends JFrame implements KeyListener {
 BallObject ball1;
 BallObject ball2;

Ball() {
    super("COLLISION DETECTION BETWEEN TWO BALLS");

    setSize(800,600); //set screen resolution
    ball1 = new BallObject(getWidth()/2,getHeight()/2,20);
    ball2 = new BallObject(40,40,20);
    addKeyListener(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;

    g2d.setColor(Color.BLACK);
    g2d.fill(new Rectangle(0,0,800,600));

    //drawing ball1
    g2d.setColor(Color.RED);
    g2d.fillOval(ball1.getX(),ball1.getY(),ball1.getRadius()*2,ball1.getRadius()*2);
    //drawing ball2
    g2d.setColor(Color.GREEN);
    g2d.fillOval(ball2.getX(),ball2.getY(),ball2.getRadius()*2,ball2.getRadius()*2);
}

public static void main (String args[]) {




   new Ball();
}


public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_LEFT)
        ball1.setX(ball1.getX()-2);

    if(e.getKeyCode()==KeyEvent.VK_RIGHT)
        ball1.setX(ball1.getX()+2);

    if(e.getKeyCode()==KeyEvent.VK_UP)
        ball1.setY(ball1.getY()-2);

    if(e.getKeyCode()==KeyEvent.VK_DOWN)
        ball1.setY(ball1.getY()+2);


    if(e.getKeyCode()==KeyEvent.VK_A){
            ball2.setX(ball2.getX()-2);
            //System.out.println("Hello");
    }


    if(e.getKeyCode()==KeyEvent.VK_D)
        ball2.setX(ball2.getX()+2);

    if(e.getKeyCode()==KeyEvent.VK_W)
        ball2.setY(ball2.getY()-2);

    if(e.getKeyCode()==KeyEvent.VK_S)
        ball2.setY(ball2.getY()+2);


        repaint();
}


            //redraw the screen to show the updated ball location
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}

}

So now i wanna to test collision between two circle..

if (((ball2.getX()-ball1.getX())* (ball2.getX()-ball1.getX())) + ((ball2.getY()-ball1.getY())* (ball2.getY()-ball1.getY()))
            < ball1.getRadius() + ball2.getRadius()* ball1.getRadius() + ball2.getRadius() );

{
    System.out.println("The 2 circles are colliding!");
}
    }

But the program can't work properly..pls help..

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For tips, see [this answer](http://stackoverflow.com/a/13796268/418556) & [this answer](http://stackoverflow.com/a/14575043/418556). – Andrew Thompson Feb 04 '13 at 08:44

1 Answers1

4

1) Why do you have the term ball2.getRadius()* ball1.getRadius()? (R1 + R2)*(R1 + R2) should be the square of the distance between the two.

2) Please clean up the code..

int dX = ball2.getRadius()* ball1.getRadius(), dY = ball2.getY()-ball1.getY();
int R = ball1.getRadius() + ball2.getRadius();

if( (dX*dX +dY*dY) < R*R){

or move all this into member functions.

if( ball2.distanceSquared(ball1) <R*R){

or

if(ball2.collide(ball1)){
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • Thx for the info Karthink..By the way,i am new for java..i apply this formula into main function after public static void main (String args[]) but it cant work properly.. – Ong Pek Feb 04 '13 at 07:54
  • @OngPek you need to elabourate "cant work properly" if my #1 doesnt fix it. – Karthik T Feb 04 '13 at 07:56
  • This is my main function public static void main (String args[]) { new Ball(); int dX = ball2.getRadius()* ball1.getRadius(), dY = ball2.getY()-ball1.getY(); if( (dX*dX +dY*dY) – Ong Pek Feb 04 '13 at 08:00
  • @OngPek that is a different problem. Need to see you code in more detail, but basically it is as the error says, You cannot directly access an instance of `ball1` or `ball2` without having an instance of the class that contains them. Also, the `new Ball()` that is the first line of your code is pointless, it does nothing useful, since you do no keep a reference to the object. – Karthik T Feb 04 '13 at 10:14
  • Karthik..Do you mind gv me your email i will send you my coding ? Pls i seriously need help... – Ong Pek Feb 04 '13 at 10:50
  • 1
    *"Do you mind gv me your email.."* That is not what SO is about. If it goes 'off-forum' it is of no use to the community that contributes to SO. *"i seriously need help.."* Not our problem, seriously. – Andrew Thompson Feb 05 '13 at 09:30