How to add a border with GKPolygonObstacle?

I was playing with the AgentCatalog Sample code. I added a border to AAPLFleeScene.


First I wrote the method based on what apple did for Circular Obstacles.


- (GKObstacle *)addRectObstacleAtPoint:(CGRect)rect {
     CGFloat x = rect.origin.x;
     CGFloat y = rect.origin.y;
     CGFloat w = rect.size.width;
     CGFloat h = rect.size.height; 

     SKShapeNode *rectNode = [SKShapeNode shapeNodeWithRect:rect];
       rectNode.lineWidth = 2.5;
       rectNode.fillColor = [SKColor grayColor];
       rectNode.strokeColor = [SKColor redColor];
       rectNode.zPosition = 1;

       [self addChild:rectNode];


     vector_float2 f[] = {
            (vector_float2){x, y},
            (vector_float2){x+w, y},      
            (vector_float2){x+w, y+h},
            (vector_float2){x, y+h}
       };

     GKPolygonObstacle *obstacle = [GKPolygonObstacle obstacleWithPoints:f count:4];

     return obstacle;
}

//The circular obstacles added by the method below works - It is by apple
- (GKObstacle *)addObstacleAtPoint:(CGPoint)point {
     SKShapeNode *circleShape = [SKShapeNode shapeNodeWithCircleOfRadius:AAPLDefaultAgentRadius];
       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:AAPLDefaultAgentRadius];
       obstacle.position = (vector_float2){point.x, point.y};

     return obstacle;
}


Then I wrote a method which uses it


-(void)addObstacles{
     CGFloat w = self.frame.size.width;
     CGFloat h = self.frame.size.height;
     CGFloat borderWidth = 50;  

     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)],


            [self addRectObstacleAtPoint:CGRectMake(0, 0, borderWidth, h)],
            [self addRectObstacleAtPoint:CGRectMake(0, h-borderWidth, w, borderWidth)],
            [self addRectObstacleAtPoint:CGRectMake(w-borderWidth, 0, borderWidth, h)],
            [self addRectObstacleAtPoint:CGRectMake(0, 0, w, borderWidth)]
       ];

    [self.enemy.agent.behavior setWeight:100 forGoal:[GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:1]];
}



Then I call that in the method above in didMoveToView in the AAPLFleeScene.


- (void)didMoveToView:(nonnull SKView *)view {
    [super didMoveToView:view];
     //-----------apple's code
     //-----------apple's code
     //After Apple's code
    [self addObstacles];
}



The result is Screenshot


Any insights on what I am doing wrong? I believe the way I wrote the rectangle is wrong. Could you please help?

Replies

I am having the same issue - I cannot seem to get an agent to avoid anything other than the GKCircleObstacles that Apple uses in their demo. I even tried using the SpriteKit method of

NSArray<GKObstacle *> *obstacles = [SKNode obstaclesFromNodePhysicsBodies:nodeObstacles];

But I received the same results - the agent has NO idea what obstacles it should avoid this way. It seems that it might be a bug with Apple's GameplayKit Framework, along with the many other bugs as it has for being so "new." Hopefully someone can help this issue from Apple and resolve it sooner than later.


Big dissapointment that this functionality is not more straightforward, let alone working.


FYI: Here is a stackoverflow postthat I put up recently covering this same issue