I tried to make a box of balls where the balls move with constant velocity. The shouldn't slow down when they collide with each other. I think I have set all properties right but it didn't work and after 30s all of the balls stopped to move.
The box is set like this:
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody.dynamic = false
self.physicsBody.restitution = 1
self.physicsBody.friction = 0
The balls are set like this:
Is this a bug of the physics engine or am I missing something?
If you want them to have a constant velocity all the time, no change at all, you're going to have to set their velocity to a fixed-length vector in SKScene update. Physics engines aren't designed to adhere strictly to the conservation of energy law ... or one could argue that some energy is dissipated through heating the device. ;)
General principle for keeping the same direction but adjusting vector length/speed to a fixed value (pseudo-code):
CGPoint velocity = somePhysicsBody.velocity;
velocity = normalized(velocity);
velocity = multiply(velocity, desiredSpeed);
somePhysicsBody.velocity = velocity;