ios - Obstacles With GameplayKit -
here question:
- how can convert
skspritenode
array array ofgkobstacles
agent can appropriately avoid these obstacles usinggoaltoavoidobstacles:(nonnull nsarray<gkobstacle *> *) maxpredictiontime:(nstimeinterval)
/?
it seems way in creating gkobstacle
array not allowing gkgoal
identify these obstacles correctly, no matter weight give goal.
i have several sknode
s added skscene
chapterscene. each of these nodes added array storing called obstaclesarray. have set test in following way works quite well:
working obstacles
- (void)didmovetoview:(nonnull skview *)view { [super didmovetoview:view]; // add 3 obstacles in triangle formation around center of scene. nsarray<gkobstacle *> *obstacles = @[ [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)], ]; // player agent follows tracking agent. self.player = [[oplayer alloc] initwithscene:self radius:50 position:cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame))]; self.player.agent.behavior = [[gkbehavior alloc] init]; [self.agentsystem addcomponent:self.player.agent]; // create seek goal, add behavior in -setseeking:. self.seekgoal = [gkgoal goaltoseekagent:self.character]; // add avoid-obstacles goal high weight keep agent overlapping obstacles. [self.player.agent.behavior setweight:100 forgoal:[gkgoal goaltoavoidobstacles:obstacles maxpredictiontime:1]]; } - (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; }
while works fine, issue having can not behavior identify sknode
bodies array have obstacles. can these manually created gkcircleobstacle
items register valid obstacles agent avoids. reason issue because relying on many obstacles not in fact simple circles, polygon structures complex, more straightforward. nevertheless, attempting following agent did not seem avoid of obstacles of sknode
way:
not working obstacles
nsarray *obstacles = [sknode obstaclesfromnodephysicsbodies:obstaclesarray]; /* other code seen above */ // add avoid-obstacles goal high weight keep agent overlapping obstacles. [self.player.agent.behavior setweight:100 forgoal:[gkgoal goaltoavoidobstacles:obstacles maxpredictiontime:1]];
unfortunately, not seem work no matter how high set weight to, agent never responds avoiding obstacles. here log of array passing it:
2016-07-24 22:32:24.907 <game>[1516:475581] obstacles: ( "<gkpolygonobstacle: 0x14d20d70>", "<gkpolygonobstacle: 0x14d20e10>", "<gkpolygonobstacle: 0x14d20ba0>", "<gkpolygonobstacle: 0x14d20bb0>", "<gkpolygonobstacle: 0x14d20bc0>", "<gkpolygonobstacle: 0x14d20a60>", "<gkpolygonobstacle: 0x14d208b0>", "<gkpolygonobstacle: 0x14d207d0>", "<gkpolygonobstacle: 0x14d20a70>" )
each of have been , appropriately initialized , added scene. each of these elements in fact responsive spritekit
didbegincontact:(skphysicscontact *)contact
method, seems nodes handing boundaries should having.
if knows doing wrong or better way turn each of these 'obstacle nodes' gkobstacle
's appreciative. in advance!
update: here node physics body testing:
skspritenode *innermap1 = [skspritenode spritenodewithimagenamed:@"test"]; innermap1.physicsbody = [skphysicsbody bodywithtexture:[sktexture texturewithimagenamed:@"test"] size:cgsizemake(innermap1.frame.size.width*0.92, innermap1.frame.size.height*0.92)]; innermap1.position = cgpointmake(-220, -140); innermap1.physicsbody.dynamic = no; [chapterskspritenodes addobject:innermap1];
here body looks skview.showsphysics = yes;
:
so know physics body need be. when attempt create gkobstacle
body however, not seem register obstacle when being assigned in avoidobstacles goal.
this sounds bug. should check return array obstaclesfromnodephysicsbodies , make sure getting obstacles expect. suspect not.
you can check relevant vertices on obstacles such:
make sure expect.
Comments
Post a Comment