0

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.

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);
} 

I am using the following code to draw the shapes:

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);   
}

I guess i cannot use the shape.intersects then? Can I?

Is there any way to solve this issue. Is there way to check the background color at that coordinate and accordingly do some action?

Ambidextrous
  • 882
  • 2
  • 12
  • 26
  • 3
    The Shape interface has an intersects method that could be of use. – Hovercraft Full Of Eels Aug 02 '14 at 22:17
  • Is the robot an oval or a circle? You are using the formula of the circle; if so, just calculate the distance of each corner of the rectangle to the center of the circle and if any is less than the radii you have a collision. – Luis Aug 02 '14 at 22:20
  • @Luis Checking the corners agains the circle is not sufficient. An edge of the rectangle may also pass through the circle (imagine it being "nearly a tangent" to the circle) – Marco13 Aug 02 '14 at 23:42

0 Answers0