1

I am using GameplayKit to create paths around obstacles in a given map boundary (lets say a rectangle 1000x1000).

I know that you can make certain nodes avoid "obstacles" when pathfinding, which I am using quite nicely. What I am curious about however is such:

  • Is there a way to use this same logic and count anything not in the map boundary as an "obstacle"?

A work around would be to create 2 SKNodes and fit them together to create an inner "hole" which becomes 1000x1000, but I am trying to avoid unnecessary addition of nodes if there is a better way. Below I am showing what I could do.

Ideally I want to make the red and black area treated as an obstacle so that all paths remain inside the main square.

enter image description here

UPDATE: I am using GameplayKit as I have already said, and the pathfinding algorithm can not count regions that are NOT included in a given physics body as an obstacle. It can only count obstacles to be closed polygons that lie within a region. Creating the 2 nodes as shown above works because the pathfinding will now not be able to create any points that lie outside the green rect.

Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42

1 Answers1

1

Just have your game layered like this

Scene
  SKNode topshape
  SKNode bottomshape
  SKNode innerbox <-- This is the size of your inner square
    SKNode  gamenodes <-- Place all inner nodes here
    ...

Then attach a SKPhysicsBody using an edge loop rectangle the size of innerbox, to the innerbox SKNode and make it a wall category, this will keep all your nodes inside, providing your nodes do not move at insane speeds breaking the engine.

You would be adding 1 additional node instead of 2 (Technically you could make bottomshape and topshape 1 node, making it 0 nodes added), but all processing would get done within the inner node, so not much overhead gets added.

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • how would u make the top and bottom one node? physics body with an inner hole?..didnt think you could do that – Will Von Ullrich Mar 03 '16 at 21:42
  • from what you are providing, you could make it 1 node with a texture of what you are displaying, then the innernode would be your actual game area – Knight0fDragon Mar 03 '16 at 21:44
  • correct. that is how i currently have it. I need this outer region to be a physical body rather than a defined region to allow a pathfinding algorithm to identify it as an "obstacle" rather than using a texture to define a perimeter. Figre 6-1 https://developer.apple.com/library/ios/documentation/General/Conceptual/GameplayKit_Guide/Pathfinding.html Sadly they have no way of making the non-node region and obstacle – Will Von Ullrich Mar 03 '16 at 21:48
  • why do you need the outer region to be a physics body, is this going to be something other than a square? – Knight0fDragon Mar 03 '16 at 21:49
  • yes. some polygon (I edited my Q a few minutes back) – Will Von Ullrich Mar 03 '16 at 21:50
  • ok, well if you want it to be some polygon, then your innernode becomes an SKShapeNode instead of a rect, with the shape being your polygon – Knight0fDragon Mar 03 '16 at 21:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105312/discussion-between-will-von-ullrich-and-knight0fdragon). – Will Von Ullrich Mar 03 '16 at 21:53