开发者

Growing circles in cocos2d and chipmunk

开发者 https://www.devze.com 2023-04-11 22:23 出处:网络
I\'m trying to create a growing circle in chipmunk, that starts growing when you touch and hold your finger at a location. I haven\'t found any specific help function for that in chipmunk, wondering i

I'm trying to create a growing circle in chipmunk, that starts growing when you touch and hold your finger at a location. I haven't found any specific help function for that in chipmunk, wondering if anyone has any advice, tip or tricks for how to do that.

One way would be to create a circle a little bigger than the previous one and destroy the old one each update circle. Maybe there is a easier way, anyone has any thoughts?

Thanks

UPDATE: At the moment I using the following method:

In my actionLayer class:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLocation = [touch locationInView: [touch view]];
    CGPoint circleOrigin = [[CCDirector sharedDirector] convertToGL: touchLocation];

    spriteObject = [[[CPHatchimal alloc] initWithSpace:space atLocation:circleOrigin] autorelease];
    [sceneSpriteBatchNode addChild:spriteObject z:2];

    return YES;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    [spriteObject setGrowCircle:NO];
}

In my "spriteObject" class:

-(void) update:(ccTime)deltaTime {
    if (growCircle) {
        CGPoint location = ccp(circleOrigin.x, circleOrigin.y);
        cpFloat r = ((cpCircleShape*) shape)->r;
        r++;

        NSString *spritName = [self getCurrentSpriteName];
        [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteF开发者_运维百科rameCache] 
                           spriteFrameByName:spritName]];

        [self setScale:(0.01 * r)];

        cpSpaceRemoveStaticShape(space, shape);
        cpShapeFree(shape);


        body = cpBodyNewStatic();
        body->p = location;

        shape = cpCircleShapeNew(body, radius, cpvzero);
        shape->e = e;
        shape->u = u;
        shape->collision_type = collisionType;
        shape->data = self;

        cpSpaceAddStaticShape(space, shape);   

    }
}

At the moment I have pretty good fps (about 60), so I guess this solution is ok..


https://github.com/slembcke/Chipmunk-Physics/blob/master/include/chipmunk/chipmunk_unsafe.h

You have to import this header separately as I don't normally encourage people to use it. Make sure you understand that Chipmunk has no way to understand how the surface is moving or changing and the collisions will be mushy because of it.

0

精彩评论

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