花栗鼠碰撞麻烦

时间:2011-04-12 17:27:06

标签: c++ objective-c cocos2d-iphone

我正在开发iPhone游戏,使用cocos2d和chipmunk。所以我从模板创建了应用程序,并修改了HelloWorldScene代码,以便在关卡中几乎没有静态形状。所以,一切都很好。通过触摸创建新的动态形状,碰撞很好。 但是当我将代码从addNewSprite转移到另一个类(创建cpBody,cpShape和CCSprite)时出现了麻烦。在随机的时间(我没有任何依赖任何东西)没有检测到碰撞,所以我的物体要么通过静态物体并从场景飞行或者只是消失。 因此,当形状在同一个类中创建时,进行模拟时,一切都很好。但是当他们处于不同的阶层时 - 会发生随机错误。

这是代码的工作变体:

-(void) addNewSpriteX: (float)x y:(float)y
{
    CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [self getChildByTag:kTagBatchNode];

    CCSprite *sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(0, 0, 50, 46)];
    [batch addChild: sprite];

    sprite.position = ccp(x,y);

    int num = 4;
    CGPoint verts[] = {
        ccp(-25,-20),
        ccp(-25, 20),
        ccp( 25, 20),
        ccp( 25,-20),
    };

    cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));
    cpBodySetMass(body, 10);

    // TIP:
    // since v0.7.1 you can assign CGPoint to chipmunk instead of cpVect.
    // cpVect == CGPoint
    body->p = ccp(x, y);
    cpSpaceAddBody(mSpace, body);

    cpShape* shape = cpPolyShapeNew(body, num, verts, CGPointZero);
    shape->e = 0.5f; shape->u = 0.5f;
    shape->data = sprite;
    cpSpaceAddShape(mSpace, shape);
}

static void
eachShape(void *ptr, void* unused)
{
    cpShape *shape = (cpShape*) ptr;
    CCSprite *sprite = (CCSprite *)shape->data;
    if( sprite ) {
        cpBody *body = shape->body;

        // TIP: cocos2d and chipmunk uses the same struct to store it's position
        // chipmunk uses: cpVect, and cocos2d uses CGPoint but in reality the are the same
        // since v0.7.1 you can mix them if you want.       
        [sprite setPosition: body->p];
        NSLog(@"x:%f, y:%f", body->p.x, body->p.y);

        [sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )];
    }
}

这是我的变体,不起作用:

-(void) addNewSpriteX: (float)x y:(float)y
{
    AtomDesc ad;
    ad.mass = 10;
    PolyShapeDesc *psd = (PolyShapeDesc *)malloc(sizeof(PolyShapeDesc));
    psd->vertNum = 4;
    psd->vertices = (CGPoint *)malloc(sizeof(CGPoint)*4);
    psd->vertices[0] = ccp(-25,-20);
    psd->vertices[1] = ccp(-25, 20);
    psd->vertices[2] = ccp( 25, 20);
    psd->vertices[3] = ccp( 25,-20);    
    SpriteDesc *sd = (SpriteDesc *)malloc(sizeof(SpriteDesc));
    sd->filename = [NSString stringWithString:@"poof.png"];
    sd->pos = CGPointMake(x, y);
    sd->rotation = 0;
    sd->scale = 1;
    ad.shapeDesc = psd;
    ad.spriteDesc = sd;
    ad.pos = CGPointMake(x, y);
    Atom *a = [[Atom alloc] initAtomWithDescriptor:&(ad) :mSpace];
    atoms.push_back(a);
    [self addChild:a];
    free(psd->vertices);
    free(psd);
    free(sd);
}

static void
eachShape(void *ptr, void* unused)
{
    cpShape *shape = (cpShape*) ptr;
    if(shape->data && [shape->data isKindOfClass:[Atom class]]) 
    {
        Atom *a = (Atom *)shape->data;
        [a update:0];       
    } 
}


@implementation Atom

- (id) initAtomWithDescriptor:(AtomDesc *)descriptor :(cpSpace *)space
{
    if( (self=[super init]))
    {       
        mSpace = space;
        PolyShapeDesc *psd;
        cpShape *shape;
        psd = descriptor->shapeDesc;

        mBody = cpBodyNew(1.0f, cpMomentForPoly(1.0f, psd->vertNum, psd->vertices, CGPointZero));
        cpBodySetMass(mBody, descriptor->mass);
        mBody->p = descriptor->pos;
        cpSpaceAddBody(mSpace, mBody);

        shape = cpPolyShapeNew(mBody, psd->vertNum, psd->vertices, psd->pos);
        shape->u = psd->friction;
        shape->e = psd->elasticity;
        shape->data = self;
        cpSpaceAddShape(mSpace, shape);


        SpriteDesc *sd;
        sd = descriptor->spriteDesc;
        mSprite = [CCSprite spriteWithFile:sd->filename];
        mSprite.position = sd->pos;
        mSprite.rotation = sd->rotation;
        mSprite.scale = sd->scale;
        [self addChild:mSprite];
    }

    return self;
}

- (bool)update:(float)dt
{
    [mSprite setPosition: mBody->p];
    [mSprite setRotation: (float) CC_RADIANS_TO_DEGREES( -mBody->a )];
    return true;
}

所以,你对这种奇怪的行为有什么看法吗?

0 个答案:

没有答案