1

したがって、ここで欠落しているコードを理解するのに助けが必要です。いたるところをチェックしましたが、使用されている式やまだ気付いていないタイプミスについて詳細が必要です。これがポリゴンクラスです。私は 8 つの頂点を持つランダムなポリゴンを作成し、もちろん無地の色で塗りつぶそうとしています。しかし、ランダムな位置を生成し続けたいが、固定したままにしておきたい. より良い方法では、ポリゴンは私の地形です.Ok改訂:ポリゴンはそこにあり、私のキャラクターはそれらと相互作用しますが、私はそれらを見ることができず、はい、それらは同じレイヤーにあります. ああ、しかし、それらは下部に生成され続けません。画面から消えたら古いものを削除するだけで、新しいポリゴンを作成する必要があると思います。

-(void) genBody:(b2World *)world pos:(CGPoint *)pos {
//Here we generate a somewhat random convex polygon by sampling
//the "eccentric anomaly" of an ellipse with randomly generated
//x and y scaling constants (a,b). The algorithm is limited by
//the parameter max_verts, and has a number of tunable minimal
//and scaling values.
// I need to change this to randomly choosing teh number of vertices between 3-8,
// then choosing random offsets from equally distributed t values.
// This will eliminate teh while loop.
screen_pos = ccp(pos->x, pos->y);
float cur_t;
float new_t;
float delta_t;
float min_delta_t = 0.5;
float t_scale = 1.5;
b2Vec2 *verts= new b2Vec2[m_maxVerts]; // this should be replaced by a private verts ... maybe ... hmm that will consume more ram though
float t_vec[m_maxVerts];

// Generate random vertices
int vec_len;
while (true) {
    cur_t = 0.0;
    for (vec_len=0; vec_len<m_maxVerts; vec_len++) {
        //delta_t = t_scale*(float)rand()/(float)RAND_MAX; // wish they just had a randf    method :/
        delta_t = t_scale*floorf((double)arc4random()/ARC4RANDOM_MAX);
#ifdef POLY_DEBUG
        CCLOG(@"delta_t %0.2f", delta_t);
#endif
        if (delta_t < min_delta_t) {
            delta_t = min_delta_t;
        }
        new_t = cur_t + delta_t;
        if (new_t > 2*PI) {
            break;
        }
        t_vec[vec_len] = new_t;
        cur_t = new_t;
    }
    // We need at least three points for a triangle
    if ( vec_len > 3 ) {
        break;
    }
}

少なくともボディが生成されている場所。それから...

float num_verts = vec_len;

b2BodyDef BodyDef;
BodyDef.type = b2_staticBody;
BodyDef.position.Set(pos->x/PTM_RATIO, pos->y/PTM_RATIO);
BodyDef.userData = self; // hope this is correct

m_polyBody = world->CreateBody(&BodyDef);

b2PolygonShape polyShape;
int32 polyVert = num_verts;
polyShape.Set(verts, polyVert);

b2FixtureDef FixtureDef;
FixtureDef.shape = &polyShape;
FixtureDef.userData = self; // hope this is correct
FixtureDef.density = 1.6f;
FixtureDef.friction = 0.4f;
FixtureDef.restitution = 0.5f;

m_polyBody->CreateFixture(&FixtureDef);

for (int i=0; i < num_verts; i++) {
    // Convert from b2Vec2 to CCPoint and from physics units to pixels
    m_verts[i] = ccp(verts[i].x*PTM_RATIO, verts[i].y*PTM_RATIO);
}
m_numVerts = num_verts;
delete verts;
}


-(void) setColor:(ccColor4F) color {
m_color = color;
}

-(BOOL) dirty {
return true;
}

-(void) draw {
//[super draw];
ccDrawPoly(m_verts, m_numVerts, YES);

CCLOG(@"Drawing?");
}

-(CGAffineTransform) nodeToParentTransform {
b2Vec2 pos  = m_polyBody->GetPosition();

float x = pos.x * PTM_RATIO;
float y = pos.y * PTM_RATIO;

/*if ( ignoreAnchorPointForPosition_ ) {
    x += anchorPointInPixels_.x;
    y += anchorPointInPixels_.y;
}*/

// Make matrix
float radians = m_polyBody->GetAngle();
float c = cosf(radians);
float s = sinf(radians);

if( ! CGPointEqualToPoint(anchorPointInPixels_, CGPointZero) ){
    x += c*-anchorPointInPixels_.x + -s*-anchorPointInPixels_.y;
    y += s*-anchorPointInPixels_.x + c*-anchorPointInPixels_.y;
}

// Rot, Translate Matrix
transform_ = CGAffineTransformMake( c,  s,
                                   -s,  c,
                                   x,   y );

return transform_;
}

間にいくつかのものがありますが、それほど重要ではありません。頼まれたら載せます。次に、私のゲーム シーン クラスに基づく update 関数です。

-(void)updateObstacles
{
//CCLOG(@"updating obstacles");
int xpos;
int ypos;
CGPoint pos;
for (int i=0; i<MAX_OBSTACLES; i++ ) {
    // If there is no obstacle generate a new one
    if ( obstacles[i] == NULL ) {
        polyObstacleSprite *sprite = [[polyObstacleSprite alloc] init];
        ypos = int(_winSize.width/2*(double)arc4random()/ARC4RANDOM_MAX) -   _winSize.width/2;
        xpos = int(_winSize.height/2*(double)arc4random()/ARC4RANDOM_MAX) - _winSize.height/2;
        //CCLOG(@"generating obstacle at %d,%d", xpos, ypos);
        pos = ccp(xpos, ypos);
        [sprite genBody:_world pos:&pos];
        [self addChild:sprite z:1];
        obstacles[i] = sprite;
    }
    //CCLOG(@"position: %d, %d", obstacles[i]->screen, obstacles[i]->position.y); FINISH
}
}

混乱の種で申し訳ありませんが、これを簡単に設定しましたが、私がやりたいことのほとんどは、キャラクターが重力で下に移動するときに、ランダムに生成されたポリゴンを iPhone 画面の下部に表示することです。ポリゴン以外はすべて機能しています。これを読むために時間を割いていただきありがとうございます。

4

0 に答える 0