Game Development Reference
In-Depth Information
Box2D units work quite well with meter-kilogram-second (MKS) units, and the moving objects work
well within a range of 0.1 to 10 meters. Although it might be tempting to use pixels as the units, this
leads to some weird behavior and poor simulation.
Lastly, we need to start the physics simulation with
world:start()
. To stop the physics simulation,
we can use
world:stop()
.
body = world:addBody( MOAIBox2DBody.DYNAMIC)
poly = {0,-1,1,0,0,1,-1,0}
fixture = body:addPolygon(poly)
fixture:setDestiny(1)
fixture:setFriction(0.3)
fixture:setFilter(0x01)
fixture:setCollisionHandler(onCollide, MOAIBox2DArbiter.BEGIN + MOAIBox2DArbiter.END, 0x02)
body:resetMassData()
body:applyAngularImpulse(2)
Now that we have created a dynamic body, we create a fixture that binds a shape to a body and
gives the body material properties like density, friction, and restitution.
Note
Restitution
deals with the way an object returns to its original shape or position after it has
been deformed after a collision.
If we run our code, a physics box will appear on the screen, as shown in Figure
10-12
.
Figure 10-12.
Moai Box2D physics bodies