碰撞后cocos2d和花栗鼠旋转物体

时间:2011-01-05 16:20:16

标签: objective-c cocos2d-iphone chipmunk

喜 我对两个框架都很陌生。但也许有人可以指出我正确的方向:

基本上我试图弹起一个形状的球。 (工作良好) 但是当球旋转时也会很棒

这是我的(复制和粘贴)代码

// BallLayer.m

#import "BallLayer.h"

void updateShape(void* ptr, void* unused){
    cpShape* shape = (cpShape*)ptr;
    Sprite* sprite = shape->data;
    if(sprite){
        cpBody* body = shape->body;
        [sprite setPosition:cpv(body->p.x, body->p.y)];
    }
}

@implementation BallLayer
-(void)tick:(ccTime)dt{
    cpSpaceStep(space, 1.0f/60.0f);
    cpSpaceHashEach(space->activeShapes, &updateShape, nil);
}

-(void)setupChipmunk{
    cpInitChipmunk();
    space = cpSpaceNew();
    space->gravity = cpv(0,-2000);
    space->elasticIterations = 1;
    [self schedule: @selector(tick:) interval: 1.0f/60.0f];

    cpBody* ballBody = cpBodyNew(200.0, cpMomentForCircle(100.0, 10, 10, cpvzero));
    ballBody->p = cpv(150, 400);
    cpSpaceAddBody(space, ballBody);
    cpShape* ballShape = cpCircleShapeNew(ballBody, 20.0, cpvzero);

    ballShape->e = 0.8;
    ballShape->u = 0.8;
    ballShape->data = ballSprite;
    ballShape->collision_type = 1;
    cpSpaceAddShape(space, ballShape);

    cpBody* floorBody = cpBodyNew(INFINITY, INFINITY);
    floorBody->p = cpv(0, 0);
    cpShape* floorShape = cpSegmentShapeNew(floorBody, cpv(0,0), cpv(320,160), 0);
    floorShape->e = 0.5;
    floorShape->u = 0.1;
    floorShape->collision_type = 0;
    cpSpaceAddStaticShape(space, floorShape);

    floorShape = cpSegmentShapeNew(floorBody, cpv(0,200), cpv(320,0), 0);

    cpSpaceAddStaticShape(space, floorShape);   
}

-(id)init{
    self = [super init];
    if(nil != self){
        ballSprite = [Sprite spriteWithFile:@"ball2.png"];
        [ballSprite setPosition:CGPointMake(150, 400)];
        [self add:ballSprite];

        [self setupChipmunk];
    }
    return self;
}
@end

请帮帮我。

1 个答案:

答案 0 :(得分:0)

当我决定发布它时,我找到了解决方案:)

void updateShape(void* ptr, void* unused)
{
    cpShape* shape = (cpShape*)ptr;
    Sprite* sprite = shape->data;
    if(sprite){
        cpBody* body = shape->body;
        [sprite setPosition:cpv(body->p.x, body->p.y)];
        [sprite setRotation: (float) CC_RADIANS_TO_DEGREES( -body->a )]; 
    }
}