SpriteKit物理模拟反弹错误

时间:2017-08-09 17:02:18

标签: swift sprite-kit simulation physics breakout

我有一个简单的突破游戏,其中包括一个节点作为球和一些其他对象。问题是当球以非常小的角度反弹到墙上时,它不会反弹,只会无限期地滑向墙壁。

在接触后手动计算角度可以解决问题,尽管使用SpriteKit提供的物理效果会很好。

以下是相关代码:

override func didMove(to view: SKView) {
    // screen border setup
    let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    border.friction = 0
    border.allowsRotation = true
    border.friction = 0
    border.restitution = 1
    border.linearDamping = 0
    border.angularDamping = 0

    physicsBody = border
    physicsWorld.gravity = CGVector(dx: 0, dy: 0)
    physicsWorld.contactDelegate = self

    // bottom edge setup
    let rect = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: 2)
    let bottom = SKNode()
    bottom.physicsBody = SKPhysicsBody(edgeLoopFrom: rect)
    bottom.physicsBody!.categoryBitMask = BottomCategory
    addChild(bottom)

    // ball setup
    let ball = childNode(withName: "ball") as! SKSpriteNode
    ball.physicsBody?.applyImpulse(CGVector(dx: 30, dy: -30))
    ball.physicsBody?.allowsRotation = true
    ball.physicsBody?.friction = 0
    ball.physicsBody?.restitution = 1
    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.angularDamping = 0
    ball.physicsBody?.contactTestBitMask = BlockCategory | BottomCategory
    ball.physicsBody?.categoryBitMask = BallCategory

    // paddle setup
    let paddle = childNode(withName: "paddle") as! SKSpriteNode
    paddle.physicsBody!.categoryBitMask = PaddleCategory
}

0 个答案:

没有答案