HTML and CSS Reference
In-Depth Information
Defining shapes
Shapes are defined in a fixture. The two most common shapes in Box2D are rectangle and
circle. We define a rectangle with the SetAsBox funcion by providing half of its width and
height. Also, the circle shape is defined by the radius.
It is worth noing that the posiion of the body is at the center of the shape. It is diferent
from EaselJS in that the default origin point is set at the top-left corner.
Pixels per meter
When we deine the dimension and locaion of the body, we use meter as a unit. That's
because Box2D uses metric for calculaion to make the physics behavior realisic.
But we usually calculate in pixels on the screen. So, we need to convert between pixels
on the screen and meters in the physics world. That's why we need the pxPerMeter
variable here. The value of this variable might change from project to project.
The update method
In the game ick, we update the physics world.
The first thing we need to do is take the world to the next step. Box2D calculates objects
based on steps. It is the same as we see in the physical world when a second is passed. If
a ball is falling, at any ixed ime, the ball is staic with the property of the falling velocity.
In the next millisecond, or nanosecond, the ball falls to a new posiion. This is exactly how
steps work in the Box2D world. In every single step, the objects are staic with their physics
properies. When we go a step further, Box2D takes the properies into consideraion and
applies them to the objects.
This step takes three arguments. The irst argument is the ime passed since the last step.
Normally, it follows the frame-per-second parameter that we set for the game. The second
and the third arguments are the iteraion of velocity and posiion. This is the maximum
iteraions Box2D tries when resolving a collision. Usually, we set them to a low value.
The reason we clear the force is because the force will be applied indefinitely if we do not
clear it. That means the object keeps receiving the force on each frame unil we clear it.
Normally, clearing forces on every frame will make the objects more manageable.
Classified intel
We often need to represent a 2D vector in the physics world. Box2D uses b2vec for this
purpose. Similar to the b2vec funcion, we use quite a lot of Box2D funcions and classes.
They are modularized into namespaces. We need to alias the most common classes to make
our code shorter.

Search WWH ::

Custom Search