There are many different kinds of field nodes that can be created, each with different effects. The section lists the field types you can create using SpriteKit, including a type that allows you to apply custom forces to physics bodies. Instantiate the appropriate kind of field node and then add it to the scene’s node tree.
When the scene simulates physics effects, a field node applies its effect to a physics body so long as the following things are true:. The field node is in the scene’s node tree. The field node’s property is true. The physics body is attached to a node that is in the scene’s node tree.
The physics body is located inside the field node’s region (see ). The physics body is not located inside the region of another field node whose property is set to true. A logical AND operation between the field node’s property and the physics body’s property results in a nonzero value. Tip While it is useful to know that SpriteKit measures items in the International System of Units, worrying about precise numbers is not that important. It doesn’t matter much whether your rocket ship weights 1 kilogram or 1,000,000 kilograms, as long as the mass is consistent with other physics values used in the game.
Often, proportions are more important than the actual values being used. Adding Physics Fields to Your Scene. It is possible to implement a game using only physics bodies, collisions, and applied forces, but sometimes this can be a lot of work. For example, if you wanted two objects to be attracted to each other, you would have to perform gravity calculations and apply those forces every time a new frame is calculated. When you want to apply generalized effects to a scene, you can do this easier using physics fields.
A physics field is a node that is placed inside your scene’s node tree. When the scene simulates physics, the physics field affects physics bodies that are configured to interact with it. There are many different kinds of fields effects, all of which are defined in. Shows one of these fields, a linear gravity field. In this example, a field node replaces the default gravity provided by the scene’s physics world. First, the scene's default gravity is disabled.
Then, the scene creates a linear gravity field with a vector pointing towards the bottom of the screen and gives the node a strength equal to Earth’s gravity. Finally, it adds the node to itself. PhysicsWorld.gravity = CGVector(dx:0, dy: 0); let gravityVector = vectorfloat3(0,-1,0); let gravityNode = SKFieldNode.linearGravityField(withVector: gravityVector) gravityNode.strength = 9.8 addChild(gravityNode) Here are some reasons you might use a field node rather than the default gravity:. You can vary the strength and direction of gravity independently of each other. You can vary the strength of the field using actions. You can change the direction of gravity by rotating the field node. By default, a field node affects all physics bodies in the scene.
However, it doesn’t have to be this way. By carefully controlling which physics bodies are affected by a field node, you can create some interesting effects. Field nodes are categorized just like physics bodies. Similarly, physics bodies declare which field categories affect them.
Combining these two properties, you can decide which kinds of fields are implemented by your game and which physics bodies are affected by each of those fields. Typically, you do this by using the field categories to define general effects that you can drop into a game. For example, assume for the moment that you are implementing a game where you want to add gravity to a planet. Gravity should pull objects such as ships and asteroids towards the planet.
The following code creates a category for gravity effects. When a planet is created, a separate radial gravity field node is added to it as a child, and is configured to use the gravity category. Whenever a new physics body is created, the body’s mask is configured to determine which fields should affect it.
Ships are affected by gravity, and missiles are affected by force fields. Let gravityCategory: UInt32 = 0x1.