I'm attempting to use luabind to bind box2d so that I can use it within my lua scripts. I've hit a problem where I cant seem to bind raw pointers with luabind. Below is my code:
luabind::module(luaState)[
luabind::class_<b2Shape>("b2Shape")
];
luabind::module(luaState)[
luabind::class_<b2PolygonShape, luabind::bases<b2Shape>>("b2PolygonShape")
.def(luabind::constructor<>())
.def("GetChildCount", &b2PolygonShape::GetChildCount)
.def("SetAsBox", (void (b2PolygonShape::*) (float32 hx, float32 hy) ) &b2PolygonShape::SetAsBox)
.def("SetAsBox", (void (b2PolygonShape::*) (float32 hx, float32 hy, const b2Vec2& center, float32 angle) ) &b2PolygonShape::SetAsBox)
.def("TestPoint", (void (b2PolygonShape::*) (const b2Transform& transform, const b2Vec2& p) ) &b2PolygonShape::TestPoint)
.def("ComputeAABB", (void (b2PolygonShape::*) (b2AABB* aabb, const b2Transform& transform, int32 childIndex) ) &b2PolygonShape::ComputeAABB)
.def("GetVertexCount", (void (b2PolygonShape::*) () ) &b2PolygonShape::GetVertexCount)
.def("GetVertex", (const b2Vec2& (b2PolygonShape::*) (int32 index) ) &b2PolygonShape::GetVertexCount)
.def("Validate", &b2PolygonShape::Validate)
];
luabind::module(luaState)[
luabind::class_<b2FixtureDef>("b2FixtureDef")
.def(luabind::constructor<>())
.def_readwrite("shape", &b2FixtureDef::shape)
.def_readwrite("friction", &b2FixtureDef::friction)
.def_readwrite("restitution", &b2FixtureDef::restitution)
.def_readwrite("density", &b2FixtureDef::density)
.def_readwrite("isSensor", &b2FixtureDef::isSensor)
.def_readwrite("filter", &b2FixtureDef::filter)
];
Here's my lua code:
local anchorBodyDef = b2BodyDef()
anchorBodyDef.position = b2Vec2(20.0, 0.0)
local anchorShape = b2PolygonShape()
anchorShape:SetAsBox(2.0, 0.5)
local anchorFixDef = b2FixtureDef()
anchorFixDef.shape = anchorShape
Everytime I attempt to assign a shape to my fixtureDef using anchorFixDef.shape = anchorShape
, lua throws an error:
terminate called after throwing an instance of 'luabind::error'
what(): lua runtime error
How would you go about binding something like const b2Shape* shape;
in luaBind since something like .def_readwrite("shape", &b2FixtureDef::shape)
is giving me problems. I've seen some code in the docs which use smart pointers in the class_ binding statements, but that hasn't fixed the problems.
Thanks.
By exposing
&shape
as a settable parameter, you're trying to assign an address(anchorShape
, because that is what it is) to an object(shape
). The&shape
part of the syntax might make you think that you're allowed to modify the address ofshape
member, but it is not possible. Since at the address&shape
you should write the whole object of typeshape
, but theanchorFixDef.shape = anchorShape
statement in lua just does a pointer assignment. This is why luabind is choking.You have to options:
Provide a setter to Lua like so for the shape field:
luabind::class_<b2FixtureDef>("b2FixtureDef") .property("shape", &b2FixtureDef::GetShape, b2FixtureDef::SetShape) .def_readwrite("friction", &b2FixtureDef::friction)
//assuming friction is a basic type and so on
Make
shape
a pointer inside b2Fixture.The former is much more preferable as it avoids all complications related to pointer memory management and it is a nice why to wrap up members of a class.
The reason
def_readwrite
works for other members is probably because they have simple basic types (I assume something like float or int), which have an equivalent type in Lua.