创建Box2D体的数组

时间:2012-01-03 10:06:23

标签: iphone cocos2d-iphone box2d

我有一个球,它是一个box2d的身体,并在屏幕上移动...我想创建多个相似类型的球,他们也应该在他们自己之间碰撞..

我现在使用的代码是

ballcount = [[levelData objectForKey:@"ballcount"]intValue] ;

ballarray = [[NSMutableArray arrayWithCapacity:ballcount]init] ;

for (int j=0; j<ballcount; j++) {
ball = [CCSprite spriteWithFile:@"ball.png"];
[ballarray insertObject:ball atIndex:j];
[self createBallBoundingBox:(CCSprite *)[ballarray objectAtIndex:j]];
[[ballarray objectAtIndex:j]setPosition:ccp(arc4random() % 480 , arc4random() % 320)];  

[self addChild:[ballarray objectAtIndex:j]];
}

运行时屏幕上只出现一个精灵?任何建议我做错了什么......只有一个球在那里时效果很好

由于

好吧我有上面的代码可以工作,但现在我在tick方法中的运动有问题... box2d形状正在移动,但CCSprite形状没有附加到box2d主体这里是我的代码< / p>

world->Step(dt, 10, 10);
    for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {

        if (b->GetUserData() != NULL) {

            CCSprite *sprite = (CCSprite *)b->GetUserData();


            if ([sprite isKindOfClass:[Ball class]]) 
            {

                b2Vec2 Vel = b->GetLinearVelocity();
                float32 angle =  atan2(Vel.y, Vel.x);
                angle += -M_PI/2;

                b->SetTransform(b->GetPosition(),angle);

                sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
                                      b->GetPosition().y * PTM_RATIO);

                sprite.rotation  = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());

            }   

好的家伙是我的createBoundingBox定义

-(void)createBallBoundingBox:(Ball *)ballSprite{
b2BodyDef BallBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(ballSprite.position.x/PTM_RATIO, ballSprite.position.y/PTM_RATIO);
ballBodyDef.userData = ballSprite;
ballBody = world->CreateBody(&ballBodyDef);


/// test circle shape on ballbody

b2CircleShape BallCircleShape;

BallCircleShape.m_radius = 10/PTM_RATIO;

// Create shape
/*
b2PolygonShape ballShape;
int num = 7;
b2Vec2 verts[] = {
    b2Vec2(0.0f / PTM_RATIO, 19.2f / PTM_RATIO),
    b2Vec2(-10.7f / PTM_RATIO, 15.2f / PTM_RATIO),
    b2Vec2(-6.7f / PTM_RATIO, -3.2f / PTM_RATIO),
    b2Vec2(-1.7f / PTM_RATIO, -18.0f / PTM_RATIO),
    b2Vec2(7.7f / PTM_RATIO, 0.5f / PTM_RATIO),
    b2Vec2(10.5f / PTM_RATIO, 14.7f / PTM_RATIO),
    b2Vec2(0.2f / PTM_RATIO, 19.0f / PTM_RATIO)     
};

ballShape.Set(verts,num);
 */
// Create shape definition and add to body

b2FixtureDef ballFixtureDef;
ballFixtureDef.shape = &ballCircleShape;
ballFixtureDef.density = 1.0f;  
ballFixtureDef.friction = 0.0f;
ballFixtureDef.restitution = 1.0f;
ballFixture = ballBody->CreateFixture(&ballFixtureDef);

b2Vec2 direction(5,2);
direction.Normalize();
float force = 1.0f;

b2Vec2 position  = ballBody->GetPosition();

//Apply linear velocity 

ballBody->ApplyLinearImpulse(force*direction,ballBody->GetPosition());

b2Vec2 Vel = ballBody->GetLinearVelocity();
float32 angle =  atan2(Vel.y, Vel.x);
angle += -M_PI/2;

ballBody->SetTransform(ballBody->GetPosition(),angle);

}

任何想法?让我知道

非常感谢你们

1 个答案:

答案 0 :(得分:0)

在你的方法中

[self createBallBoundingBox:(CCSprite *)[ballarray objectAtIndex:j]];
//OR NOW
[self createMiceBoundingBox:tempmice];

您是否忘记将Ball作为UserData附加到您的b2Body

- (void) createMiceBoundingBox: (Ball*) ball
{
    //...
    b2Body body = ...
    body.SetUserData(ball);
    //..
}

如果没有向我们展示方法的代码

编辑:如果您使用Mice而不是Ball,请确保将tick方法更新为

if ([sprite isKindOfClass:[Mice class]])