【Configuring the Physical Properties of a Physics Body】
The SKPhysicsBody class defines properties that determine how the physics body is simulated.
- The mass property determines how forces affect the body, as well as how much momentum the body has when it is involved in a collision.
- The friction property determines the roughness of the body’s surface. It is used to calculate the frictional force that a body applies to other bodies moving along its surface.
- The linearDamping and angularDamping properties are used to calculate friction on the body as it moves through the world. For example, this might be used to simulate air or water friction.
- The restitution property determines how much energy a body maintains during a collision—its bounciness.
Other properties are used to determine how the simulation is performed on the body itself:
- The dynamic property determines whether the body is simulated by the physics subsystem.
- The affectedByGravity property determines whether the simulation exerts a gravitational force on the body. For more information on the physics world, see “Configuring the Physics World.”
- The allowsRotation property determines whether forces can impart angular velocity on the body.
【Mass Determines a Body’s Resistance to Acceleration】
You should set the mass on every volume-based body in your scene so that it properly reacts to forces applied to it.
A physics body’s mass, area, and density properties are all interrelated. When you first create a body, the body’s area is calculated, and never changes afterwards. The other two properties change values at the same time, based on the following formula:
mass = density x area
When you configure a physics body, you have two options:
- Set the mass property of the body. The density property is then automatically recalculated. This approach is most useful when you want to precisely control each body’s mass.
- Set the density property of the body. The mass property is then automatically recalculated. This approach is most useful when you have a collection of similar bodies created with different sizes. For example, if your physics bodies were used to simulate asteroids, you might give all asteroids the same density, and then set an appropriate bounding polygon for each. Each body automatically computes an appropriate mass based on its size on the screen.
【Physics World】
All physics bodies in a scene are part of the physics world, which is represented in Sprite Kit by an SKPhysicsWorld object attached to the scene. The physics world defines two important characteristics of the simulation:
- The gravity property applies an acceleration to volume-based bodies in the simulation. Static volumes and physics bodies that have set the affectedByGravity property to NO are unaffected.
- The speed property determines the rate at which the simulation runs.