Configuring the physics world
From now on, all the script will be written in gamescript.js
as usual, so be prepared to write your first Cocos2d-JS Box2D script.
Some of the magic happens from the first two lines, which declare global variables:
var world; var worldScale = 30;
Here, the world
variable will represent the physics world we are setting the game in, and will include its own gravity and other properties that we will discover. However, first I want to say a couple of words about worldScale
.
Box2D is a realistic physics engine that uses real-world units of measurement. This way, everything you will create in Box2D world will be measured in meters. If you create a box whose side length is 2, you mean it's two meters.
On the other hand, browsers have their own unit of measurement, which is pixels. You can have a game 480 pixels wide, but you'll never find a game two meters wide.
So, we need to find a ratio between pixels and meters. In almost every project, the 1 meter = 30 pixels setting...