In the last article we introduced the concepts behind the SpaceManager framework. SpaceManager makes it easy to integrate physics effects to an iPhone application. In this article we are going to take a look at Spring and Slide effects which adds awesomeness to your iPhone application.


Joints:

There are many different kind of joints available in the SpaceManager framework. Joints enables two bodies to connect to each other. This connection can be created in many different ways since there are number of joints available in the SpaceManager framework. In the next section we are going to demonstrate how to create Spring and Slide joints between two bodies in space. 

Adding Spring Joint:

A Spring Joint connects two bodies together using a spring. Two bodies can be stationary objects, moving objects or combination of both. The following implementation joins a stationary object rectangle to a movable ball object.

  

Most of the code is same as in the previous article with the exception of a joint. The joint makes it possible to attach two bodies together. The addSpringToBody method attaches a spring from the ballSprite to the block. Make sure to attach the spring to the shape property of the body and not the sprite itself. The stiffness and restLength attributes further specifies how the spring will behave in space. A new cpConstraintNode instance is created to which the joint is assigned. Finally, we add the cpConstraintNode (jointNode) to the space. The result is shown below:



Currently, the only force of nature applying to our bodies in space is gravitational pull acting downwards. This force will cause the spring movement. We can however apply additional forces as shown in the implementation below:



Now, a force is applied where ever we touch the iPhone screen. The result is shown in the screenshot below:



Adding Slide Joint:

A slide joint can be considered a connection between two bodies where the length can be stretched to a maximum and a minimum. The implementation below shows the slide joint in action. 



The implementation above indicates that the minLength between the two bodies participating in slide joint is 100 and the maxLength is 200. The effect is shown in the screenshow below:




Removing Joints:

There are situations where you want to dynamically remove a joint between the two bodies. When one body is removed from the joint another body must be added to accommodate the joint. In other words a joint must be connected at both the ends. The following code is invoked when the user touches a button (ball) on the screen.

   

As you can see the removeAndFreeConstraint is fired which removes the joint between the two objects. Next, we connect a different sprite to the joint and then add the new jointNode to the space. The result is shown below:




Looks kinda scary right!

Conclusion:

In this article we demonstrated how to get started with joints in SpaceManager library. Joints adds more interactivity to the application. In the next article we are going to take a look at the Particle system provided by Cocos2d framework.

[Download Sample