物理问题:球弹跳得太高

时间:2015-09-15 00:19:57

标签: swift sprite-kit skphysicsbody

我正在开展一场比赛,在这场比赛中,球会反弹并击中一个平台,然后向右反弹。理想情况下,球应该弹回到它开始时的高度。然而,在我的比赛中,球慢慢地越来越弹跳。我查看了文档并尝试更改所有物理属性,但似乎没有任何效果。

有什么建议吗?

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    let ballCategory:UInt32 = 0x1 << 0;
    let circleCategory:UInt32 = 0x1 << 1;
    let platformCategory:UInt32 = 0x1 << 2;

    var ball = SKShapeNode(circleOfRadius: 20.0)
    var circle = SKShapeNode(circleOfRadius: 200.0)
    var platform = SKShapeNode(rectOfSize: CGSizeMake(10, 1))

    var circleColor = 2
    var ballColor = 3

    var scoreLabel: SKLabelNode!
    var score = 0

    override func didMoveToView(view: SKView) {

        setUpLabels()

        self.physicsWorld.contactDelegate = self

        backgroundColor = (UIColor.whiteColor())
        ball.fillColor = SKColor.redColor()
        ball.strokeColor = SKColor.clearColor()
        ball.position = CGPoint(x: self.size.width/2, y: self.size.height/2+60)

        ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
        ball.physicsBody?.categoryBitMask = ballCategory
        ball.physicsBody?.collisionBitMask = platformCategory
        ball.physicsBody?.contactTestBitMask = platformCategory
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.restitution = 1.0
        ball.physicsBody?.affectedByGravity = true
        ball.physicsBody?.linearDamping = 0.0
        ball.physicsBody?.friction = 0.0


        self.addChild(ball)


        circle.fillColor = SKColor.clearColor()
        circle.strokeColor = SKColor.redColor()
        circle.lineWidth = 5.0
        circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)

        circle.physicsBody = SKPhysicsBody(edgeLoopFromPath: circle.path)
        circle.physicsBody?.categoryBitMask = circleCategory

        self.addChild(circle)


        platform.fillColor = SKColor.clearColor()
        platform.strokeColor = SKColor.clearColor()
        platform.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(10, 1))
        platform.position = CGPoint(x: self.size.width/2, y: circle.position.y/2)
        platform.physicsBody?.categoryBitMask = platformCategory
        platform.physicsBody?.dynamic = false
        platform.physicsBody?.friction = 0.0


        self.addChild(platform)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


        circleColor += 1

        if circleColor%3  == 1 {

            circle.strokeColor = UIColor.redColor()
        }

        if circleColor%3  == 2 {

            circle.strokeColor = UIColor.greenColor()
        }

        if circleColor%3  == 3 {

            circle.strokeColor = UIColor.yellowColor()
        }

        if circleColor%3  == 4 {

            circle.strokeColor = UIColor.greenColor()
        }

        if circleColor%3  == 5 {

            circle.strokeColor = UIColor.blueColor()
        }
        if circleColor%3  == 0 {

            circle.strokeColor = UIColor.yellowColor()
        }


    }


    func didBeginContact(contact: SKPhysicsContact) {

        if ball.fillColor == circle.strokeColor {
            score += 1
            scoreLabel.text = String("Score \(score)")
        }


        if ball.fillColor != circle.strokeColor {
            let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)

            let scene = SecondScene(size: self.scene!.size)
            scene.scaleMode = SKSceneScaleMode.AspectFill


            self.scene!.view!.presentScene(scene, transition: transition)
        }



        ballColor = Int(arc4random_uniform(10))


        if ballColor%3  == 1 {

            ball.fillColor = UIColor.redColor()
        }

        if ballColor%3  == 2 {

            ball.fillColor  = UIColor.greenColor()
        }

        if ballColor%3  == 3 {

            ball.fillColor  = UIColor.yellowColor()
        }

        if ballColor%3  == 4 {

            ball.fillColor  = UIColor.greenColor()
        }

        if ballColor%3  == 5 {

            ball.fillColor  = UIColor.blueColor()
        }
        if ballColor%3  == 0 {

            ball.fillColor  = UIColor.yellowColor()
        }


    }

    func setUpLabels () {

        scoreLabel = SKLabelNode(fontNamed: "Arial")
        scoreLabel.position = CGPoint(x: 60, y: self.size.height-30)
        scoreLabel.text = String("Score \(score)")
        scoreLabel.fontColor = UIColor.blackColor()
        scoreLabel.fontSize = 25
        self.addChild(scoreLabel)


    }

    override func update(currentTime: CFTimeInterval) {

    }
}

2 个答案:

答案 0 :(得分:4)

稍微减少class RunnableA,以控制物理身体的弹性。像这样:

restitution

答案 1 :(得分:1)

一般来说,解决下一个时间步骤的颂歌的数值微分方程求解器会将能量引入系统,这就是你看到你所看到的东西的原因。您可以通过引入阻尼力来缓解这种情况。在SpriteKit的情况下,这意味着改变恢复或线性阻尼因子。

有关发生漂移的详情,请参阅:https://en.wikipedia.org/wiki/Energy_drifthttp://www.cs.cmu.edu/~baraff/sigcourse/notesb.pdf