2

I've been searching for days about this new framework and trying to make usage of some of it's funcionalities, but... there're some things that are not fitting together for me, the demobot source code isn't helping at some points, I miss some kind of simple tutorial but here goes my main doubts:

let obstacles = scene["obstacle"]
polygonObstacles = SKNode.obstaclesFromNodePhysicsBodies(obstacles)
graph = GKObstacleGraph(obstacles: polygonObstacles, bufferRadius: 60.0)


func drawGraph() {
    for node in graph.nodes as! [GKGraphNode2D] {
        for destination in node.connectedNodes as! [GKGraphNode2D] {
            let points = [CGPoint(node.position), CGPoint(destination.position)]
            let shapeNode = SKShapeNode(points: UnsafeMutablePointer<CGPoint>(points), count: 2)
            shapeNode.strokeColor = SKColor(white: 1.0, alpha: 0.5)
            shapeNode.lineWidth = 5.0
            shapeNode.zPosition = 3
            scene.addChild(shapeNode)
        }
    }
}

So, when I try to draw this graph and see the connections, I get this: http://i.imgur.com/EZ3dx5v.jpg I find it really weird, anywhere I put my obstacles, even in low numbers, the left-corner portion of the screen have always more connections(the radius don't have influence on that)

I don't use GKComponents on my game, but I tried to run some GKAgents2D to hunt the player, like this:

func calculateBehaviorForAgents(){

  let mainCharacterPosition = float2(scene.mainCharacter.position)
  let mainCharacterGraphNode = GKGraphNode2D(point: mainCharacterPosition)
  graph.connectNodeUsingObstacles(mainCharacterGraphNode)
  for i in 0...monsters.count-1{
     let monster = monsters[i]
     let agent = agents[i]

     let behavior = GKBehavior()

     let monsterPosition = float2(monster.position)
     let monsterGraphNode = GKGraphNode2D(point: monsterPosition)
     graph.connectNodeUsingObstacles(monsterGraphNode)

     let pathNodes = graph.findPathFromNode(monsterGraphNode, toNode: mainCharacterGraphNode) as! [GKGraphNode2D]
     let path = GKPath(graphNodes: pathNodes, radius: 00.0)

     let followPathGoal = GKGoal(toFollowPath: path, maxPredictionTime: 1.0, forward: true)
     behavior.setWeight(1.0, forGoal: followPathGoal)

     let stayOnPathGoal = GKGoal(toStayOnPath: path, maxPredictionTime: 1.0)
     behavior.setWeight(1.0, forGoal: stayOnPathGoal)

     agent.behavior = behavior

     graph.removeNodes([monsterGraphNode])
  }
  graph.removeNodes([mainCharacterGraphNode])

}

Now when I call the updateWithDeltaTime method, his delegate methods: func agentWillUpdate(agent: GKAgent){} func agentDidUpdate(agent: GKAgent){} give me unexpected values for the agents, it's position doesn't make any sense, with giant numbers that leads to outside of the battlefield

But I saw that the his velocity vector were making sense, so I matched it to my monster and updated the agent to the monster's position

func updateWithDeltaTime(currentTime : CFTimeInterval){

  for i in 0...monsters.count-1{

  let agent = agents[i]
  let monster = monsters[i]

  monster.physicsBody?.velocity = CGVectorMake(CGFloat(agent.velocity.x), CGFloat(agent.velocity.y))

  agent.updateWithDeltaTime(currentTime)
  agent.position = float2(monster.position)
  monster.gameSceneUpdate(currentTime)

}

Now I was getting some results, but it's far away from what I want: The monsters are not following the character to the edges or the right-top portion of screen, I remove their points from the graph but after make a path for them to follow (the image doesn't have this points, but they exists). Apparently because there was no path leading to there, remember the image?

The question is: how to make this agent system to work?

Maybe I'm totally wrong at the usage of agents, goals and even the graphs! I read the documentation but I still can't make it right And more... At first, the monster were not avoid obstacles, even with GKGoals like "avoidObstacles", passing the same PolygonObstacles, but when I change

graph.connectNodeUsingObstacles(mainCharacterGraphNode)
graph.connectNodeUsingObstacles(monsterGraphNode)

to

graph.connectNodeUsingObstacles(mainCharacterGraphNode, ignoringObstacles: polygonObstacles)
graph.connectNodeUsingObstacles(monsterGraphNode, ignoringObstacles: polygonObstacles)

it worked! o.O

I really need some help, thank you all :D!

4

0 回答 0