2014-12-04 68 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