# Color Switch Collision Detection

If you don't know what color switch is look here: http://poki.com/en/g/color-switch

I need a good way to detect the color collisions, one way would be to get the colors of pixels around the ball on the jpanel but I don't know how that would work. I can't use Shapes and Area intersections because some of the obstacle shapes have a hole in the center so I can't use the shape and area class to find collisions.

Any Ideas would be great.

All my Obstacles are made with A multiple Shape objects for different colors and holes.

I strongly suggest you to stay away from the pixel color checking, as it will decrease the performance a lot. Sorry for giving this kind of information but creating this kind of game with standard java libraries is completely the same with re-inventing the wheel.

What I would recommend to you is try using a framework at least, like Libgdx, slick2D. Because you need to create classes for obstacles, since you need to carry out the shape, color, rotation speed etc. at the same time. That is nearly impossible work to do with Java libraries. With those frameworks I guess you would be able to create obstacle shapes with hole in the center.

From the quick preview of the game, I assume it would be possible to just do a circle vs circle collision detection. You could accomplish this by seeing if the intersection range is more and less then a certain threshold (this would like you go inside the ball, but a small area would be intersectable).

To accomplish the color collision part, you could test the ball colour with a hard coded colour related to the rotation of the ring (rotation % 360) and say if it is less than 90 but more than 0, only a certain colour can pass. Of course this would only work for one axis (like in the demo), but I assume you could expand it to use the x axis aswell.

Demo (FLAWED doesnt take in account both rings will edit oops!)

public void testCollision()
{
collision = false;
float x1 = ballPosX;
float y1 = ballPosY;
float x2 = ringPosX;
float y2 = ringPosY;
r1 = ballScale;
r2 = ringScale;
float distanceBetween = (x2-x1)^2 + (y1-y2)^2;
float ringThickness = 0.5f;
if(distanceBetween <= (r1+r2)^2)
{
if(y1 + r1 > y2 + yScale - ringThickness && y1 + r1 < y2 + yScale + ringThickness )
{
collision = true;
}
//do same for bottom of ring
}
if(collision)
{
float ringRotation = someValue;
if(ringRotation % 360  < 90 && ballColour == RED)
{
killBall;
}
else
{
//ball is OK
}
else if(ringRotation % 360  < 180 && ballColour == BLUE)
{
killBall;
}
//FOR OPPOSITE SIDE OF RING : do same thing but check if ball y > ring y (or other way around) and then do the opposite value check (eg: 90 degree = 270 degree)
//etc etc...
}
}