SpriteKit brick – Solve the ball infinite cycle movement

Problem description: When the brick is round and right triangle, the small bounce will have multiple possible directions, and the infinite trajectory will appear as shown below:

Solutions for infinite horizontal motion:

if abs(ball.physicsBody? .velocity.dy)<10{
    let oy:CGFloat=ball.position.y>0? -30:30
    let dx:CGFloat=sqrt(speed*speed-oy*oy) ball.physicsBody? .velocity=CGVector(dx:dx,dy:oy)
}
Copy the code

Infinite vertical motion solution: When the ball collides with the baffle, determine whether the current vector dx of the ball is less than 10. If the ball is on the right, it runs to the left, and vice versa.

if abs(ball.physicsBody? .velocity.dx)<10{
    let ox:CGFloat=ball.position.x>0? -20:20
    let dy:CGFloat=sqrt(speed*speed-ox*ox) ball.physicsBody? .velocity=CGVector(dx:dx,dy:oy)
}
Copy the code