1

所以我需要帮助弄清楚我在这里缺少什么代码。我已经检查了所有地方,但我需要详细说明它使用的公式还是我还没有注意到的错字。这是多边形类。我正在尝试创建具有 8 个顶点的随机多边形,然后当然填充纯色。但我希望他们继续生成随机位置但保持固定。以更好的方式,多边形是我的地形。好的修改:多边形在那里,我的角色与它们交互,但我看不到它们,是的,它们在同一层上。哦,但它们不会一直在底部生成,我猜我只需要在它们离开屏幕后删除旧的,它应该会生成一个新的多边形。

-(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_;
}

两者之间有一些东西,但它不太重要。如果被问到,我可以发布它。然后是基于我的游戏场景类的更新功能。

-(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