0
votes

I want to build a platform game with cocos2d/Box2D. I use CCFollow to follow the player sprite but CCFollow constantly puts it in the center of the screen. I want CCFollow to follow more naturally, like a human turning a camcorder with an acceptable lag, a small overshoot ...etc.

Here is a method of mine that didn't work: I attached (via a distance joint) a small physics body to the player that doesn't collide with anything, represented by a transparent sprite. I CCFollow'ed the transparent sprite. I was hoping this ghost body would act like a balloon attached to the player, hence a smooth shift in view. The problem is distance joint breaks with too heavy - too light objects. The balloon moves around randomly, and of course, it pulls the player back a little no matter how light it is.

What is a better way of following a moving sprite smoothly?

2

2 Answers

2
votes

Try add this to CCActions in cocos2d libs.

-(void) step:(ccTime) dt
{
#define CLAMP(x,y,z) MIN(MAX(x,y),z)

    CGPoint pos;
    if(boundarySet)
    {
        // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased
        if(boundaryFullyCovered) return;

        CGPoint tempPos = ccpSub(halfScreenSize, followedNode_.position);
        pos = ccp(CLAMP(tempPos.x,leftBoundary,rightBoundary), CLAMP(tempPos.y,bottomBoundary,topBoundary));
    }
    else {
        // pos = ccpSub( halfScreenSize, followedNode_.position );
        CCNode *n = (CCNode*)target_;
        float s = n.scale;
        pos = ccpSub( halfScreenSize, followedNode_.position );
        pos.x *= s;
        pos.y *= s;
    }

    CGPoint moveVect;

    CGPoint oldPos = [target_ position];
    double dist = ccpDistance(pos, oldPos);
    if (dist > 1){
        moveVect = ccpMult(ccpSub(pos,oldPos),0.05); //0.05 is the smooth constant.
        oldPos = ccpAdd(oldPos, moveVect);
        [target_ setPosition:oldPos];
    }

#undef CLAMP

}

i get this from cocos2d forums.

0
votes

Perhaps this http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_ease can help you get an "acceleration" effect with CCFollow.