2

我正在研究 Gameplaykit 寻路概念验证,我无法让 GKObstacleGraph 正确找到路径。

在下面的代码片段中(它应该在 Xcode 7.2 游乐场中工作),如果在创建图形时提供了障碍物,path2 始终是一个空数组。如果我使用空的障碍物数组创建 obGraph 对象,则 findPathFromNode 返回正确的路径。

创建的障碍物应该是一个简单的 U 形多边形,端点在 U 内。

import UIKit

import GameplayKit

let pts = [vector_float2(2,2),
    vector_float2(3,2),
    vector_float2(3,6),
    vector_float2(7,6),
    vector_float2(7,2),
    vector_float2(8,3),
    vector_float2(8,7),
    vector_float2(2,7),
    vector_float2(2,2)]
let obstacle1 = GKPolygonObstacle(points: UnsafeMutablePointer(pts) ,
    count: pts.count)

let obGraph = GKObstacleGraph(obstacles: [obstacle1], bufferRadius: 0)

let startPt = GKGraphNode2D(point: vector_float2(5,9))
let endPt = GKGraphNode2D(point: vector_float2(5,5))
let pt3 = GKGraphNode2D(point: vector_float2(0,0))
let pt4 = GKGraphNode2D(point: vector_float2(0,9))
let pt5 = GKGraphNode2D(point: vector_float2(5,0))
let pt6 = GKGraphNode2D(point: vector_float2(10,0))

obGraph.connectNodeUsingObstacles(startPt)
obGraph.connectNodeUsingObstacles(endPt)
obGraph.connectNodeUsingObstacles(pt3)
obGraph.connectNodeUsingObstacles(pt4)
obGraph.connectNodeUsingObstacles(pt5)
obGraph.connectNodeUsingObstacles(pt6)
startPt.connectedNodes
endPt.connectedNodes
pt3.connectedNodes

let path2 = obGraph.findPathFromNode(startPt, toNode: endPt)
print(path2)
4

2 回答 2

5

我和杰克有同样的问题。我从 Will 的代码示例开始,并在 Xcode 10.3 中将其翻译为 Swift 5.0。我将它添加到 Xcode 的游戏项目模板中。我仍然得到相同的结果:来自findPath(from:to:).

在玩弄了代码之后,我意识到任何与物理相关的东西都会影响路径。显示适用于所有人的代码的唯一方法是包含SKScene所有SKNode实例的创建。请注意,我设置gravity为 0SKPhysicsWorld并且我对任何内容都添加了 no SKPhysicsBody

在操场上运行它。您可以通过点击场景中的任意位置来激活动画。

import PlaygroundSupport
import SpriteKit
import GameKit

class GameScene: SKScene {

    let nodeToMove:SKShapeNode = {
        let n = SKShapeNode(circleOfRadius: 10)
        n.lineWidth = 2
        n.strokeColor = UIColor.orange
        n.position = CGPoint(x: -200, y: 150)
        return n
    }()

    override func sceneDidLoad() {
        addChild(nodeToMove)

        let nodeToFind = SKShapeNode(circleOfRadius: 5)
        nodeToFind.lineWidth = 2
        nodeToFind.strokeColor = UIColor.red
        addChild(nodeToFind)
        nodeToFind.position = CGPoint(x: 200, y: -150)

        let nodeToAvoid = SKShapeNode(rectOf: CGSize(width: 100, height: 100))
        nodeToAvoid.lineWidth = 4
        nodeToAvoid.strokeColor = UIColor.blue
        addChild(nodeToAvoid)
        nodeToAvoid.position = CGPoint.zero

        let polygonObstacles = SKNode.obstacles(fromNodeBounds: [nodeToAvoid])
        let graph = GKObstacleGraph(obstacles: polygonObstacles, bufferRadius: 10.0)
        let end = GKGraphNode2D(point: vector2(Float(nodeToMove.position.x), Float(nodeToMove.position.y)))
        let start = GKGraphNode2D(point: vector2(Float(nodeToFind.position.x), Float(nodeToFind.position.y)))
        graph.connectUsingObstacles(node: end)
        graph.connectUsingObstacles(node: start)

        graphNodes = graph.findPath(from: end, to: start) as!  [GKGraphNode2D]
        print("graphNodes = \(graphNodes)")
    }

