box2d ポリゴンを作成していて、フィクスチャをボディに取り付けるメソッドの外側にフィクスチャを作成したいのですが、これを行うとアクセス違反が発生します。
私はコードをステップ実行しましたが、box2d は確実にフィクスチャ オブジェクトを取得しています。
メソッド内のボディにフィクスチャを取り付けると、正常に動作します。呼び出し元のメソッドで試してみると、アクセス違反エラーがスローされます
b2FixtureDef* LineSegment::GenerateFixture(b2Vec2 vertices[4],b2Body* body,b2World* world){
b2BodyDef bodyDef;
//bodyDef.type = b2_kinematicBody;
bodyDef.position.Set(_currentStepStartPosition.x,_currentStepStartPosition.y);
body =world->CreateBody(&bodyDef);
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDef2;
fixtureDef2.shape = &polygon;
fixtureDef2.density = 1.0f;
fixtureDef2.friction = 0.3f;
body->CreateFixture(&fixtureDef2);/// works if i attatch the fixture here here
return &fixtureDef2;
}
ここにフィクスチャを取り付けると失敗する呼び出しメソッド
bool LineSegment::GenerateNextBody(b2Body* retBody){
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(_currentStepStartPosition.x,_currentStepStartPosition.y);
retBody =world->CreateBody(&bodyDef);
_currentStep++;
b2Vec2 vertices[4];
if(_inclinePerStep != 0){
GetVertsInclineSquare(vertices,_stepWidth,_thickness,_inclinePerStep);
}else{
GetVertsSquare(vertices,_stepWidth,_thickness);
}
if(_currentStep == 1){
_GameWorldVerticies[0] =b2Vec2((_currentStepStartPosition.x+vertices[0].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[0].y)*PTM_RATIO);
_GameWorldVerticies[3] =b2Vec2((_currentStepStartPosition.x+vertices[3].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[3].y)*PTM_RATIO);
}
b2FixtureDef* fixture = GenerateFixture(vertices,retBody,world);
//retBody->CreateFixture(fixture); throws a access violation if i attatch the fixture here
_currentStepStartPosition.x += vertices[2].x+(vertices[2].x);
_currentStepStartPosition.y +=(_inclinePerStep/2);
if(_steps <= _currentStep){
_GameWorldVerticies[1] =b2Vec2((_currentStepStartPosition.x+vertices[1].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[1].y)*PTM_RATIO);
_GameWorldVerticies[2] =b2Vec2((_currentStepStartPosition.x+vertices[2].x)*PTM_RATIO,(_currentStepStartPosition.y+vertices[2].y)*PTM_RATIO);
return false;
}
return true;
}
アップデート
新しいスニペット
b2FixtureDef* fixtureDef2 = new b2FixtureDef();
fixtureDef2->shape = &polygon;
fixtureDef2->density = 1.0f;
fixtureDef2->friction = 0.3f;
//body->CreateFixture(&fixtureDef2);/// works if i attatch the fixture here here
return fixtureDef2;