문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top