我的游戏有问题。我想做的事情是绘制一条线,如果线与周围环境相交,它会计算周围环境与玩家之间的最短距离,并放置一个红点(见图)
当我用一条线运行我的程序时它工作得很完美但是当我向周围的所有角落发送线条时,一些线条穿过周围环境。 (抱歉英语不好)
P.S。 线路:马里奥与周围点之间的界线 LineSur:描述边框或周围环境的线条 我试图做这样的事情 http://ncase.me/sight-and-light/
有人知道这里出了什么问题吗?
这是我的代码
public void Lighting(){
ListIterator<Surroundings> surrounding = m.getSurrounding();
Line.clear();
LineSur.clear();
Point.clear();
rememDistance=1500;
while (surrounding.hasNext() ){
Surroundings rectangle = surrounding.next();
Rectangle sur = new Rectangle((int) rectangle.grootte().getX(),(int) rectangle.grootte().getY()-13,(int) rectangle.grootte().getWidth(),(int) rectangle.grootte().getHeight());
Line.add(new Lines((int) sur.getX() , (int) sur.getY() , (int) sur.getX() , (int) (sur.getY()+sur.getHeight())));
Line.add(new Lines((int) sur.getX() , (int) sur.getY() , (int) (sur.getX()+sur.getWidth()), (int) sur.getY()));
Line.add(new Lines((int) (sur.getX()+sur.getWidth()), (int) (sur.getY()+sur.getHeight()), (int) (sur.getX()+sur.getWidth()), (int) sur.getY()));
Line.add(new Lines((int) (sur.getX()+sur.getWidth()), (int) (sur.getY()+sur.getHeight()), (int) sur.getX() , (int) (sur.getY()+sur.getHeight())));
LineSur.add(new LinesSur((int) m.player.getxPositie(), (int) m.player.getyPositie(), (int) sur.getX() , (int) sur.getY()));
LineSur.add(new LinesSur((int) m.player.getxPositie(), (int) m.player.getyPositie(), (int) (sur.getX()+sur.getWidth()), (int) sur.getY()));
LineSur.add(new LinesSur((int) m.player.getxPositie(), (int) m.player.getyPositie(), (int) sur.getX() , (int) (sur.getY()+sur.getHeight())));
LineSur.add(new LinesSur((int) m.player.getxPositie(), (int) m.player.getyPositie(), (int) (sur.getX()+sur.getWidth()), (int) (sur.getY()+sur.getHeight())));
}
//Problem is probably coming from these for loops
int distance=0;
for(int i=0;i < LineSur.size();i++){
for(int j=0;j < Line.size();j++){
Line2D.Double lineI = LineSur.get(i).points();
if(lineI.intersectsLine(Line.get(j).points())){
Point2D IntPoint = getIntersection(Line.get(j).points(),lineI);
Point.add(new Points((int) IntPoint.getX(),(int) IntPoint.getY()));
distance = (int) Point.get(Point.size()-1).point2D().distance(m.player.getxPositie(), m.player.getyPositie());
if(rememDistance > distance){
rememDistance = distance;
System.out.println(rememDistance);
Point.get(Point.size()-1).distance= rememDistance;
}
else{Point.remove((Point.size()-1));}
}
}
for(int k=0;k < Point.size();k++){
if(rememDistance == Point.get(k).distance){
Point.get(k).shortest=true;}
}
rememDistance = 1500;
}
for(int i=0;i < Point.size();i++){
if(Point.get(i).shortest==false){Point.remove(i);}
}
}
并且继承了getIntersection方法
public Point2D getIntersection(final Line2D.Double line1, final Line2D.Double line2){
final double x1,y1, x2,y2, x3,y3, x4,y4;
x1 = line1.x1; y1 = line1.y1; x2 = line1.x2; y2 = line1.y2;
x3 = line2.x1; y3 = line2.y1; x4 = line2.x2; y4 = line2.y2;
final double x = ((x2 - x1)*(x3*y4 - x4*y3) - (x4 - x3)*(x1*y2 - x2*y1)) /((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4));
final double y = ((y3 - y4)*(x1*y2 - x2*y1) - (y1 - y2)*(x3*y4 - x4*y3)) /((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4));
return new Point2D.Double(x, y);
}