ios - GameplayKit GKPolygonObstacle Not Working With GKGoal -
similarly question have posted here, realizing problem more trivial anticipated, works elements of gameplaykit
not others..
i have obstacle, sknode
, trying define gkpolygonobstacle
can used agent, gkagent2d
, obstacle avoid when moving in skscene
have set up.
i have looked apple's agentscatalog see how use gkobstacle
agent in gameplaykit
method:
goaltoavoidobstacles:(nonnull nsarray<gkobstacle *> *) maxpredictiontime:(nstimeinterval)
when use following code in own project create gkcircleobstacle
objects, find agent quite nicely navigates around , avoids these circular obstacles quite nicely depending on weight give (importance level).
here code using:
nsarray<gkobstacle *> *obstacles2 = @[ [self addobstacleatpoint:cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame) + 150)], [self addobstacleatpoint:cgpointmake(cgrectgetmidx(self.frame) - 200, cgrectgetmidy(self.frame) - 150)], [self addobstacleatpoint:cgpointmake(cgrectgetmidx(self.frame) + 200, cgrectgetmidy(self.frame) - 150)], ]; enemy.avoidgoal = [gkgoal goaltoavoidobstacles:obstacles2 maxpredictiontime:1]; [enemy.agent.behavior setweight:100 forgoal:enemy.avoidgoal];
with following method creates , adds these obstacles: (this directly pulled apple's source code agentscatalog)
- (gkobstacle *)addobstacleatpoint:(cgpoint)point { skshapenode *circleshape = [skshapenode shapenodewithcircleofradius:50]; circleshape.linewidth = 2.5; circleshape.fillcolor = [skcolor graycolor]; circleshape.strokecolor = [skcolor redcolor]; circleshape.zposition = 1; circleshape.position = point; [self addchild:circleshape]; gkcircleobstacle *obstacle = [gkcircleobstacle obstaclewithradius:50]; obstacle.position = (vector_float2){point.x, point.y}; return obstacle; }
this works great enemy avoids these circles tries move changing position in scene.
issue
when attempt recreate gkgoal
behavior using gkpolygonobstacle
objects in place of gkcircleobstacle
objects, enemy agent cannot seem identify polygon obstacles obstacles avoid behavioral goal. here how attempting add these obstacles:
nsarray<gkobstacle *> *obstacles = [sknode obstaclesfromnodephysicsbodies:innermaparray]; // take array of gkpolygonobstacle objects , add // gkgoal of enemy obstacles avoid enemy.avoidgoal = [gkgoal goaltoavoidobstacles:obstacles maxpredictiontime:1]; [enemy.agent.behavior setweight:100 forgoal:enemy.avoidgoal];
what frustrating this, know array correctly creating nsarray
of gkpolygonobstacle
objects have used approach pathfinding (before decided painfully implement gameplaykit
, it's seek, avoid, , wander goals). here how using innermaparray:
- (nsarray *)findpathwithnode:(sknode *)nodetofindpath { nsarray *obstacles = [sknode obstaclesfromnodephysicsbodies:innermaparray]; gkobstaclegraph *graph = [gkobstaclegraph graphwithobstacles:obstacles bufferradius:35.0f]; // set enemy , target gkgraphnode2d *target = [gkgraphnode2d nodewithpoint:vector2((float)character.position.x, (float)character.position.y)]; gkgraphnode2d *enemy = [gkgraphnode2d nodewithpoint:vector2((float)nodetofindpath.position.x, (float)nodetofindpath.position.y)]; [graph connectnodeusingobstacles:enemy]; [graph connectnodeusingobstacles:target]; /// create tracking path nsarray *pathpointsfound = [graph findpathfromnode:enemy tonode:target]; return pathpointsfound; }
this method quite nicely returns points @ efficient path should include go around obstacles telling enemy avoid when trying reach location.
- so real question becomes: why
gkgoal
acceptgkcircleobstacle
objects notgkpolygonobstacle
objects?
if can me figure out how convert these sknode
objects acceptable obstacles register gkgoal
extremely appreciative. thank you.
this strange / simple answer, , i'm afraid have obstacles being avoided (more not @ least..) enemy.
- giving
gkgoal
avoiding obstacles lowmaxpredictiontime
(t < 10) seems make agent ignore obstacles completely. prediction time of 10 or greater + weight of 100+ seems cause correct behavior.
Comments
Post a Comment