    var graphNodes = [GKGraphNode2D]()

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        touches.first.flatMap {_ in
            let newActions: [SKAction] = graphNodes.map { n in
                return SKAction.move(to: CGPoint(x: CGFloat(n.position.x), y: CGFloat(n.position.y)), duration: 2)
            }
            nodeToMove.run(SKAction.sequence(newActions))
        }
    }
}

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
let scene = GameScene(size: CGSize(width: 640, height: 480))
scene.scaleMode = .aspectFill
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
scene.backgroundColor = UIColor.purple
scene.physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
sceneView.presentScene(scene)

PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

控制台中的输出是:

graphNodes = [GKGraphNode2D: {-200.00, 150.00}, GKGraphNode2D: {62.85, 62.85}, GKGraphNode2D: {200.00, -150.00}]

开始状态:

动画开始前的场景

结束状态:

动画完成后的场景

警告:我不知道为什么在用户点击和动画开始之间需要一整秒。性能调优是一个单独的主题。

于 2019-08-18T15:04:55.310 回答
2

(提前抱歉 obj-c 不迅速)

在我的印象中,不需要将每个顶点添加为连接,只需告诉GKObstacleGraph它就GKPolygonObstacle足以避免生成的多边形形状。我已经使用以下内容并收到了 3 个节点来围绕我的障碍物创建一条路径(缓冲区为 10.0f),如下所示:

- (void)findPathWithNode:(SKNode *)nodeToFindPath {

    NSMutableArray *obstaclesToAvoid = [NSMutableArray array];

    for (SKNode *objectInScene in chapterScene.children) {
        if ([objectInScene.name isEqualToString:@"innerMapBoundary"]) {
            [obstaclesToAvoid addObject:objectInScene];
        }
    }


    /* FYI: The objectInScene is just a black SKSpriteNode with a 
       square physics body 100 x 100 rotated at 45°
    */

    NSArray *obstacles = [SKNode obstaclesFromNodePhysicsBodies:[NSArray arrayWithArray:obstaclesToAvoid]];
    GKObstacleGraph *graph = [GKObstacleGraph graphWithObstacles:obstacles bufferRadius:10.0f];


    GKGraphNode2D *end = [GKGraphNode2D nodeWithPoint:vector2((float)character.position.x, (float)character.position.y)];
    GKGraphNode2D *start = [GKGraphNode2D nodeWithPoint:vector2((float)nodeToFindPath.position.x, (float)nodeToFindPath.position.y)];


    [graph connectNodeUsingObstacles:end];
    [graph connectNodeUsingObstacles:start];


    NSArray *pathPointsFound = [graph findPathFromNode:enemy toNode:target];
    NSLog(@"Path: %@", pathPointsFound);


    GKPath *pathFound;


    // Make sure that there were at least 2 points found before creating the path
    if (pathPointsFound.count > 1) {
        for (GKGraphNode2D *nodeFound in pathPointsFound) {

            // This is just to create a visual for the points found

            vector_float2 v = (vector_float2){(float)nodeFound.position.x, (float)nodeFound.position.y};
            CGPoint p = CGPointMake(v.x, v.y);
            SKShapeNode *shapetoadd = [SKShapeNode shapeNodeWithCircleOfRadius:4];
            shapetoadd.name = @"shapeadded";
            shapetoadd.fillColor = [UIColor redColor];
            shapetoadd.position = p;
            [chapterScene addChild:shapetoadd];
        }

        pathFound = [GKPath pathWithGraphNodes:pathPointsFound radius:10.0];
    }

}

希望这会为您指明正确的方向!

于 2016-02-29T21:25:19.633 回答