Tuesday, 27 January 2015

SpriteKit Joints


Joints

In Sprite Kit, joints allow you to constrain the positions of two bodies relative to each other. Let’s consider what practical uses you might find in a game for the five different types of joints that Sprite Kit offers.

Fixed joint

A fixed joint gives you the ability to fix two physics bodies together. Imagine you have two objects and you nail them to each other with a number of big, rusty nails. The two objects are fixed together, so if you take one of them and throw it, the other one will fly with it. Fixed! 

                      





Often, you want an object to be immoveable. Just fix it to the scene edge loop and you are ready to go. Other times, you want a complex object that the player can destroy into pieces. Just fix the parts together and remove the joints when you want the body to fall apart.

Limit joint 

A limit joint sets the maximum distance two objects can be from each other. The two bodies can be closer than that distance, but not farther apart. You can imagine the limit joint as a soft but strong rope that connects two objects. On the diagram below, the ball is connected to the square via a limit joint—it can bounce around, but can never move farther away than the length of the limit joint:

                                    


For example, this can be useful to make sure that different players always stay within a fixed range of each other in multiplayer games.

Spring joint

A spring joint acts much like a limit joint, but the connection between the two bodies is also elastic, much like if the two bodies were connected by a spring. As with the limit joint, this joint is useful to simulate rope connections, especially ropes made of elastic. If you have a game about bungee jumping, the spring joint will be of great help!

                                               


Pin joint

A pin joint fixes two objects around a certain point, the anchor of the joint. This allows both to rotate freely around this point—if they don’t collide, of course. You can imagine the pin joint as a big screw that keeps two objects tightly together, but still allows them to rotate:


If you were to build a clock, you’d use a pin joint to fix the hands to the dial, or if you were to build a physics body for an airplane, you’d use a pin joint to attach the propeller to the plane’s nose.

Sliding joint

A sliding joint fixes two bodies on an axis along which they can freely slide; you can further define the minimum and maximum distances the two bodies can be from each other while sliding along the axis.

The two connected bodies act as though they are moving on a rail and there’s a limit on their distance from each other: 

                                     
You can use the sliding joint for complex interactive bodies, like a crane with a control cabin on top and a robotic arm, both hanging on the crane’s jib—the cabin and the robotic arm being the two bodies that slide along the crane’s jib axis. 



Gopinath T B,
CEO, Meteora Gaming

1 comment: