I have a simple breakout game which consists of a node being the ball and some other objects. The problem is when the ball bounces against the wall at a very small angle, it doesn't bounce back, it just slides against the wall indefinitely.
Manually calculating the angle after the contact could solve the problem, though it would be nice to use the physics provided by SpriteKit
.
Here is the relevant code:
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
}