我試圖使用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