GKGoal Avoid Boundary Obstacles
Asked Answered
S

0

8

I am seeming to have difficulty keeping my sprite nodes inside a map boundary that I have set up in the following way:

I have an SKNode *enemy that moves around an SKScene by goals and behaviors courtesy of GameplayKit from iOS 9. Currently the node wanders, and avoids obstacles that are defined GKPolygonObstacle objects. I have my bitmask set up so that any obstacle is deemd category wall, which the node is told to collide with (AKA disallow passing through).

In my didBeginContact:(SKPhysicsContact *)contact I am handling these collisions. All is working exactly as planned and there are no issues when manually moving this enemy.

However, the problem begins when I have the enemy wander around the scene through an SKGoal *wanderGoal set up as

    // Low --> 0.5     Lowest --> 0.25
    enemy.wanderGoal = [GKGoal goalToWander:low];
    [enemy.agent.behavior setWeight:lowest forGoal:enemy.wanderGoal];

    // Add obstacles to avoid for each of the inner map nodes
    // that act as impassible areas for the enemy to pass through.
    NSArray *obstacles = [SKNode obstaclesFromNodePhysicsBodies:innerMapArray];

    /** This goal does not change --> enemies will ALWAYS
     avoid the obstacles in the level map. This should always 
     be set to the highest priority of the enemy goals
     */
    enemy.avoidGoal = [GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:10];
    // Highest --> 250
    [enemy.agent.behavior setWeight:highest forGoal:enemy.avoidGoal];

The enemy moves randomly as expected, and seems to avoid the obstacles most of the time...yet on the occasion the enemy does in fact pass through the map barrier. Let me expand on this:

The map barrier is essentially a rectangle, and I have set up 2 nodes that act as a shell that represent the left half and the right half of this shape. These nodes have a thickness of 100 points so they are not simply CGPath refs but rather shapes. They act as these two brackets act: [] (my halves are flush touching at the middle).

My question is such:

  • How can I prevent the enemy from passing through these obstacles with using either physics with the bitmask categories, or by the goal behavior? (Or both together)

I understand that while a GKGoal is a suggested behavior it does not guarantee this goal to be reached (but it can be ~95% sure it wont happen as Apple's AgentsCatalog shows). But, what I don't understand is why the enemy does not simply "slide" along the edge of the boundary as the goal tells it to (try to) "pass" through such boundary.

This "sliding" behavior does happen when I manually move the enemy around the scene, and as I attempt to move it passed a boundary deemed an obstacle, it slides towards the direction that I am having it move (just as any video game does that has map boundaries).

SO

If someone can help me prevent this from happening / explain how to approach preventing the enemy from ever passing through these (supposedly) "impassible" areas, I would be most appreciative.

FYI: If you are unclear with what I am describing / how I have created any objects, please ask and I will provide more info to help.

Soupandfish answered 27/7, 2016 at 5:25 Comment(2)
I thought this was a well-written question that deserves an answer.Meli
Not sure how much clearer I could have been here.. maybe nobody knows a fix. Good thing the project was completed in other ways. Thanks everyone for the helpful help! Glad I spent 30 minutes writing this.. in order to avoid the snobby "Show code" or "Too vague" junk.Soupandfish

© 2022 - 2024 — McMap. All rights reserved.