gpt4 book ai didi

ios - 来自 SKPhysicsBody applyImpulse 的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 08:50:11 26 4
gpt4 key购买 nike

所以我有几个球从天上掉下来,碰到桨。

桨适用

ball.physicsBody?.applyImpulse(CGVectorMake(0, 200))

到球。

10 次中有 9 次,球弹回屏幕的 1/4。

10 次中有 1 次,球变得 super 疯狂并且飞得好像 applyImpulse 应该是 (0, 1000)~

还有!当球从第一次反弹返回并再次获得 .applyImpulse 时,它​​们会一直飞到顶部。远高于第一个 applyImpulse。 (发生在所有球上,第一次反弹:1/4 屏幕。第二次反弹:3.9/4 屏幕。)

这是为什么?

我希望球保持一致,我希望每次弹跳都具有相同的力量。

    ball.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ball.size.width, height: ball.size.height))
ball.physicsBody?.categoryBitMask = PhysicsCategory.ball.rawValue
ball.physicsBody?.contactTestBitMask = PhysicsCategory.bar.rawValue | PhysicsCategory.boss.rawValue
ball.physicsBody?.collisionBitMask = PhysicsCategory.noCollision.rawValue
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.dynamic = true

func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
let secondBody : SKPhysicsBody = (contact.bodyA.categoryBitMask >= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB

if (firstBody.categoryBitMask & PhysicsCategory.bar.rawValue == PhysicsCategory.bar.rawValue &&
secondBody.categoryBitMask & PhysicsCategory.ball.rawValue == PhysicsCategory.ball.rawValue) {
CollisionWithBar(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)

}

}

func CollisionWithBar(bar: SKSpriteNode, ball: SKSpriteNode) {
ball.physicsBody?.applyImpulse(CGVectorMake(0, 200))
NSLog("Ball hit Bar")

}

最佳答案

在输入 ball.physicsBody?.applyImpulse(CGVectorMake(0, 200)) 之前,尝试输入 ball.physicsBody?.velocity = CGVectorMake(0,0)这将重置球的速度,以便(希望如此)它跳回相同的高度。

关于ios - 来自 SKPhysicsBody applyImpulse 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34442330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com