1

我的游戏世界空间是 4 个屏幕尺寸(2 行 x 2 列)的矩形,我正在根据球的运动移动相机。球的位置是根据物理引擎设置的。我注意到,有时当相机跟随球移动时,球会稍微改变方向。

最初我认为这是一种基于相机运动的错觉,但当我画一条路径时,这条线有时是弯曲的。我使用了 CCFollow 实现代码背后的一些代码(不是 CCFollow 本身,而是它背后的代码)来跟踪球(我遇到了 CCfollow 的问题)

下面是我的 Tick() 函数: ————————————————— - (void)tick:(ccTime) dt {

bool ballMov = false;
CGPoint pos;
CCSprite *ballData;

dt = 1.0 / 60.0f;
world->Step(dt,8, 3 );

for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
        CCSprite *tempData = (CCSprite *)b->GetUserData();

        if (tempData == ball){
            ballData = tempData;
            ballMov = true;
            ballData.position = ccp(b->GetPosition().x * PTM_RATIO,    b->GetPosition().y * PTM_RATIO);
        }    
    }
}


//
//  Move the layer so we can keep looking at the travelling ball
//  Code is similar to CCFollow code with some adjustments
pos = ballPosition;    
if(ballMov){ 

    #define CLAMP(x,y,z) MIN(MAX(x,y),z)
    pos = ccp(CLAMP(pos.x,leftBoundary,rightBoundary), CLAMP(pos.y,bottomBoundary,topBoundary));     
    #undef CLAMP

    pos = ccpSub(pos, halfScreenSize );
    pos.x = - pos.x ;
    pos.y = - pos.y ;

    //calculate delta move towards the new position
    if(gunShot){
        CGPoint moveVect;
        CGPoint oldPos = [self position];
        double dist = ccpDistance(pos, oldPos);
        if (dist > 1){
            moveVect = ccpMult(ccpSub(pos,oldPos),0.4); //0.05 is the smooth constant.
            oldPos = ccpAdd(oldPos, moveVect);
            pos = oldPos;        
        }
    }    
}

//move towards the ball 
[self setPosition:(pos)];

lastPosition = pos;
ballPosition = CGPointMake([ball body]->GetPosition().x * PTM_RATIO, [ball body]->GetPosition().y * PTM_RATIO );   

//if ball is out of gameworld space then reset 
if (!CGRectContainsPoint ( outerBoundaryRect, ballPosition)){              
    ball.visible = false ;  
    [self recreateBody];       //Ball is out of Gameworld, recreate it at the default position 
}    

}

4

0 回答 0