创建 Box2D 主体数组

发布于 2024-12-24 02:26:05 字数 3119 浏览 0 评论 0原文

我有一个球,它是一个 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]];
}

只有一个精灵出现在当我运行它时的屏幕?有什么建议我做错了什么...当只有一个球存在时它工作得很好

谢谢

,好的,我已经有了上面的代码,但现在我在勾选方法中的移动遇到了问题..box2d 形状正在移动但是 CCSprite 形状没有附加到 box2d 主体上,这是我的代码

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);

}

有什么想法吗?让我知道

非常感谢大家

i have a ball which is a box2d body and moves around the screen... i want to create multiple balls of similar type and they should also collide among them self..

the code i am using now is

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]];
}

There is only one sprite appearing on the screen when i run it ? any suggestions what am i doing wrong ... it works perfectly when only one ball is there

Thanks

ok i have got the above code to work but now i am having a problem with the movement in the tick method .. the box2d shapes are moving but the CCSprite shapes are not getting attached to the box2d bodies here is my code

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

            }   

Ok guys here is my createBoundingBox definition

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

}

any ideas ? Let me know

Thanks a lot guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-12-31 02:26:05

在您的方法内部,

[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,请确保将勾选方法更新为

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

inside your method

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

did you forgot to attach the Ball as UserData to your b2Body

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

if not show us the code of the method

EDIT: if you use Mice instead of Ball make sure you update tick method to

if ([sprite isKindOfClass:[Mice class]]) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文