# Collision Detection

• 10-07-2011, 05:55 PM
sunde887
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).

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:
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:

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

Code:

```for all p   for all x     if p hits x       reverse direction of p and x     end if   end for end for```
• 10-07-2011, 11:13 PM
jammas615
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:
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.
• 10-07-2011, 11:40 PM
doWhile
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).