2

I'm currently working in Xcode 8, using Swift 3 and the new SKTileMapNode from SpritKit to make a 2D dungeon crawler type of game. I'm have trouble getting GKObstacleGraph to work with the tilemap. Please help! I tried to loop through all the tiles within the obstacle layer of the tilemap and create a polygon for each tile and store it in the GKObstacleGraph. Each tile in obstacle layer is a wall tile. The map looks like some type of dungeon crawler, so the wall is all over the places. I have something like below:

for row in 0..<tileMapNode.numberOfRows {
  for column in 0..<tileMapNode.numberOfColumns {
     let tile = tileMapNode.tileDefinition(atColumn: column, row: row)
     let tileCenter = tileMapName.centerOfTile(atColumn: column, row: row)
     //find 4 corners of each tile from its center
     let bottomLeft = float2(CGPointMake(tileCenter.x - tile.size.width/2, tileCenter.y - tile.size.height/2))
     let bottomRight = float2(CGPointMake((tileCenter.x - tile.size.width/2, tileCenter.y + tile.size.height/2))
     let topRight = float2(CGPointMake((tileCenter.x + tile.size.width/2, tileCenter.y + tile.size.height/2)) 
     let topLeft = float2(CGPointMake((tileCenter.x - tile.size.width/2, tileCenter.y + tile.size.height/2))
     var vertices = [topLeft , bottomLeft , bottomRight , topRight ]
     let obstacle = GKPolygonObstacle(points: &vertices, count: 4)
     obstacleGraph.add(obstacle)
  }
}

However, when i run the app it shows that there are over 80000 nodes, way too many pathfinding pathes. Any help would be appreciated.

Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
Bob
  • 155
  • 1
  • 8

1 Answers1

0

I'm not certain that GKObstacleGraph is the right choice of graph here. According to the GameplayKit documentation:

For example, you can design a level with the SpriteKit Scene Editor in Xcode and use physics bodies to mark regions that the player (or other game entities) cannot pass through, then use the obstaclesFromNodePhysicsBodies: method to generate GKPolygonObstacle objects marking impassable regions.

The function obstaclesFromNodePhysicsBodies is used like this to extract obstacles and create the graph;

let obstacles = SKNode.obstaclesFromNodePhysicsBodies(self.children)
graph = GKObstacleGraph(obstacles: obstacles, bufferRadius: 0.0)

For a SKTilemapNode representing a cartesian grid, the GKGridGraph seems the likely choice.

Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
  • Thanks Mark. I tried using obstaclesFromNodePhysicsBodies function but it ended up giving me the same results, which makes sense, because each wall tile has its own physicsbody, and there are hundreds of them on the map. I thought about GKGridGraph but not sure if it is a good fit, because the entities in my game move freely on the map, it's not a grid based game. Can i still use GKGridGraph for pathfinding even when the game is not grid based? – Bob Dec 02 '16 at 15:51
  • One more thing. I intend to use GKAgent to move enemy entities. somehting like: GKGoal(toFollowPath: path, maxPredictionTime: 1.0, forward: true). Therefore the path must contain GKGraphNodes, not GKGridGraphNodes. – Bob Dec 02 '16 at 17:01
  • The `GKGridGraphNode` inherits from `GKGraphNode` so no problem using it with `GKGoal`. – Mark Brownsword Dec 02 '16 at 17:33
  • Since your entities move freely `GKObstacleGraph` might be the right choice then. I would start small, with just a few obstacles and prove that it works before proceeding. – Mark Brownsword Dec 02 '16 at 17:52
  • Also, I would look at the obstacle layer, it seems from your code example that every tile is an obstacle because there is no nil checking. I would expect an obstacle layer to have a lot of empty tiles that would become the paths that the entities move through. – Mark Brownsword Dec 02 '16 at 18:05
  • Thanks Mark. I found a solution to the "problem". I wrote a function to loop through rows and columns to pick up each wall tile and group them together and turn them into large blocks. (i.e 20 wall tiles that connect to each other would turn into 1 big wall block). This has effectively reduced the number of obstacles and the result looks good. – Bob Dec 05 '16 at 15:34
  • 1
    @Bob Could you please share your solution(algorithm) of grouping multiple tiles into one large block? – Pavel Mar 16 '17 at 05:09