An object that adds physics simulation to a node.
SDKs
- iOS 7.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 3.0+
Framework
- Sprite
Kit
Declaration
class SKPhysicsBody : NSObject
Overview
Assign a SKPhysics
object to the physics
property of the SKNode
object to add physics simulation to the node. When a scene processes a new frame, it performs physics calculations on physics bodies attached to nodes in the scene. These calculations include gravity, friction, and collisions with other bodies. You can also apply your own forces and impulses to a body. After the scene completes these calculations, it updates the positions and orientations of the node objects.
Important
A physics body must be associated with a node before you apply forces or impulses to it.
SpriteKit supports two kinds of physics bodies, volume-based bodies and edge-based bodies. When you create a physics body, its kind, size, and shape are determined by the constructor method you call. An edge-based body does not have mass or volume and is unaffected by forces or impulses in the system. Edge-based bodies are used to represent volumeless boundaries or hollow spaces in your physics simulation. In contrast, volume-based bodies are used to represent objects with mass and volume. The is
property controls whether a volume-based body is affected by gravity, friction, collisions with other objects, and forces or impulses you directly apply to it.
The SKPhysics
class defines the physical characteristics for the body when it is simulated by the scene. For volume-based bodies, the most important property is the mass
property. A volume-based body is assumed to have a uniform density. You can either set the mass
property directly, or you can set the body’s density
property and let the physics body calculate its own mass. All values in Sprite Kit are specified using the International System of Units (SI units). The actual forces and mass values are not important so long as your game uses consistent values.
When you design a game that uses physics, you define the different categories of physics objects that appear in the scene. You define up to 32 different categories of physics bodies, and a body can be assigned to as many of these categories as you want. In addition to declaring its own categories, a physics body also declares which categories of bodies it interacts with. See Working with Collisions and Contacts. You use a similar mechanism to declare which physics field nodes (SKField
) can affect the physics body.
For a volume-based body, you can dynamically control how the body is affected by forces or collisions. See Defining How Forces Affect a Physics Body.