2

I am writing a code in which I am drawing two rectangles as obstacles and an oval as the robot. The robot moves while obstacles are stationary. The problem is as the robot's location is generated randomly in the beginning, it sometimes overlap the rectangles. I am trying continuously to fix this, but I am not able to find a good solution.

I tried checking the bounds of the rectangle with the x,y coordinate of circle before drawing it. But, it only checks for that single point and sometimes some other point of circle overlaps the rectangle. I tried intersect method but I am not able to implement that in my code as I am using fillOval and fillRect.

I know this question is already asked in the community multiple times. But, all answers revolve around the intersect method in AWT. I am not using the Rectangle/Ellipse classes. I am using fillRect and fillOval to create my shapes. Is there any other way to prevent collision when in the initialize method these shapes are being first time drawn on the JPanel randomly.

Code:

public void initializePanel(Graphics g)
{
    createObstacle(g,150,225,100,40);
    createObstacle(g,500,300,40,100);  
    drawNParticles(g);
    createRobot(g); 
}

private void createRobot(Graphics 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);
    System.out.println("Robot:"+robot_x+"--"+robot_y+"--"+robot_orientation);
    if((robot_x>620)||(robot_y>395)||(robot_x<1)||(robot_y<1))
    {
        robot_orientation=180;
    }
    drawRobot(g,robot_x,robot_y,robot_radius);
}


private ArrayList<Integer> positionRobot(int x, int y)
{
    int robot_radius=50;  
    ArrayList<Integer> list= new ArrayList<Integer>();
    if(counter==0)
    {
        x=randomInteger(25,655);//so that it stays inside the content_Walls panel 
        y=randomInteger(25,425); //so that it stays inside the content_Walls panel 
        x=x-(robot_radius/2);
        y=y-(robot_radius/2);
        robot_orientation=randomFloat(0,360);
        if((x<251&&x>149)&&(y<266&&y>224))
        {
            x=0;
            y=0;
            positionRobot(x,y);
        }
        else if((x<=541&&x>499)&&(y<401&&y>299))
        {
        x=0;
        y=0;
        positionRobot(x,y);
        }
    counter++;
    }
    else
    {
        setXPosition_robot(robot_x);
        setYPosition_robot(robot_y);
    }
        list.add(x);
        list.add(y);
        return list;                
}

private void drawNParticles(Graphics g)
{   
    ArrayList<Integer> list;        
    list = new ArrayList<Integer>(Collections.nCopies(n, 0));
    for(int i=0;i<list.size();i++)
    {
       generateParticle(g);
    }
}

private void generateParticle(Graphics g)
{
    int radius = 4;
    ArrayList<Integer> list= new ArrayList<Integer>();
    list=positionParticles(particle_x,particle_y);
    g.setColor(Color.RED);
    g.fillOval(list.get(0),list.get(1), radius, radius);
}

private ArrayList<Integer> positionParticles(int x, int y)
{
    int radius = 4;
    ArrayList<Integer> list= new ArrayList<Integer>();

        x=randomInteger(2,678); // bounds of x between which the particles should be generated
        y=randomInteger(2,448); // bounds of y between which the particles should be generated
        x=x-(radius/2);
        y=y-(radius/2);
        if((x<251&&x>149)&&(y<266&&y>224))
        {
            x=0;
            y=0;
            positionParticles(x,y);
        }
        if((x<541&&x>499)&&(y<401&&y>299))
        {
            x=0;
            y=0;
            positionParticles(x,y);
        }
    list.add(x);
    list.add(y);
    return list;
}

private void createObstacle(Graphics g, int x, int y, int width, int height)
{
    //g.setColor(Color.YELLOW);
    //g.fillRect(x, y, width, height);  
}

private void drawRobot(Graphics g, int x, int y, int radius)
{
    g.setColor(Color.GREEN);
    g.fillOval(x, y, radius, radius);   
}

private static Random rand;

private static int randomInteger(int min, int max)
{
    if(rand==null)
        rand=new Random();
    int randomNum = rand.nextInt((max - min) + 1) + min;
    return randomNum;
}

private static Random randFloat;
private static float randomFloat(float min, float max)
{
    if(randFloat==null)
        randFloat=new Random();
    float randomNum = randFloat.nextFloat() *(max-min)+ min;
    return randomNum;
}
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Ambidextrous
  • 882
  • 2
  • 12
  • 26
  • Possible [duplicate](http://stackoverflow.com/questions/14574045/collision-detection-with-complex-shapes) of this [duplicate](http://stackoverflow.com/q/25099831/230513). – trashgod Aug 03 '14 at 17:56

1 Answers1

5

I am not using the Rectangle/Ellipse classes. I am using fillRect and fillOval to create my shapes.

So change your code.

I tried checking the bounds of the rectangle with the x,y coordinate of circle before drawing it. But, it only checks for that single point and sometimes some other point of circle overlaps the rectangle.

Exactly, it is not easy code to write which is why you should take advantage of existing API's. Don't reinvent the wheel by trying to write your own intersection code! I know it is above my skill level to get all the geometry correct.

Check out Playing With Shapes for ideas that don't involve doing your own painting using the fillRect() and fillOval() methods.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    @camickr I was trying to find a solution without changing existing code. But now i feel it would be better to change my code. – Ambidextrous Aug 03 '14 at 18:02