Musicala v1.2 – Lua-based 2D physics for all
September 19th, 2007
Yesterday, I threw in Lua scripting support in the Musicala framework and also took the time to bind it to Box2D (ugh – 905 complete).

So instead of writing C++ code like this:
// Create left, bottom, right walls
Ogre::Real length = 50.0f;
Ogre::Real thickness = 2.0f;
b2World* world = box2d->getWorld();
{
b2ShapeDescription sd;
sd.type = e_boxShape;
sd.box.m_extents.Set(length, thickness);
b2BodyDescription bd;
bd.position.Set(0.0f, -length);
bd.AddShape(&sd);
ground = world->CreateBody(&bd);
}
{
b2ShapeDescription sd;
sd.type = e_boxShape;
sd.box.m_extents.Set(length, thickness);
b2BodyDescription bd;
bd.position.Set(length, 0.0f);
bd.rotation = b2_pi*0.5f;
bd.AddShape(&sd);
world->CreateBody(&bd);
}
{
b2ShapeDescription sd;
sd.type = e_boxShape;
sd.box.m_extents.Set(length, thickness);
b2BodyDescription bd;
bd.position.Set(-length, 0.0f);
bd.rotation = b2_pi*-0.5f;
bd.AddShape(&sd);
world->CreateBody(&bd);
}
You write it in Lua so you don’t need to re-compile the app if you want a small change.
-- Start
local box2d = Musicala.Box2dManager:getSingletonPtr()
local world = box2d:getWorld(0)
local sd = b2ShapeDescription()
sd.type = e_boxShape
sd.box.m_extents:Set(50, 2)
local bd = b2BodyDescription()
bd.position.x = 0.0
bd.position.y = -50.0
bd:AddShape(sd)
world:CreateBody(bd)
bd.position.x = 50.0
bd.position.y = 0.0
bd.rotation = 3.1415*0.5
bd:AddShape(sd)
world:CreateBody(bd)
bd.position.x = -50.0
bd.position.y = 0.0
bd.rotation = -3.1415*0.5
bd:AddShape(sd)
world:CreateBody(bd)
-- End
Get the demo here which I’ve temporarily named “Lua2D”:
http://sourceforge.net/project/showfiles.php?group_id=197381&package_id=246104
Box2D is quite stable, but a little too stable. The typically jitter of classical 2D physics engine is GONE (say bye-bye penalty methods too), but is now replaced with a “rigid stone block” feel. I guess that’s typical of rigid body physics (no deformations).
TODO
- Fix passing of arrays of arrays between C++ and Lua
- Add b2 utils like random to the Lua binding.
Entry Filed under: Game Development
Trackback this post