mardi 5 mai 2015

Unable to set velocity of sprites correctly

I am making an asteriods-like app in iOS8. I have code that creates a laser shot and sets its velocity vector according to the facing of the ship. When the ship is oriented upwards on the screen, the code works correctly. When the ship is oriented downwards on the screen the sprite has no, or almost no, velocity along the y-axis. That is, the .dy component of physicsBody.velocity behaves as if it is near zero; the .dx component behaves correctly. LLDB reports the .dy component as what it should be, but the sprite moves incorrectly. Here is the relevant code:

func createLaser()->SKSpriteNode {
    let laserBeam = SKSpriteNode(imageNamed: "laserBeam")
    laserBeam.name = "laserBeam"
    laserBeam.position.x = shipNode.position.x
    laserBeam.position.y = shipNode.position.y
    laserBeam.physicsBody = SKPhysicsBody(circleOfRadius: 10.0)
    laserBeam.physicsBody?.dynamic = true
    laserBeam.physicsBody?.affectedByGravity = false
    laserBeam.zRotation = shipNode.zRotation
    laserBeam.physicsBody?.mass = 1.0
    laserBeam.physicsBody?.velocity.dx = kLaserSpeed * sin(shipNode.zRotation ) * -1 //TODO apply an impulse instead?
    laserBeam.physicsBody?.velocity.dy = kLaserSpeed * cos(shipNode.zRotation) * 1
    println("in create Laser ship rotation = \(shipNode.zRotation), dx = \(laserBeam.physicsBody?.velocity.dx), dy = \(laserBeam.physicsBody?.velocity.dy)")
    return laserBeam
}

Aucun commentaire:

Enregistrer un commentaire