Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

I need a help in BouncingBall

Hi,
I have a BouncingBall program, I'm having problem to add something here.
I have a ball that bounce on the screen.I need add something that makes two ball collide with each other and bounce away from each other appropriately. Can anybody help me in that.

Here is my code;

BouncingBall Class:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BouncingBall extends JFrame
{
private int x;
private int y;
private int xv;
private int yv;
private int R;
private int G;
private int B;
private Color color;
public int xMax=400;
public int yMax=400;
public static int r = 15;
private MouseHandler mh;
public static ArrayList<Ball> ballsList;
Ball thisBall;
BouncingBall bb;

public BouncingBall()
{

super( "Click in the window to add balls" );
ballsList = new ArrayList<Ball>();
mh = new MouseHandler( );
addMouseListener( mh );
setSize( xMax, yMax );
setVisible( true );

bb = this;
}

private class MouseHandler extends MouseAdapter{
public void mouseClicked(MouseEvent me){

x = me.getX();
y = me.getY();
R = 0 + (int) (256 * Math.random());
G = 0 + (int) (256 * Math.random());
B = 0 + (int) (256 * Math.random());
color = new Color(R, G, B);

ballsList.add(thisBall = new Ball(bb, x, y, xMax, yMax, color));
repaint();

}
}

public void paint(Graphics g)
{
super.paint(g);
for (Ball b : ballsList)
{
b.setXmax(this.getWidth());
b.setYmax(this.getHeight());
b.draw(g);
repaint();

}
}

public static void main(String [] args){


BouncingBall app = new BouncingBall();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );



}
}

BAll class

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

public class Ball
{
private int x;
private int y;

private int xv;
private int yv;
public int xMax=350;
public int yMax=350;
public static int r = 15;
private Color color;

BouncingBall space;
public Ball(BouncingBall spacex, int ballx, int bally,
int xMax, int yMax, Color ballcolor)
{

x = ballx;
y = bally;
int theta = (int)(Math.random() * (-2 * Math.PI));
xv = (int)(r * (Math.cos(theta)/10)) ;
yv = (int)(r * (Math.sin(theta)/8));
this.xMax = xMax;
this.yMax = yMax;
color = ballcolor;
space = spacex;

}


public void setXmax(int newXmax)
{
xMax= newXmax;
}

public void setYmax(int newYmax)
{
yMax = newYmax;
}
public void collide()
{

}


public void Bounce()
{
if (x+xv>xMax)

{
x=xMax;
xv=-xv;
}
else if (x+xv<0)

{
x=0;
xv=-xv;
}
else
x+=xv;

if (y+yv>yMax)

{
y=yMax;
yv=-yv;
}
else if (y+yv<0)

{
y=0;
yv=-yv;
}
else
y+=yv;

space.repaint();
}

public void draw(final Graphics g){
//g.setColor(g.getBackground());
g.setColor(color);
g.fillOval(x - r/2, y - r/2, r, r);
Bounce();


}

}
[3833 byte] By [nosuguy] at [2007-11-11 8:26:27]
# 1 Re: I need a help in BouncingBall
at every animation step (movement of any ball) you will have to include a collision detection.
eg. when you iterate the list off balls and move them, you will have to iterate it once more and check, if the bounds of two balls overlap. since your balls are circles (i think?), you will have to solve a circular equation like (a-x)^2+(b-y)^2<=r^2. x and y are the center coordinates of the ball, r the radius. entering any value of a or b returns <= r^2 if a and b are coordinates within the ball, >r^2 if the given coordinates are outside the ball. since you have two balls, you will have that equation twice and will have to merge them together to a bigger equation.
graviton at 2007-11-11 22:35:28 >
# 2 Re: I need a help in BouncingBall
by the way, have a look at this howto intersection of two circles ( http://astronomy.swin.edu.au/~pbourke/geometry/2circle/)
graviton at 2007-11-11 22:36:22 >
# 3 Re: I need a help in BouncingBall
That article descibes and reminds me of the following fact:
to determine, if there is a collision (no matter at which points), you don't need to handle the equations. you just have to calculate the distance of the middle points of both circles. if the distance is smaller or equal to the sum of both diameters, there is a collision. however, for a correct bouncing you will have to calculate more than the actual positions of the balls. they need a direction and a velocity. on collision, the directions of both balls will change.
graviton at 2007-11-11 22:37:32 >