Итак, для того, что имеет отношение к этому, я создал следующие (упрощенные) классы:
Код: Выделить всё
public class Node {
private Map connectedNodes = new HashMap();
private Polygon polygon;
private TileType type;
private Vector2 position;
public Node(TileType type, float x, float y){
this.type = type;
position = new Vector2(x+ TILE_WIDTH/2f,y+TILE_HEIGHT/2f); //Center of the polygon
float[] poly = new float[]{
TILE_WIDTH/2f,0,
0,TILE_HEIGHT/4f,
TILE_WIDTH/2f,TILE_HEIGHT/2f,
TILE_WIDTH,TILE_HEIGHT/4f
};
polygon = new Polygon(poly);
polygon.setPosition(position.x - TILE_WIDTH/2f,position.y - TILE_HEIGHT/4f);
}
public void addConnectedNode(Node node, float distance){
connectedNodes.put(node, distance);
}
public Map getConnectedNodes(){
return connectedNodes;
}
public Polygon getPolygon(){
return polygon;
}
public Vector2 getPosition(){
return position;
}
public boolean isMoveable(){
return type.isMoveable();
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return Objects.equals(position, node.position);
}
}
Код: Выделить всё
public class NodeMap {
private Set moveableNodes = new HashSet();
private Set nonMoveableNodes = new HashSet();
private Zone zone;
public void addConnectedNodes(Node node1) {
for(Node node2 : moveableNodes) {
if(!node1.equals(node2) && isStraightLine(node1, node2)){
float distance = node1.getPosition().dst(node2.getPosition());
node1.addConnectedNode(node2, distance);
}
}
}
public boolean isStraightLine(Node node1, Node node2) {
for(Node nonMoveable : nonMoveableNodes){
if(Intersector.intersectSegmentPolygon(node1.getPosition(), node2.getPosition(), nonMoveable.getPolygon())){
return false;
}
}
return true;
}
}
Код: Выделить всё
public class Pathfinder {
private final NodeMap nodeMap;
public Pathfinder(NodeMap nodeMap){
this.nodeMap = nodeMap;
}
private Node createNode(Vector2 position){
Node node = new Node(TileType.GROUND, position.x, position.y);
nodeMap.addConnectedNodes(node);
return node;
}
public Path pathFromTo(Vector2 a, Vector2 b){
Node start = createNode(a);
Node goal = createNode(b);
/*
*
* implement futher pathfinding logic
*
*/
}
}
Код: Выделить всё
Node start = new Node(TileType.GROUND, coords[0][0].x, coords[0][0].y);
for(Node node : nodeMap.getMoveableNodes()){
if(nodeMap.isStraightLine(start, node)){
drawDebugLine(start.getPosition(), node.getPosition(), viewport.getCamera().combined);
start.addConnectedNode(node, 5);
System.out.println(node.getPosition());
}
}
debugRenderer.setColor(Color.RED);
drawDebugCircle(start.getPosition(), viewport.getCamera().combined);
drawDebugCircle(goal.getPosition(), viewport.getCamera().combined);
debugRenderer.setColor(Color.WHITE);
Поэтому я немного не понимаю, что здесь происходит: метод isStraightLine, похоже, дает разные результаты, получая один и тот же ввод. (Я проверил это с помощью отладки)
И они используют один и тот же экземпляр Pathfinder.
Буду признателен за любую помощь, так как я уже целый день ломаю голову над тем, в чем разница.
Подробнее здесь: https://stackoverflow.com/questions/798 ... nt-results
Мобильная версия