From aa242beb73a1f6cf0bec2eb8dabd77b8f34f83bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20S=C3=A4gesser?= Date: Tue, 13 Jan 2015 14:17:51 +0100 Subject: - how to use PolygonPathFinder from GD Script --- demos/2d/polygon_path_finder_demo/.fscache | 4 ++ demos/2d/polygon_path_finder_demo/engine.cfg | 5 ++ demos/2d/polygon_path_finder_demo/icon.png | Bin 0 -> 3639 bytes demos/2d/polygon_path_finder_demo/icon.png.flags | 1 + .../new_scene_poly_with_holes.scn | Bin 0 -> 2609 bytes .../polygon_path_finder_demo/polygonpathfinder.gd | 80 +++++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 demos/2d/polygon_path_finder_demo/.fscache create mode 100644 demos/2d/polygon_path_finder_demo/engine.cfg create mode 100644 demos/2d/polygon_path_finder_demo/icon.png create mode 100644 demos/2d/polygon_path_finder_demo/icon.png.flags create mode 100644 demos/2d/polygon_path_finder_demo/new_scene_poly_with_holes.scn create mode 100644 demos/2d/polygon_path_finder_demo/polygonpathfinder.gd diff --git a/demos/2d/polygon_path_finder_demo/.fscache b/demos/2d/polygon_path_finder_demo/.fscache new file mode 100644 index 0000000000..f699ca5849 --- /dev/null +++ b/demos/2d/polygon_path_finder_demo/.fscache @@ -0,0 +1,4 @@ +::res://::1421147952 +icon.png::ImageTexture::1420046079:: +new_scene_poly_with_holes.scn::PackedScene::1421147952:: +polygonpathfinder.gd::GDScript::1421146502:: diff --git a/demos/2d/polygon_path_finder_demo/engine.cfg b/demos/2d/polygon_path_finder_demo/engine.cfg new file mode 100644 index 0000000000..41c4adf701 --- /dev/null +++ b/demos/2d/polygon_path_finder_demo/engine.cfg @@ -0,0 +1,5 @@ +[application] + +name="polygon_path_finder_demo" +main_scene="res://new_scene_poly_with_holes.scn" +icon="icon.png" diff --git a/demos/2d/polygon_path_finder_demo/icon.png b/demos/2d/polygon_path_finder_demo/icon.png new file mode 100644 index 0000000000..0c422e37b0 Binary files /dev/null and b/demos/2d/polygon_path_finder_demo/icon.png differ diff --git a/demos/2d/polygon_path_finder_demo/icon.png.flags b/demos/2d/polygon_path_finder_demo/icon.png.flags new file mode 100644 index 0000000000..dbef2209e8 --- /dev/null +++ b/demos/2d/polygon_path_finder_demo/icon.png.flags @@ -0,0 +1 @@ +gen_mipmaps=true diff --git a/demos/2d/polygon_path_finder_demo/new_scene_poly_with_holes.scn b/demos/2d/polygon_path_finder_demo/new_scene_poly_with_holes.scn new file mode 100644 index 0000000000..07838be41e Binary files /dev/null and b/demos/2d/polygon_path_finder_demo/new_scene_poly_with_holes.scn differ diff --git a/demos/2d/polygon_path_finder_demo/polygonpathfinder.gd b/demos/2d/polygon_path_finder_demo/polygonpathfinder.gd new file mode 100644 index 0000000000..a0e71dd127 --- /dev/null +++ b/demos/2d/polygon_path_finder_demo/polygonpathfinder.gd @@ -0,0 +1,80 @@ + +extends Spatial + +func _ready(): + var pf = PolygonPathFinder.new() + + var points = Vector2Array() + var connections = IntArray() + + # poly 1 + points.push_back(Vector2(0, 0)) #0 + points.push_back(Vector2(10, 0)) #1 + points.push_back(Vector2(10, 10)) #2 + points.push_back(Vector2(0, 10)) #3 + + connections.push_back(0) # connect vertex 0 ... + connections.push_back(1) # ... to 1 + drawLine(points[0], points[1], get_node("/root/Spatial/Polys")) + connections.push_back(1) # connect vertex 1 ... + connections.push_back(2) # ... to 2 + drawLine(points[1], points[2], get_node("/root/Spatial/Polys")) + connections.push_back(2) # etc. + connections.push_back(3) + drawLine(points[2], points[3], get_node("/root/Spatial/Polys")) + connections.push_back(3) # connect vertex 3 ... + connections.push_back(0) # back to vertex 0, to close the polygon + drawLine(points[3], points[0], get_node("/root/Spatial/Polys")) + + # poly 2, as obstacle inside poly 1 + points.push_back(Vector2(2, 0.5)) #4 + points.push_back(Vector2(4, 0.5)) #5 + points.push_back(Vector2(4, 9.5)) #6 + points.push_back(Vector2(2, 9.5)) #7 + + connections.push_back(4) + connections.push_back(5) + drawLine(points[4], points[5], get_node("/root/Spatial/Polys")) + connections.push_back(5) + connections.push_back(6) + drawLine(points[5], points[6], get_node("/root/Spatial/Polys")) + connections.push_back(6) + connections.push_back(7) + drawLine(points[6], points[7], get_node("/root/Spatial/Polys")) + connections.push_back(7) + connections.push_back(4) + drawLine(points[7], points[4], get_node("/root/Spatial/Polys")) + + + print("points: ",points) + print("connections: ",connections) + + pf.setup(points, connections) + + var path = pf.find_path(Vector2(1, 5), Vector2(8, 5)) + + var lastStep = null + print("path: ",path) + for step in path: + print("step: ",step) + if (lastStep != null): + var currPathSegment = Vector2Array() + drawLine(lastStep, step, get_node("/root/Spatial/Path")) + lastStep = step + + + +func drawLine(pointA, pointB, immediateGeo): + var drawPosY = 0.1 + var im = immediateGeo + + im.begin(Mesh.PRIMITIVE_POINTS, null) + im.add_vertex(Vector3(pointA.x, drawPosY, pointA.y)) + im.add_vertex(Vector3(pointB.x, drawPosY, pointB.y)) + im.end() + im.begin(Mesh.PRIMITIVE_LINE_STRIP, null) + im.add_vertex(Vector3(pointA.x, drawPosY, pointA.y)) + im.add_vertex(Vector3(pointB.x, drawPosY, pointB.y)) + im.end() + + -- cgit v1.2.3