nata11
nata11

Reputation: 119

SKSpriteKitNode's rotation does not match GKAgent2D's rotation

Hello I am trying to understand basic gameplay kit. But seem to run into trouble with matching the rotation of my agent to that of the spritekitnode on which it is supposed to act. I am using the spaceship which comes with the new game template in iOS. And I am trying to induce a basic wander behavior on the spaceship. The spaceship does wander but it almost looks like the spaceship is doing this weird side stepping dance because its rotation is not matching the agents rotation. Code is below.

class Entity : GKEntity {

let sprite: SKSpriteNode

override init() {
    let texture = SKTexture(imageNamed: "Spaceship")
    sprite = SKSpriteNode(texture: texture, color: UIColor.white, size: texture.size())
    sprite.position = CGPoint(x: 0, y: 0)
    sprite.zRotation = CGFloat(-.pi / 2.0)
    sprite.zPosition = 10
    sprite.setScale(1.0 / 8.0)
    super.init()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class GameScene: SKScene , GKAgentDelegate {

var entities = [GKEntity]()
var graphs = [String : GKGraph]()
var anEntity = Entity()

private var lastUpdateTime : TimeInterval = 0

override func didMove(to view: SKView) {

    self.lastUpdateTime = 0

    let wanderGoal = GKGoal(toWander: 10)
    let behavior = GKBehavior(goal: wanderGoal, weight: 100)
    let agent = GKAgent2D()
    agent.radius = 40.0
    agent.position = vector_float2(Float(anEntity.sprite.position.x), Float(anEntity.sprite.position.y))
    agent.rotation = Float(-.pi / 2.0)
    print (agent.rotation)
    agent.delegate = self
    agent.maxSpeed = 100
    agent.maxAcceleration = 50
    agent.behavior = behavior
    anEntity.addComponent(agent)
    addChild(anEntity.sprite)
    entities.append(anEntity)

}




override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {

}


override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    // Initialize _lastUpdateTime if it has not already been
    if (self.lastUpdateTime == 0) {
        self.lastUpdateTime = currentTime
    }

    // Calculate time since last update
    let dt = currentTime - self.lastUpdateTime

    // Update entities
    for entity in self.entities {
        entity.update(deltaTime: dt)
    }

    self.lastUpdateTime = currentTime
}

func agentWillUpdate(_ agent: GKAgent) {

}

func agentDidUpdate(_ agent: GKAgent) {

    guard let agent2d = agent as? GKAgent2D else {
        return
    }

    anEntity.sprite.position = CGPoint(x: CGFloat(agent2d.position.x), y: CGFloat(agent2d.position.y))
    //print (position)

    anEntity.sprite.zRotation = CGFloat(agent2d.rotation)
}
}

Upvotes: 1

Views: 155

Answers (1)

user3647542
user3647542

Reputation: 21

You can either rotate the bitmap of sprite (the png) 90deg to the right. Or you can write in the func agentDidUpdate(_ agent: GKAgent)

anEntity.sprite.zRotation = CGFloat(agent2d.rotation) - 0.5 * CGFloat.pi

Upvotes: 1

Related Questions