开发者

cocos2d and chipmunk rotate object after collision

hi im pretty new to both frameworks. but maybe someone can point me into the right direction:

basically i try to bounce a ball of a shape. (works fine) but it would be great when the ball would rotate, too

here is my (copy & paste) code

// BallLayer.m

#import "BallLayer.h"

void updateShape(void* ptr, void* unused){
    cpShape* shape = (cpShape*)ptr;
  开发者_StackOverflow  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

please help me out.


well when i decided psoting it i found the solution :)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