2

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.

  1. physicsBody.linearDamping = 0;
  2. physicsBody.friction = 0;

Is this even a physicsBody property, or does this behaviour stem from a property on the SKScene's physicsWorld property?

Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
  • You cannot preserve their momentum as you described. There are a couple of things you can do though to keep a steady speed. There are a number of things you could do. One being you could apply non-stop velocity to you object and cap its speed. – sangony May 05 '14 at 01:00
  • You can set the collisionBitMask of all the physicsBodies to zero. This will make them move on about as if they are not colliding with anything. However, you should set the contactTestBitMask, as then contact delegates will still be called. If you do want the bodies to collide, you can set their velocities in the contact delegates – ZeMoon May 05 '14 at 05:16
  • @sangony I found the answer and posted if you are still interested. – Brian Tracy May 30 '14 at 07:00

2 Answers2

1

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.

Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
1

Also density (or mass) should be considered. restitution alone didn't solve the loose in velocity on some body configurations.

Bogdan
  • 2,608
  • 2
  • 27
  • 26