2014-12-04 2 views
0

luabind를 사용하여 box2d를 바인딩하면 루아 스크립트에서 사용할 수 있습니다. 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) 
]; 

가 여기 내 루아 코드입니다 : 다음은 내 코드입니다

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

방법 : 나는 anchorFixDef.shape = anchorShape를 사용하여 내 fixtureDef에 모양을 지정하려고

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 

매번, 루아에서 오류가 발생합니다 .def_readwrite("shape", &b2FixtureDef::shape)과 같은 것이 나에게 문제를주고 있기 때문에 luaBind에 const b2Shape* shape;과 같은 것을 묶는 것에 대해 갈 것인가? class_ 바인딩 문에서 스마트 포인터를 사용하는 문서에서 코드를 보았지만 문제가 해결되지 않았습니다.

감사합니다.

답변

0

&shape을 설정 가능한 매개 변수로 표시하면 개체()에 주소 (anchorShape)가 할당됩니다. 구문의 일부분 인 &shapeshape 회원의 주소를 수정할 수 있다고 생각할 수도 있지만 불가능합니다. 주소가 &shape 일 때 유형이 shape 인 전체 객체를 작성해야하지만 lua의 anchorFixDef.shape = anchorShape 문은 포인터 할당을 수행합니다. 이것이 루아 바인드가 질식하는 이유입니다.

당신은 옵션이 있습니다

  1. 때문에 모양 필드처럼 루아에 세터를 제공합니다 :

    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로 가정)이 있기 때문일 수 있습니다.

+0

포인트 1을 조사하겠습니다 만, 모양은 이미 b2Fixture의 포인터입니다. 내가 묶으려고하는 것이지. https://code.google.com/p/box2d/source/browse/trunk/Box2D/Box2D/Dynamics/b2Fixture.h#71 – Sun

+0

@Sun, 여기서 추측합니다. 루아 바인드는 "알려진"유형에 대한 "readwrite"를 통해서만 할당을 지원할 수 있습니다. 그리고 포인터는 "알 수없는"유형입니다. –

관련 문제