我有一个简单的SpriteKit应用程序,带有墙壁和球.两者都设置了SKPhysicsBody.
当我向一个方向施加力时,我希望球在碰撞时以相同的角度在墙壁上反射,但方向相反.
但有时我看到角度很奇怪.我使用了所有的physicsBody属性,但是无法修复它.有时第一次反射看起来很好,但是第三次或第六次反射,有时第一次反射是错误的角度.
我从不同的帖子中读到,人们有点自我计算的“正确方向”.但我无法想象SpriteKits的物理引擎无法做到这一点.
检查我的附图,了解我的意思.有人可以帮忙吗?我不想开始使用Box2d for Swift,因为看起来我很难融入Swift.
这是我的GameScene.swift文件中的physicsWorld init,其中所有元素都添加到:
self.physicsWorld.gravity = CGVectorMake(0,0)
添加我的所有代码太多了,所以我添加了重要的部分.希望它足以分析.所有元素,如球,墙都是SKSpriteNode
这是球的physicsBody代码:
self.physicsBody = SKPhysicsBody(circleOfRadius: Constants.Config.playersize+self.node.lineWidth) self.physicsBody?.restitution = 1 self.physicsBody?.friction = 0 self.physicsBody?.linearDamping = 1 self.physicsBody?.allowsRotation = false self.physicsBody?.categoryBitMask = Constants.Config.PhysicsCategory.Player self.physicsBody?.collisionBitMask = Constants.Config.PhysicsCategory.Wall | Constants.Config.PhysicsCategory.Enemy self.physicsBody?.contactTestBitMask = Constants.Config.PhysicsCategory.Wall | Constants.Config.PhysicsCategory.Enemy
这是墙的物理体:
el = SKSpriteNode(color: UIColor.blueColor(),size: CGSize(width: Constants.Config.wallsize,height: Constants.Config.wallsize)) el.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: Constants.Config.wallsize,height: Constants.Config.wallsize)) el.physicsBody?.dynamic = false el.physicsBody?.categoryBitMask = Constants.Config.PhysicsCategory.Wall el.physicsBody?.collisionBitMask = Constants.Config.PhysicsCategory.Player el.physicsBody?.contactTestBitMask = Constants.Config.PhysicsCategory.Player
最后,我只是在球物理实体上调用applyImpulse函数,使其在物理模拟中移动.
另请检查我附加的第二张图片/ gif.它使用简单的skphysics应用程序显示边缘碰撞问题,没有任何特殊参数化.只是一个带有球的矩形,一个矢量用作冲动.
使用appzYourLife的解决方案继承我完整的非工作代码.
class GameScene: SKScene { private var ball:SKShapeNode! override func didMoveToView(view: SKView) { let screen = UIScreen.mainScreen().bounds let p = UIBezierPath() p.moveToPoint(CGPointMake(0,0)) p.addLineToPoint(CGPointMake(screen.width,screen.height)) p.addLineToPoint(CGPointMake(0,screen.height)) p.closePath() let shape = SKShapeNode(path: p.CGPath) shape.physicsBody = SKPhysicsBody(edgeLoopFromPath: p.CGPath) shape.physicsBody?.affectedByGravity = false shape.physicsBody?.dynamic = false shape.strokeColor = UIColor.blackColor() self.addChild(shape) ball = SKShapeNode(circleOfRadius: 17) ball.name = "player" ball.position = CGPoint(x: 20,y: 20) ball.fillColor = UIColor.yellowColor() ball.physicsBody = SKPhysicsBody(circleOfRadius: 17) ball.physicsBody?.angularDamping = 0 ball.physicsBody?.linearDamping = 0 ball.physicsBody?.restitution = 1 ball.physicsBody?.friction = 0 ball.physicsBody?.allowsRotation = false self.addChild(ball) self.ball.physicsBody?.applyImpulse(CGVector(dx: 2.4,dy: 9.7)) }
我刚刚意识到我的球精灵的质量和密度是零.为什么?即使设置它也保持为零.
解决方法
Since i cannot imagine this is some unknown bug in SpriteKits physics engine,i very hope someone can help me here as this is a full stopper for me.
它是SpriteKits物理引擎中的(已知)错误.但有时只是玩这些值会使这个错误消失 – 例如,每当它撞到墙壁时,尝试改变球的速度(即使是小值).
此处列出了另一种解决方案
SpriteKit physics in Swift – Ball slides against wall instead of reflecting
(与你的问题相同,已经从2014年开始,仍未解决..)