SKPhysicsWorld Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/SpriteKit.framework
Availability
Available in iOS 7.0 and later.
Companion guide
Declared in
SKPhysicsWorld.h

Overview

An SKPhysicsWorld object simulates collisions and other physical properties. You do not create SKPhysicsWorld objects directly; instead, read the physicsWorld property of an SKScene object.

The physics world object allows you to perform the following tasks:

Tasks

Configuring the Physics World Simulation

Joining Physics Bodies Together

Detecting Collisions in the Physics World

Searching for Bodies in the World

Properties

contactDelegate

A delegate that is called when two physics bodies come in contact with each other.

@property(assign, nonatomic) id<SKPhysicsContactDelegate> contactDelegate
Discussion

A contact is created when two physics bodies overlap and one of the physics bodies has a contactTestBitMask property that overlaps with the other body’s categoryBitMask property.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

gravity

A vector that specifies the gravitational acceleration applied to physics bodies in the physics world.

@property(nonatomic) CGVector gravity
Discussion

The components of this property are measured in meters per second. The default value is (0.0,-9.8), which represent’s Earth’s gravity.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

speed

The rate at which the simulation executes.

@property(nonatomic) CGFloat speed
Discussion

The default value is 1.0, which means the simulation runs at normal speed. A value other than the default changes the rate at which time passes in the physics simulation. For example, a speed value of 2.0 indicates that time in the physics simulation passes twice as fast as the scene’s simulation time. A value of 0.0 pauses the physics simulation.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

Instance Methods

addJoint:

Adds a joint to the physics world.

- (void)addJoint:(SKPhysicsJoint *)joint
Parameters
joint

The joint to add.

Discussion

For a joint to take effect, it must be added to the physics world.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

bodyAlongRayStart:end:

Searches for the first physics body that intersects a ray.

- (SKPhysicsBody *)bodyAlongRayStart:(CGPoint)start end:(CGPoint)end
Parameters
start

The starting point for the ray in scene coordinates.

end

The ending point for the ray in scene coordinates.

Return Value

The first physics body discovered that intersects the ray. If no body intersects the ray, this method returns nil.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

bodyAtPoint:

Searches for the first physics body that contains a point.

- (SKPhysicsBody *)bodyAtPoint:(CGPoint)point
Parameters
point

A point in scene coordinates.

Return Value

The first physics body discovered that contains the point. If no body contains the point, this method returns nil.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

bodyInRect:

Searches for the first physics body that intersect the specified rectangle.

- (SKPhysicsBody *)bodyInRect:(CGRect)rect
Parameters
rect

A rectangle in scene coordinates.

Return Value

The first physics body discovered that intersects the rectangle. If no body intersects the rectangle, this method returns nil.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

enumerateBodiesAlongRayStart:end:usingBlock:

Enumerates all the physics bodies in the scene that intersect a ray.

- (void)enumerateBodiesAlongRayStart:(CGPoint)start end:(CGPoint)end usingBlock:(void (^)(SKPhysicsBody *body, CGPoint point, CGVector normal, BOOL *stop))block
Parameters
start

The starting point for the ray in scene coordinates.

end

The ending point for the ray in scene coordinates.

block

A block to be called for each physics body that the ray touches. The block takes the following parameters:

body

The physics body that the ray intersected.

point

The point in scene coordinates where the ray contacted the physics body.

normal

The normal vector for the physics body at the point of contact.

stop

A pointer to a Boolean variable. Your block can set this to YES to terminate the enumeration.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

enumerateBodiesAtPoint:usingBlock:

Enumerates all the physics bodies in the scene that contain a point.

- (void)enumerateBodiesAtPoint:(CGPoint)point usingBlock:(void (^)(SKPhysicsBody *body, BOOL *stop))block
Parameters
point

A point in scene coordinates.

block

A block to be called for each physics body that contains the point. The block takes the following parameters:

body

The physics body that the ray intersected.

stop

A pointer to a Boolean variable. Your block can set this to YES to terminate the enumeration.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

enumerateBodiesInRect:usingBlock:

Enumerates all the physics bodies in the scene that intersect the specified rectangle.

- (void)enumerateBodiesInRect:(CGRect)rect usingBlock:(void (^)(SKPhysicsBody *body, BOOL *stop))block
Parameters
rect

A rectangle in scene coordinates.

block

A block to be called for each physics body that contains the point. The block takes the following parameters:

body

The physics body that intersected the rectangle.

stop

A pointer to a Boolean variable. Your block can set this to YES to terminate the enumeration.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

removeAllJoints

Removes all joints from the physics world.

- (void)removeAllJoints
Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h

removeJoint:

Removes a specific joint from the physics world.

- (void)removeJoint:(SKPhysicsJoint *)joint
Parameters
joint

The joint to remove.

Availability
  • Available in iOS 7.0 and later.
Declared In
SKPhysicsWorld.h