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.

4

1 回答 1

1

让你的游戏像这样分层

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

然后SKPhysicsBody使用内部框大小的边缘循环矩形附加一个innerbox SKNode并使其成为墙类别,这将使您的所有节点保持在内部,前提是您的节点不会以疯狂的速度移动而破坏引擎。

您将添加 1 个额外节点而不是 2 个(从技术上讲,您可以将 bottomshape 和 topshape 设为 1 个节点,使其添加 0 个节点),但所有处理都将在内部节点内完成,因此不会增加太多开销。

于 2016-03-03T16:22:17.627 回答