See Also: SKPhysicsBody Members
The visual appearance of a SpriteKit.SKScene is determined by its SpriteKit.SKNodes. The physics simulation of a SpriteKit.SKScene is determined by the SpriteKit.SKPhysicsWorld object of the SpriteKit.SKScene's SKScene.PhysicsWorld property. The physics of that word are applied to those SpriteKit.SKNodes that have a non-null SKNode.PhysicsBody.
The following example demonstrates a simple dynamic system:
C# Example
var blockSize = new SizeF(20, 20); var node1 = new SKSpriteNode(UIColor.Red, blockSize) { Position = new PointF(100, 100), Name = "Red", PhysicsBody = SKPhysicsBody.BodyWithRectangleOfSize(blockSize), }; var node2 = new SKSpriteNode(UIColor.Green, blockSize) { Position = new PointF(150, 100), Name = "Green", PhysicsBody = SKPhysicsBody.BodyWithRectangleOfSize(blockSize) }; //Pin node1 in position and connect node2 to it with a spring node1.PhysicsBody.Dynamic = false; var joint = SKPhysicsJointSpring.Create(node1.PhysicsBody, node2.PhysicsBody, node1.Position, node2.Position); //Add nodes to scene scene.AddChild(node1); scene.AddChild(node2); //Add the joint to the physics world scene.PhysicsWorld.AddJoint(joint);
Application developers must add the SpriteKit.SKNodes to the scene-graph prior to setting SpriteKit.SKPhysicsJoints or Sprite Kit may segfault.