2014-12-04 34 views
0

我试图使用luabind绑定box2d,以便我可以在我的lua脚本中使用它。我遇到了一个问题,我似乎无法将原始指针与luabind绑定。下面是我的代码:如何绑定与Luabind的原始指针

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) 
]; 

这里是我的Lua代码:

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 

每次我试图将一个形状分配给使用anchorFixDef.shape = anchorShape我fixtureDef,LUA抛出一个错误:

terminate called after throwing an instance of 'luabind::error' 
what(): lua runtime error 

如何你会去约束如luaBind const b2Shape* shape;,因为像.def_readwrite("shape", &b2FixtureDef::shape)给我的问题。我在文档中看到了一些在class_ binding语句中使用智能指针的代码,但这并没有解决问题。

谢谢。

回答

0

通过公开&shape为可设置的参数,你要分配一个地址(anchorShape,因为那是它是什么)的一个对象(shape)。语法的&shape部分可能会让您认为您可以修改shape成员的地址,但这是不可能的。由于地址为&shape,您应该写入shape类型的整个对象,但lua中的anchorFixDef.shape = anchorShape语句仅执行指针分配。这就是为什么Luabind窒息。

你必须选择:

  1. 提供一个二传手到Lua像这样的形状领域:

    luabind::class_<b2FixtureDef>("b2FixtureDef") .property("shape", &b2FixtureDef::GetShape, b2FixtureDef::SetShape) .def_readwrite("friction", &b2FixtureDef::friction)
    //assuming friction is a basic type and so on

  2. shape内b2Fixture的指针。

前者是更优选的,因为它避免了与指针的内存管理所有的并发症,它是一个很好的理由来包装一个类的成员。

原因def_readwrite适用于其他成员可能是因为他们有简单的基本类型(我假设像float或int),它们在Lua中具有相同的类型。

+0

我会研究一点,但形状已经是b2Fixture中的一个指针。这是我想要绑定的。 https://code.google.com/p/box2d/source/browse/trunk/Box2D/Box2D/Dynamics/b2Fixture.h#71 – Sun 2014-12-05 20:48:15

+0

@孙,我在这里推测。 luabind可能仅支持通过'readwrite'为“已知”类型进行赋值。指针是一个“未知”类型。 – 2014-12-05 23:45:28

相关问题