Amb*_*ous 2 java swing collision-detection
我正在编写一个代码,其中我绘制了两个矩形作为障碍物和一个椭圆形作为机器人。机器人在障碍物静止时移动。问题是机器人的位置在一开始是随机生成的,它有时会与矩形重叠。我一直在努力解决这个问题,但我找不到好的解决方案。
在绘制矩形之前,我尝试使用圆的 x,y 坐标检查矩形的边界。但是,它只检查该单个点,有时其他一些圆点与矩形重叠。我尝试了 intersect 方法,但我无法在我的代码中实现它,因为我正在使用 fillOval 和 fillRect。
我知道这个问题已经在社区中多次提出。但是,所有答案都围绕 AWT 中的 intersect 方法展开。我没有使用 Rectangle/Ellipse 类。我正在使用 fillRect 和 fillOval 来创建我的形状。在 initialize 方法中,这些形状第一次随机绘制在 JPanel 上时,是否还有其他方法可以防止碰撞。
代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
我没有使用 Rectangle/Ellipse 类。我正在使用 fillRect 和 fillOval 来创建我的形状。
所以改变你的代码。
在绘制矩形之前,我尝试使用圆的 x,y 坐标检查矩形的边界。但是,它只检查该单个点,有时其他一些圆点与矩形重叠。
确切地说,编写代码并不容易,这就是您应该利用现有 API 的原因。不要试图编写自己的交集代码来重新发明轮子!我知道要使所有几何图形正确,这超出了我的技能水平。
查看Playing With Shapes了解不涉及使用 fillRect() 和 fillOval() 方法进行自己绘画的想法。