Connecting Physics Bodies

时间:2023-12-11 17:54:08

Connecting Physics Bodies

  The kinds of joints you can create in Sprite Kit.

  Connecting Physics Bodies

  Connecting Physics 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:

  1. Create two physics bodies.
  2. Attach the physics bodies to a pair of SKNode objects in the scene.
  3. Create a joint object using one of the subclasses listed in Table 8-3.
  4. If necessary, configure the joint object’s properties to define how the joint should operate.
  5. Retrieve the scene’s SKPhysicsWorld object.
  6. Call the physics world’s addJoint: method.

Searching for Physics Bodies

  Sometimes, it is necessary to find physics bodies in the scene. For example, you might need to:

  • Discover whether a physics body is located in a region of the scene.
  • Detect when a physics body (such as the one controlled by the player) crosses a particular line.
  • Trace the line of sight between two physics bodies, to see whether another physics body, such as a wall, is interposed between the two objects.

  In some cases, you can implement these interactions using the collisions and contacts system. For example, to discover when a physics body enters a region, you could create a physics body and attach it to an invisible node in the scene. Then, configure the physics body’s collision mask so that it never collides with anything, and its contact mask to detect the physics bodies you are interested in. Your contact delegate is called when the desired interactions occur.

  You can use physicalsworld's [bodyAlongRayStart:end:] to get the body between start & end.

  And you can also use bodyAtPoint: and bodyInRect: to get bodies at point or in rect.

Most Physics Properties Are Dynamic, so Adapt Them at Runtime

  As an object moves throughout the scene, you adjust its linear and rotational damping based on the medium it is in. For example, when the object moves into water, you update the properties to match.