In my SpriteKit
project, I am learning to use the wonderful, built in physics engine. I am accomplishing this through the use of SKPhysicsBody
instances attached to nodes, and it has worked out well so far. My current set up is when I add a node, I set its physicsBody
's velocity
vector to some constant velocity. Despite manually setting the velocity to some fixed value, after a few seconds of nodes colliding, their velocity decreases. I assume this is the default characteristic as it simulates real life physics (loss of energy through multiple collisions). I would like to stop this behavior. For example, I would like, despite numerous collisions, all energy to be perfectly "preserved" and no velocity lost. Here are a few things I have tried to no avail.
physicsBody.linearDamping = 0;
physicsBody.friction = 0;
Is this even a physicsBody
property, or does this behaviour stem from a property on the SKScene
's physicsWorld
property?
After much time spent reviewing the Apple Documentation, I found the answer. The restitution
property on the SKPhysicsBody
controls how much energy is lost when that body collides with others. This property is a float that is in the range of [0 ... 1]
, which inversely correlates to the amount of energy lost in collisions (the higher the number, the less energy lost). For example, the default value of this property is .2
, representing a rather high energy loss. To solve my problem, I set this property to 1
on each of my bodies so when they interact, no energy is lost.
self.someNode.physicsBody.restitution = 1.0f;
The Results: This fixed the problem 100% and after several hours of simulation, the physics bodies lose no energy at all.