iOS Developer Library

Developer

SpriteKit Framework Reference SKPhysicsWorld Class Reference

Options
Deployment Target:

On This Page
Language:

SKPhysicsWorld

Inherits From


Conforms To


Import Statement


Swift

import SpriteKit

Objective-C

@import SpriteKit;

Availability


Available in iOS 7.0 and later

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:

  • Set global properties for the simulation, such as gravity

  • Create joints between two physics bodies in the scene

  • Set a delegate to receive notifications when two physics bodies are in contact with each other

  • Determine which physics bodies within the scene intersect with points, rectangles, or rays

  • gravity gravity Property

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

    Declaration

    Swift

    var gravity: CGVector

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • speed speed Property

    The rate at which the simulation executes.

    Declaration

    Swift

    var speed: CGFloat

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Adds a joint to the physics world.

    Declaration

    Swift

    func addJoint(_ joint: SKPhysicsJoint)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Removes all joints from the physics world.

    Declaration

    Swift

    func removeAllJoints()

    Objective-C

    - (void)removeAllJoints

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Removes a specific joint from the physics world.

    Declaration

    Swift

    func removeJoint(_ joint: SKPhysicsJoint)

    Objective-C

    - (void)removeJoint:(SKPhysicsJoint *)joint

    Parameters

    joint

    The joint to remove.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

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

    Declaration

    Swift

    unowned(unsafe) var contactDelegate: SKPhysicsContactDelegate!

    Objective-C

    @property(nonatomic, assign) 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.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Searches for the first physics body that intersects a ray.

    Declaration

    Swift

    func bodyAlongRayStart(_ start: CGPoint, end end: CGPoint) -> SKPhysicsBody?

    Objective-C

    - (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. This may be any body along the ray; it is not guaranteed to be the closest physics body. If no body intersects the ray, this method returns nil.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Searches for the first physics body that contains a point.

    Declaration

    Swift

    func bodyAtPoint(_ point: CGPoint) -> SKPhysicsBody?

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Searches for the first physics body that intersects the specified rectangle.

    Declaration

    Swift

    func bodyInRect(_ rect: CGRect) -> SKPhysicsBody?

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

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

    Declaration

    Swift

    func enumerateBodiesAlongRayStart(_ start: CGPoint, end end: CGPoint, usingBlock block: ((SKPhysicsBody!, CGPoint, CGVector, UnsafeMutablePointer<ObjCBool>) -> Void)!)

    Objective-C

    - (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 YEStrue to terminate the enumeration.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

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

    Declaration

    Swift

    func enumerateBodiesAtPoint(_ point: CGPoint, usingBlock block: ((SKPhysicsBody!, UnsafeMutablePointer<ObjCBool>) -> Void)!)

    Objective-C

    - (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 YEStrue to terminate the enumeration.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

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

    Declaration

    Swift

    func enumerateBodiesInRect(_ rect: CGRect, usingBlock block: ((SKPhysicsBody!, UnsafeMutablePointer<ObjCBool>) -> Void)!)

    Objective-C

    - (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 YEStrue to terminate the enumeration.

    Import Statement

    Objective-C

    @import SpriteKit;

    Swift

    import SpriteKit

    Availability

    Available in iOS 7.0 and later

  • Samples all of the field nodes in the scene and returns the summation of their forces at that point.

    Declaration

    Objective-C

    - (vector_float3)sampleFieldsAt:(vector_float3)position

    Parameters

    position

    A position in scene coordinates.

    Return Value

    The summation of forces exerted on that point.

    Discussion

    The sample is calculated as if a physics body is placed at that position in the scene. The body is assumed to have a mass of 1.0, with no charge or velocity. The body is affected by all field nodes.

    Import Statement

    Objective-C

    @import SpriteKit;

    Availability

    Available in iOS 8.0 and later