为什么我的线条在它们相互正确交叉时检测不到(光滑线类)

时间:2013-10-07 17:10:30

标签: java collision lines

我一直在努力寻找问题并尝试使用循环替代方法来使我的线路交叉点正常工作无济于事。线路类来自光滑,我100%它正常工作,因为我在课堂外进行了测试。

这是一个显示我的问题的图像(粉红色矩形中的顶点应该在到达屏幕中间的黑色方块之前相交(说实话,此刻每个顶点都应该触及自己的边缘,但这是一个不同的问题(?))) http://imgur.com/aJj4a5t

这是一个显示红色边缘和蓝色顶点的图像。白线目前正在创建。正方形另一侧的每个顶点都不应该有一条连接到它的白线.imgur.com/MGdEbii

这是我的Light类遇到问题。 (问题从“检查线路交叉口”开始标记。感谢随时提供帮助。

import java.util.ArrayList;


import org.newdawn.slick.geom.Line;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Polygon;


public class Light extends Play{

private ArrayList<Point> points = new ArrayList<Point>();
private ArrayList<Point> points2 = new ArrayList<Point>();
private ArrayList<Polygon> polys = new ArrayList<Polygon>();




public ArrayList<Polygon> light(Players p){

    Point head = new Point(p.getRectangle().getX() + p.getRectangle().getWidth()/2, p.getRectangle().getY()); //Players eyes

    //=====================//Puts all vertices & Edges into two groups//=====================//
    for (int n = 0; n <= bounds.size() - 1; n++){
        Bounds b = bounds.get(n);

        for (int c = 0; c <= 3; c++){
            edges.add(b.getBorder(c)); // Edge list is not used in this class anymore. Keeping it around to render the lines for testing purposes.
            points.add(b.getVertices(c));

        }
    }

    points.add(new Point(0,0)); //top left corner of screen
    points.add(new Point((float) gcX, 0)); //top right corner of screen
    points.add(new Point(0, (float) gcY)); // bottom left corner of screen
    points.add(new Point((float) gcX, (float) gcY)); // bottom right corner of screen

    //=====================//organizing list by smallest angles//=====================//
    while(points.size() != 0){
        Point corner = new Point(0,0);// 0,0 point so that it will never return null on accident.

        for(int q = 0; q < points.size(); q++){
            if(q == 0 ){ //if it's the first number in the list then it will make it the corner.
                corner = points.get(q);
                if(points.size() > 1)
                    q++;
                }

            if(MyMath.getAngle(head.getX(), head.getY(), corner.getX(), corner.getY()) // if the new corner is less then the old corner it will become the old corner
                    < MyMath.getAngle(head.getX(), head.getY(), points.get(q).getX(), points.get(q).getY())){
            }else
                corner = points.get(q);

                if(q == points.size() - 1){ // the corner at the end of the list will have the smallest angle in the list
                    points2.add(corner);
                    points.remove(corner);

                }
            }

        }

    //=====================//Checking Line Intersections//=====================//

    for (int a = 0; a <= points2.size() - 1; a++){
        Point v = points2.get(a);   

        Line poo = new Line(v.getX(), v.getY(), head.getX(), head.getY());

        for (int t = 0; t <= bounds.size() - 1; t++){
            Bounds b2 = bounds.get(t);

            for (int c = 0; c <= 3; c++){
                if(b2.getBorder(c).intersects(poo)){}
                else points.add(points2.get(a)); 
                }


            }

        }

// uses the new organized list to make a polygon.
    Polygon poly = new Polygon();

        for (int x = 0; x <= points.size() - 1; x++){ //makes a list of polygons -- Head is not included.
            Point q = points.get(x);
            poly.addPoint(q.getX(), q.getY());
        }

        polys.add(poly);

        points.clear();

    return polys;

}


}

这是我的边界班:

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Line;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

public class Bounds extends Editor {

    Rectangle rect = null;
        int id, x, y, w, h;
Line edge[] = new Line[4];
Point vertex[] = new Point[4];

public Bounds(int x, int y,  int id){
    this.x = x;
    this.y = y;

    rect = new Rectangle(x,y,ts,ts);
    this.id = id;

}


public Line getBorder(int i){
    return edge[i];
}

public Point getVertices(int i){
    return vertex[i];
}

public int getID(){
    return id;
}

public void refresh(){
    rect = new Rectangle(x + Camera.getPoint().getX(), y + Camera.getPoint().getY(), ts, ts);

    vertex[0] = new Point(rect.getX(), rect.getY());
    vertex[1] = new Point(rect.getX() + ts, rect.getY());

    vertex[2] = new Point(rect.getX() + ts, rect.getY() + ts);
    vertex[3] = new Point(rect.getX(), rect.getY() + ts);

    edge[0] = new Line(vertex[0].getX(), vertex[0].getY(), vertex[1].getX(), vertex[1].getY());
    edge[1] = new Line(vertex[1].getX(), vertex[1].getY(), vertex[2].getX(), vertex[2].getY());

    edge[2] = new Line(vertex[2].getX(), vertex[2].getY(), vertex[3].getX(), vertex[3].getY());
    edge[3] = new Line(vertex[3].getX(), vertex[3].getY(), vertex[0].getX(), vertex[0].getY());


}

public Rectangle getRectangle(){
    return rect;
}

最后我的Play Class。 (是的,我确实在Play中声明了我的聚合列表)

        Light l = new Light(); // Generate Light Clip
    for (Players p : players){

        //g.drawImage(Hero, p.getRectangle().getX(), p.getRectangle().getY() - 0);
        ArrayList<Polygon> polys = l.light(p);
        g.setColor(Color.white);
        for (int n = 0; n <= polys.size() - 1; n++){
            Polygon poly = polys.get(n);    
            g.draw(poly);
        }
        g.setColor(Color.black);
        g.draw(p.getRectangle());


    }

0 个答案:

没有答案