我试图使用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语句中使用智能指针的代码,但这并没有解决问题。
谢谢。
我会研究一点,但形状已经是b2Fixture中的一个指针。这是我想要绑定的。 https://code.google.com/p/box2d/source/browse/trunk/Box2D/Box2D/Dynamics/b2Fixture.h#71 – Sun 2014-12-05 20:48:15
@孙,我在这里推测。 luabind可能仅支持通过'readwrite'为“已知”类型进行赋值。指针是一个“未知”类型。 – 2014-12-05 23:45:28