0 Replies
      Latest reply on Sep 28, 2015 3:19 PM by dev0400
      dev0400 Level 2 Level 2 (25 points)
        NSLog("Hello World, I'm stuck!")
        

        Hello,

        I'm attempting to set up a GKAgent for path finding inside my game. I've been rewatching the WWDC video on this topic and I'm absolutely stumped!

        So, if anyone can give advice on how to get this working I would greatly appreciate it!

         

        For my obstacles, I have a .sks file SKSpriteNodes that have physic bodies. I then use the following to get the array of obstacles:

        SKScene *obstaclesAndBackgroundScene = [SKScene nodeWithFileNamed:self.level.sceneName];
        
            NSMutableArray *obstacles = [[NSMutableArray alloc] init];
        
            for (SKNode *aNode in obstaclesAndBackgroundScene.children) {
                [aNode removeFromParent];
                [scene addChild:aNode];
          
                if ([aNode isKindOfClass:[SKSpriteNode class]]) {
                    if (aNode.physicsBody) {
                        aNode.physicsBody.usesPreciseCollisionDetection = YES;
                        [obstacles addObject:aNode];
                    }
                }
            }
        
            NSArray *obstaclesObjects = [SKNode obstaclesFromNodeBounds:obstacles];
        
            scene.obstacles = obstaclesObjects;
        

         

        //Main scene

        self.obstacleGraph = [GKObstacleGraph graphWithObstacles:self.obstacles bufferRadius:followBufferRadius]
        

         

        //In a loop that I set up each player:

          
        [self.obstacleGraph connectNodeUsingObstacles:aPlayer.car.carGraphNode2d];
        
        
        

         

        Here's my setup method:

        -(void)setUpFollowGoalWithObstacleGraph:(GKObstacleGraph *)obstacleGraph playerToFollow:(JDPlayer *)player {
        
            if (self.car.isHuman) {
                return;
            }
        
        
            NSArray *nodesForPathToFollow = [obstacleGraph findPathFromNode:self.car.carGraphNode2d toNode:player.car.carGraphNode2d];
        
            if (nodesForPathToFollow.count >= 2) {
                GKPath *pathToFollow = [GKPath pathWithGraphNodes:nodesForPathToFollow radius:20.0];
        
        
        
                self.car.followGoal = [GKGoal goalToFollowPath:pathToFollow maxPredictionTime:20.0 forward:YES];
            
                self.car.stayOnPathGoal = [GKGoal goalToStayOnPath:pathToFollow maxPredictionTime:20.0];
        
                [self.car.agent.behavior setWeight:2.0 forGoal:self.car.followGoal];
        
        
                [self.car.agent.behavior setWeight:2.0 forGoal:self.car.stayOnPathGoal];
        
        
        
            }
        
        }
        

         

        //Delegate on each player

        -(void)agentDidUpdate:(GKAgent2D *)agent {
           
            if (!self.car.isHuman) {
               
                self.car.physicsBody.velocity = CGVectorMake(agent.velocity.x * 300 , agent.velocity.y * 300);
               
            }
        }
        
        -(void)agentWillUpdate:(GKAgent2D *)agent {
            agent.rotation = (float)(self.car.zRotation);
            agent.position = (vector_float2){self.car.position.x, self.car.position.y};
        }
        

         

         

        Thank you!