Evaluating the results
All this code and rules had an objective, physics simulation. You could probably had achieved the exact same results with half the code but now we'll find why using Chipmunk is the better option:
- Try changing the friction of all the floor's shapes from 0.1 to 0.5. Run the code, the fall will now lose more velocity.
- Try increasing or decreasing the gravity and/or the ball's mass to several different values.
- Try editing the shapes a bit.
Finally, you may have noticed that all the shapes had their elasticity property set but it doesn't seem to have any effect on the simulation. To enable elastic body's simulation add the following code after setting up the gravity of the space (the order doesn't matter as long as it's after the space was created obviously):
// Add some elastic effects to the simulation space->elasticIterations = 10;
Now if you run the simulation and/or play with the elasticity values of the shapes you'll see the ball bouncing off the floor when it falls.
Though flexible and easy to use Chipmunk requires some time and effort to understand how it works and some of its concepts. In this tutorial I hope to have been clear enough to the point where anyone could use Chipmunk's basic features: space, bodies and shapes.
There is a lot more to be said and learnt including forces, collision detection and joints. I hope to be back with a follow up covering some of those topics.