Search code examples
javaswingcollision-detection

Collision detection: Shapes overlap even after using intersects method


I am writing a code in which a circle moves randomly in a box and if it collides with any of the small rectangles inside it, it changes its heading direction/ bounces back. I am using the intersects method to find the collision between them. But the circle sometimes overlaps the rectangles rather than bouncing back on contact. I am bouncing back the ball by changing the orientation (180+current_orientation).

I am trying to solve this issue, but did not found any success yet. I read that intersects is finding the match by checking overlap of the bounding rectangles. But, how can I fix this issue. Is this problem due to the intersection or is there any issue with the way i am changing orientation. Any idea?

Code:

private void collisionAvoidanceRobot(int x, int y, int r, double robot_orientation2)
    {
        boolean collide1=false;
        boolean collide2=false;
        boolean collide3=false;
        boolean collide4=false;
        boolean collide5=false;
        boolean collide6=false;
        r+=5;
        Shape collisionrobot=new Ellipse2D.Double(x,y,r,r);
        collide1=collisionrobot.intersects(obst1);
        if(collide1)
        {
            robot_orientation=180+robot_orientation;
        }
        collide2=collisionrobot.intersects(obst2);
        if(collide2)
        {
            robot_orientation=180+robot_orientation;
        }
        collide3=collisionrobot.intersects(obst3);
        if(collide3)
        {
            robot_orientation=180+robot_orientation;
        }
        collide4=collisionrobot.intersects(obst4);
        if(collide4)
        {
            robot_orientation=180+robot_orientation;
        }
        collide5=collisionrobot.intersects(obst5);
        if(collide5)
        {
            robot_orientation=180+robot_orientation;
        }
        collide6=collisionrobot.intersects(obst6);
        if(collide6)
        {
            robot_orientation=180+robot_orientation;
        }

    }

public void setXPosition_robot(int x)
    {
        double distance=0;
        distance = unit_moved + randomDouble(0, forwardNoise);
        robot_x= (int) (x + Math.sin(Math.toRadians(robot_orientation))*distance);
        //System.out.println("Robot_X:"+robot_x);
    }

public void setYPosition_robot(int y)
{
    double distance=0;
    distance = unit_moved + randomDouble(0, forwardNoise);
    robot_y=(int) (y+ Math.cos(Math.toRadians(robot_orientation))*distance);
    //System.out.println("Robot_Y:"+robot_y);
}

private void createRobot(Graphics2D g)
{
    ArrayList<Integer> robot_list= new ArrayList<Integer>();
    robot_list=positionRobot(robot_x,robot_y);
    robot_x=robot_list.get(0);
    robot_y=robot_list.get(1);
    setNoise(0.05,0.05,5.0);
   //System.out.println("Robot:"+robot_x+"--"+robot_y+"--"+robot_orientation);
    adjustRobotOrientation();
    collisionAvoidanceRobot(robot_x,robot_y,robot_radius,robot_orientation);
    drawRobot(g,robot_x,robot_y,robot_radius);
}

Screenshot: enter image description here


Solution

  • Does the circle appear to wobble? since you are moving the circle a random distance each iteration, the distance that it moved into the square could be greater than the distance it gets on the next iteration to move in the opposite direction, this would cause the circle to "stick".

    also, your collisionAvoidanceRobot could use a for loop instead of all those ifs