1. ## Collision Detection

Hey, I am hoping I can get some advice on this topic. I am currently playing around with very basic physic simulation (I should stress the basic). I created a class called particle, which has a location, and velocities (I'm using ints for positioning).

Java Code:
```public class Particle{
private int xPos, yPos;
private int dX, dY;  //delta x, delta y
...
}```
Now I draw them to a buffered image and animate them with swing timer. All of that I got working, they bounce around the screen and the collisions on the walls are handled.

I'd like to implement collision detection between the balls, simple enough, right?

My thought is to create the following method in the particle class:
Java Code:
```public boolean collide(Particle p){
Point center = new Point(xPos + (DIAMETER / 2), yPos + (DIAMETER/2));
Point pCenter = new Point(xPos + (DIAMETER/2), yPos +(DIAMETER/2));
if(distance(center, pCenter) <= DIAMETER)
return true;
return false;
}```
Where distance calculates the distance as:
change in x square + change in y squared and then take the square root of the result.

Which, in case it's not clear simply calculates the center point of the circle (since the xPos, yPos, will be the rectangular corner on the top left -- to my knowledge) then sees if the two particles are within a certain range(DIAMETER = 20). With this working it seems easy enough to simply do the following to handle collisions:

Java Code:
```for(Particle x : rainDrops){
if(p.collide(x)){
p.setDX(p.getDX() * -1);
p.setDY(p.getDY() * -1);
x.setDX(x.getDX() * -1);
x.setDY(x.getDY() * -1);
}
}```
rainDrops is an array of particles. I added in a line which would print when some collision occurs and it spams quite quickly. Now my problem is that very rarely do the particles bounce off each other. Quite often it seems they just move right through each other.

It my approach to collision detection well thought? Or should I be doing it a different way?

The code isn't too much but I'd rather not dump 200 lines of code if it's not necessary. My basic process is

Java Code:
```for all p
for all x
if p hits x
reverse direction of p and x
end if
end for
end for```

2. Senior Member
Join Date
Jul 2011
Location
Melbourne, Victoria, Australia
Posts
155
Rep Power
6

## Re: Collision Detection

That looks pretty good, Take a read of this: Shape (Java 2 Platform SE v1.4.2)
I have seen other people use:
Java Code:
```public Rectangle getBounds() {

}

and then:

object.intersects(rectangle)```
That is two of the methods in the shape class, but you should read them all.
If you made your own methods to return if you have a collision and not being lazy and use what i just did, I think its well thought. I like your way of doing it.

3. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9

## Re: Collision Detection

Consider something like a Quadtree to organize the objects into data structure. The tree can then be used to limit the number of calculations necessary between each object by avoiding comparisons between very distant objects. Also note that in the algorithm above, avoid duplicate collision checks between the same 2 objects (which would negate the affects of the collision).
Last edited by doWhile; 10-07-2011 at 11:42 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•