The abstract superclass for objects that connect physics bodies.
- iOS 7.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 3.0+
SKPhysics object connects two physics bodies so that they are simulated together by the physics world. You never instantiate objects of this class directly; instead, you instantiate one of the subclasses that defines the kind of joint you want to make.
Connecting Physics Bodies
Although you can make a lot of interesting games that use the physics systems that have already been described, you can take your designs further by connecting physics bodies using joints. When the scene simulates the physics, it takes these joints into account when calculating how forces affect the bodies.
You add or remove joints using the physics world. When you create a joint, the points that connect the joint are always specified in the scene’s coordinate system. This may require you to first convert from node coordinates to scene coordinates before creating a joint.
To use a physics joint in your game, follow these steps:
Create two physics bodies.
Attach the physics bodies to a pair of
SKNodeobjects in the scene.
Create a joint object using one of the subclasses listed in the table above.
If necessary, configure the joint object’s properties to define how the joint should operate.
Retrieve the scene’s
Call the physics world’s
The following code pins two nodes,
piston, together using a
SKPhysics joint. In this example, the node
pin is a child of
connecting. Because it is not an immediate child of the scene, its coordinates are converted to scene coordinates for use initializing the joint:
Disconnecting Physics Bodies
You can use the
remove(_:) to remove joints from a simulation. By making use of the
update(_: method of the
SKScene, you can interrogate the properties of joints — such as the distance between the two connected bodies.
The following code shows an example of this. Each time a new spring joint is created, it is also added to an array named
springs of type
[SKPhysics. With each simulation step, every spring is evaluated and if the distance between the two connected bodies is greater than
max, the spring is removed from both the physics world and the array: