3

So I'm creating a game where I want to add some GKGoal for my GKAgent behaviour.

So after hours of fighting, I've downloaded next project this time is from Apple Agents Catalog, and in Xcode 7.3 it works. I rewrite it to Swift and create basic GKAgent with GKGoal(toWander:) this is my code:

 class AAPLAgentNode: SKNode, GKAgentDelegate {
    init(withScene scene:SKScene ,radius: Float, position:CGPoint) {
        super.init()
        
        self.position = position
        self.zPosition = 10
        scene.addChild(self)
        
        agent = GKAgent2D()
        agent.radius = radius
        agent.position = vector2(Float(position.x), Float(position.y))
        agent.delegate = self
        agent.maxSpeed = 100
        agent.maxAcceleration = 50
        
        let circleShape = SKShapeNode(circleOfRadius: CGFloat(radius))
        circleShape.lineWidth = 2.5
        circleShape.fillColor = SKColor.gray
        circleShape.zPosition = 1
        self.addChild(circleShape)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func agentWillUpdate(_ agent: GKAgent) {
        
    }
    
    func agentDidUpdate(_ agent: GKAgent) {
        self.position = CGPoint(x: Double(self.agent.position.x), y: Double(self.agent.position.y))
        print("aaa == \(self.position)")
    }
    
    var agent: GKAgent2D!
}

When I add to scene

     let wanderer = AAPLAgentNode(withScene: self, radius: 100, position: CGPoint(x: 0, y: 0))
    
    wanderer.agent.behavior = GKBehavior(goal: GKGoal(toWander: 10), weight: 100)

    agentSystem.addComponent(wanderer.agent)

without behaviour position is static but when I add it, position goes crazy and in each iteration of update, values are like

  • position == (-10051366.0, 251672512.0)
  • position == (1368370.0, 259904576.0)
  • position == (-131583.0, 264841120.0)

Is it just an Xcode 8 beta bug, or am I doing something wrong. I spend a lot of hours trying to work it out. Thanks:)

Community
  • 1
  • 1
Michal Rogowski
  • 431
  • 3
  • 18
  • I am getting a similar behaviour (no pun intended) with Swift Playgrounds 1.1.2 on iPad with `x` position being ok, but `y` always getting meaningless huge values. There is definitely a bug in `GKGoal` implementation. – Max Desiatov Dec 24 '16 at 19:07
  • I'm seeing the same behaviour in Xcode 9. As soon as you set a behaviour on a GKAgent2D that's a component of an SKNode, the agent's delegate method reports insane positions for the agent. This effectively renders it makes GKGoal, GKBehaviour, GKAgent inoperable in SpriteKit. Interestingly though, I don't see this behaviour with GKAgent3D attached to SCNNodes in SceneKit. Terrible that a bug as bad as this has made it through two versions of Xcode. – OliverD Sep 26 '17 at 19:32
  • The same code works on iOS 11 though. I just see the erratic behaviour on macOS Sierra (haven't updated to High Sierra yet). If it's been fixed in iOS 11, perhaps it's fixed in High Sierra? – OliverD Sep 27 '17 at 19:39

2 Answers2

2

I know this is very old, but are you calling with a very high deltaTime on your first update?

I was calling with the system time as my first update call to the agent behaviour, causing a massive jump at the first timestep

Joe Bentley
  • 296
  • 2
  • 13
  • This was the cause. The initial value for the previousTime was 0. This was causing a massive initial value. Changing the previousTime to an optional and using the current time when nil did the trick. – joshd Jun 22 '19 at 08:37
1

It's been super frustrating trying to grock this Agent, Behaviour stuff... I was having this issue in Xcode 9.0 while trying out toSeekAgent when I was adding my nodes and agents in the didMove() method of my SKScene but after "init-ing" them from touchesBegan() the extreme position issue went away.

apolwla
  • 36
  • 1
  • 3
  • This is what happened to me too, so I tried your solution and it works. I have no idea why it doesn't work in didMove() but works in touchesBegan(). – thecanteen Nov 15 '17 at 12:38