2016-01-20 42 views
1

我正在研究Gameplaykit路徑查找概念證明,並且我無法使GKObstacleGraph正確查找路徑。GKObstacleGraph在障礙物引入時未找到路徑

在下面的代碼片段中(它應該在Xcode 7.2操作系統中工作),如果創建圖形時提供了障礙物,那麼path2始終是一個空數組。如果我使用空的障礙數組創建obGraph對象,則findPathFromNode返回正確的路徑。

創建應符合(預先抱歉OBJ-C不SWIFT)內的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) 
+0

最好把GameplayKit當成一種意向手勢而不是一個可用的成品。 – Confused

+1

那麼,這不是很令人鼓舞@Confused –

+0

我還應該指出,幾乎沒有人使用GameplayKit。所以你得到一個有見識的答案的機會很渺茫。 – Confused

回答

1

終點是一個簡單U形多邊形障礙物

這是我的印象不需要將每個頂點添加爲連接,僅告訴GKObstacleGraphGKPolygonObstacle就足以避免生成的polgyon形狀。我用以下並獲得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]; 
    } 

} 

希望這點你在正確的方向!

+0

如果找不到路徑,我如何才能找到最近有效位置的路徑? – ColdSteel

+0

不完全確定...似乎如果gameplaykit沒有找到路徑,它不太開心。我想你可以嘗試迭代你的當前位置一段距離,直到你找到一條有效的路徑。當然,這在計算時間/內存方面會非常昂貴,但不知道蘋果是否提供了這種方法 –

+0

是的,蘋果沒有提供足夠的工具來解決它... 我問它作爲一個單獨的問題在SO http://stackoverflow.com/questions/39546759/gkobstaclegraph-how-to-find-closest-valid-point 從這個答案稍微修改數學我設法得到的路徑 – ColdSteel