Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node is slowing down in Sprite Kit

I'm trying to create an arcade game, where a ball moves at a constant speed and is unaffected by gravity or friction. So I created the ball as an SKShapeNode and set its linearDamping and friction to 0. I also set the game scene to have no gravity. But when playing, if the ball hits another shape node (a circle) at a low angle, it can slow down. The ball's restitution is 1, and allowsRotation is false.

I am keeping the ball moving by applying one impulse at the beginning of the game, that is a random direction.

like image 373
brimstone Avatar asked Aug 01 '26 15:08

brimstone


2 Answers

This might not be the most ideal fix but you could set the fixed speed of the object every update to a specific value which is your constant speed.

The other alternative way to solve this would be to set the fixed speed of the object under the collision delegate functions.

like image 145
Wraithseeker Avatar answered Aug 05 '26 13:08

Wraithseeker


I had a similar problem that was resolved by setting physicsBody?.isDynamic = false on the node that the ball makes contact with.

For example if you have a ball and a brick:

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width / 2) // (diameter / 2) = radius
ball.physicsBody?.categoryBitMask = ballCategoryBitMask
// Detect contact with the bottom of the screen or a brick
//
ball.physicsBody?.contactTestBitMask = bottomCategoryBitMask | brickCategoryBitMask
ball.physicsBody?.friction = 0
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.applyImpulse(CGVector(dx: 10, dy: -10))

brick.physicsBody = SKPhysicsBody(rectangleOf: brick.frame.size)
brick.physicsBody?.linearDamping = 0
brick.physicsBody?.allowsRotation = false
brick.physicsBody?.isDynamic = false // Prevents the ball slowing down when it gets hit
brick.physicsBody?.affectedByGravity = false
brick.physicsBody?.friction = 0.0
like image 40
DoesData Avatar answered Aug 05 '26 15:08

DoesData