0
\$\begingroup\$

I have a ball inside of a multi-colored circle, the ball will hit the inner area of the circle like this bellow circle image

multiColoredCirlce

And the Circle will be rotated clockwise or anti-clockwise based on user input. How would I know, where the ball hit the Big Colored circle.

I just need to identify in which color of my circle sprite has made a collision with the ball.

I have tried a lot, googled a lot but still, the solution has not been found yet.

Experts, please help me.

\$\endgroup\$
4
  • \$\begingroup\$ Can you add some code on how the circle colors are defined? Are you using box2d? Personally I think it would be more convenient not to use it if the game you're trying to make is Color Switch-like. You would just need to check the angle of the ball in the circle and compare it with the color distribution of the circle. \$\endgroup\$ – Nicolas May 25 '19 at 13:41
  • \$\begingroup\$ i wanted to use box 2d but truly speaking i can't decide which one will be perfect . Small ball will bounce if it hits the same color area of the big circle it will need box 2d, but main challenge is where the small ball hit on big circle. So i just need to calculate which part of the big circle has been hit by the small ball. So my question is how to calculate the different color area of a circle like the above image , and how to calculate the hitting position by the ball . Don't need box2d, just from the sprite how to calculate it please help @Nicolas \$\endgroup\$ – Avijit Biswas May 25 '19 at 20:05
  • \$\begingroup\$ I have little experience with box2d but I think you should read some info here and watch this. In the video, a circle body (actually a polygon with many sides) is created in 4 parts. You could then detect the collision with each part. \$\endgroup\$ – Nicolas May 25 '19 at 20:14
  • \$\begingroup\$ If i want to make it like color switch- like then how do track the angle of the ball and how do i know where the ball hits. Just avoid box 2d. \$\endgroup\$ – Avijit Biswas May 25 '19 at 20:22
1
\$\begingroup\$

If you want to make something like color switch and you want to detect the collision with the circle.

Your circle is described by the class:

public class Circle {
    public Vector2 pos = new Vector2();  // Circle center position
    public float angle = 0f;  // Rotation angle, between 0 and 2*pi

    public float innerRadius = 100f;  // Inner radius
    public float thickness = 10f;  // Thickness

    // The list of colors in the circle in anticlockwise order. 
    // Each color takes the same portion of the circle.
    public Color[] colors = new Color[]{Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE};  

    // a constructor here probably, setters, getters...
}

And your ball by the class:

public class Ball {
    public Vector2 pos = new Vector2();  // Ball center position
    public float radius = 10f;  // The ball radius
    public Color color = Color.WHITE;

    // probably more attributes like vertical speed

    // a constructor here probably, setters, getters...
}

On each frame, you update the ball position, circle rotation and then check for collision.

If you want to check if the ball collides with a circle, you have two possibilities:

  1. The ball is completely inside or completely outside the circle. That means the ball can't be colliding with the circle.

  2. Otherwise the ball touches the circle outline. The angle of the ball in the circle must be found to check if the color is right to decide of the collision.

It could look like this:

public boolean isBallCollidingWithCircle(Ball ball, Circle circle) {
    // Compute the distance between the circle and ball centers
    double dx = (double) (ball.pos.x - circle.pos.x);
    double dy = (double) (ball.pos.y - circle.pos.y);
    double distance = Math.hypot(dx, dy);

    if (distance < circle.innerRadius - ball.radius ||
            distance > circle.innerRadius + circle.thickness + ball.radius) {
        // No collision possible.
        return false;
    }

    float angle = Math.atan2(dy, dx);
    int colorsCount = circle.colors.length
    // Not entirely sure about this line, basically find the circle color from the angle.
    Color collisionColor = circle.colors[((angle + circle.rotation) / (2 * Math.PI) * colorsCount  + colorsCount) % colorsCount];
    if (collisionColor.equals(ball.color)) {
        // Same color, no collision.
        return false;
    }

    // Not the same color, collision.
    return true;
}

Make sure all the coordinates are in a Y-up system since atan2 needs that. I included x coordinates but I'm not sure if that's necessary for Color Switch.

I cut some corners obviously, for example if the ball inside the circle outline, no collision will be detected until the ball center touches the wrong color (and not the ball outline). If you really plan on making a game similar to Color Switch, I encourage you to use box2d for easier collision detection. (circle-circle collision is simple, but circle-polygon is something else...)

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I am gonna try this .. \$\endgroup\$ – Avijit Biswas May 25 '19 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.