Simulating physics in Sprite Kit
Most of the game engines have an inbuilt physics engine, and you can also add an external physics engine to a game engine. Fortunately, Apple provides a physics engine in Sprite Kit. In Sprite Kit, physics properties are applied by an object of the class, SKPhysicsBody
. As we have already learned that objects are connected to a node in a node tree, physics simulation uses a node's orientation and position for simulation. In Sprite Kit, when a game renders, each frame invokes some functions in a cycle, as follows:
update didEvaluateActions didSimulatePhysics didApplyConstraints didFinishUpdate
After actions (such as image changing in a node for animation) SKScene
simulates physics to do all the actions, such as gravity on a physics body, velocity change, collision between two physics bodies, and so on. If we go through our SKNode
class, we will see there is a property called physicsBody
. It takes the SKPhysicsBody
object as a parameter and defines physics laws...