Contents

SKPhysicsBody

An object that adds physics simulation to a node.

Declaration

class SKPhysicsBody

Mentioned in

Overview

Assign a SKPhysicsBody object to the physicsBody 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.

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 isDynamic 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 SKPhysicsBody 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 SKPhysicsBody. You use a similar mechanism to declare which physics field nodes (SKFieldNode) can affect the physics body.

For a volume-based body, you can dynamically control how the body is affected by forces or collisions. See SKPhysicsBody.

Topics

First Steps

Creating a Body from a Shape

Creating a Body from a Texture

Creating a Body from a Collection of Bodies

Creating an Edge-Based Physics Body

Defining How Forces Affect a Physics Body

Defining a Physics Body’s Physical Properties

Working with Collisions and Contacts

Applying Forces and Impulses to a Physics Body

Inspecting a Physics Body’s Position and Velocity

Reading a Physics Body’s Node

Determining Which Joints Are Connected to a Physics Body

Interacting with Physics Fields

Pinning a Physics Body to a Node’s Parent

Initializers

See Also

Physics Simulation