# How to Handle my Collisions in java

I'm currently making a color switch game. All my obstacles are made out of 4 shapes of a different color. The shapes are in an array with their index being the same index as its color in a array of colors. The player is a ball or Ellipse shape with a color curColor. How do I see if it overlaps shapes with a different color?

Perhaps using something like a quadtree might be of some use here. By splitting collision detection into two stages, first coarse then fine you don't waste time computing collision between distant objects.

Split your game world up into rectangles and populate with the stationary(?) obstacles. Each time your player ball moves, find what quads in the tree the bounding box of the player intersects with and return obstacles in these quads. With these, use a more precise collision detection method between the two shapes, using color of the shape as a predicate before checking.

It might be worth considering if obstacles are moving and you need to add them to the quad tree every frame anyway, just adding in colored obstacles which are relevant for collision detection.

If the other shapes are rectangles you could use this to check if a circle intersects with it:

double left  = //rectangle's most left coordinate
double bottom = //rectangle's most bottom coordinate
//etc
double centerX = //player (circle) center X coordinate
double centerY //it's y coord

//it is intersecting
}


And then the same for the top and right coords replacing bottom with top, replacing left with (you guessed it) right and replacing < with >.

If the shape is a circle it will be just a little bit more complicated:

double r0 //radius of player
double r1 //radius of shape
double x0 //center X of player
double x1 //center X of shape
double y0 //center Y of player
double y1 //center Y of shape

double DX = x1 - x0;
double DY = y1 - y0;
double D = Math.hypot(DX, DY);
if(D<r0+r1){
//They are intersecting
}