1

基本的に、体には 2 つのポリゴンがあります。userData にスプライトを追加すると、テクスチャの位置が希望の位置になりません。私がやりたいのは、ボディ内のテクスチャの位置を調整することです。これを設定しているコードサンプルは次のとおりです。

CCSpriteSheet *sheet = (CCSpriteSheet*) [self getChildByTag:kTagSpriteSheet];
CCSprite *pigeonSprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(0,0,40,32)];
[sheet addChild:pigeonSprite z:0 tag:kPigeonSprite];

pigeonSprite.position = ccp( p.x, p.y);

bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);

b2CircleShape dynamicCircle;
dynamicCircle.m_radius = .25f;
dynamicCircle.m_p.Set(0.0f, 1.0f);

        // Define the dynamic body fixture.
b2FixtureDef circleDef;
circleDef.shape = &dynamicCircle;   
circleDef.density = 1.0f;
circleDef.friction = 0.3f;

body->CreateFixture(&circleDef);

b2Vec2 vertices[3];
vertices[0].Set(-0.5f, 0.0f);
vertices[1].Set(0.5f, 0.0f);
vertices[2].Set(0.0f, 1.0f);
b2PolygonShape triangle;
triangle.Set(vertices, 3);

b2FixtureDef triangleDef1;
triangleDef1.shape = ▵ 
triangleDef1.density = 1.0f;
triangleDef1.friction = 0.3f;

body->CreateFixture(&triangleDef1);
4

2 に答える 2

2

私は Objective-C にあまり詳しくありませんが、試してみます。

私が見ることができるのは、スプライト オブジェクトへのポインターを本体のユーザー データに格納し、そこに残していることだけです。ボディの位置をスプライトに転送する場合は、フレームごとに更新する必要があります。

C++ では、これは次のようになります。

// To be called each time physics should be updated.
void physicsStep(float32 timeStep, int32 velocityIterations, int32 positionIterations) {
    // This is the usual update routine.
    world.Step(timeStep, velocityIterations, positionIterations);
    world.ClearForces();

    // SpriteClass can be replaced with any class you favor.
    // Assume there is a known pointer to the b2Body. Otherwise you'll have to get that,
    // or iterate over all bodies in the world.
    SpriteClass *sprite = (SpriteClass*)body->GetUserData();

    // Once you have the pointer you can transfer all the data.
    sprite.position = body->GetPosition();
    sprite.angle = body->GetAngle();
    // ... and so on
}

ユーザー データは、b2Body 内の任意のストレージ スペースにすぎず、Box2D は、そこに何を格納するかについてはわかりません。

于 2010-06-30T10:26:48.450 に答える