试图以恒定的速度平滑地旋转我的精灵。但它会降低速度

时间:2011-08-23 11:17:50

标签: objective-c math cocos2d-iphone rotation angle

(我正在使用cocos2d for iphone)

首先我会向你解释我的代码:(重要的部分)

 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch * touch = [touches anyObject];

    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position)));

    plane.dstAngle = dstAngle;
     }
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position)));

    plane.dstAngle = dstAngle; }

如果我触摸屏幕,我会计算我的平面和触摸位置之间的角度。

现在我设置了飞机的“目的地”角度。 我的“飞机”是一个子类。

在我的头文件中,我有以下浮点值:

//this is in my plane subclass
float diffAngle_; 
float startAngle_;
float dstAngle_;
float smoothRotationSpeed_;

编辑:在我的图层初始化中,我将“smoothRotationSpeed”设置为一个值。调度“rotationTick”方法。 我有一个方法来旋转平面(也在平面子类中)

-(void)rotateTick:(ccTime)dt {

    startAngle_ = [self rotation];
    if (startAngle_ > 0)
        startAngle_ = fmodf(startAngle_, 360.0f);
    else
        startAngle_ = fmodf(startAngle_, -360.0f);

    diffAngle_ =dstAngle_ - startAngle_;
    if (diffAngle_ > 180)
        diffAngle_ -= 360;
    if (diffAngle_ < -180)
        diffAngle_ += 360;

    [self setRotation: startAngle_ + diffAngle_ * dt]; //smooth rotate

}
-(void)setSmoothRotationSpeed:(float)smoothRotationSpeed {
    [self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed
}

现在的问题是,如果我触摸一个位置,精灵正确地旋转到它,但是一开始它旋转的速度有点快,而且它的速度会慢下来。

距目标角度越近,移动越慢......

但我希望它以恒定的速度移动。这个“常量”速度在“smoothRotationSpeed”值中定义,但我如何在我的代码中实现它呢?

如果我将间隔设置为“smoothRotationSpeed”值,它将不会平滑。它会滞后。

有人愿意帮我吗?

1 个答案:

答案 0 :(得分:2)

您正在根据目标角度和当前角度之间的差异递增精灵的旋转,因此当前角度与目标角度越接近,您应用于当前旋转的增量越小。但是从你的说法来看,那不是你想要的。所以你可以简单地做这样的事情:

-(void)rotateTick:(ccTime)dt {

    startAngle_ = [self rotation];
    if (startAngle_ > 0)
        startAngle_ = fmodf(startAngle_, 360.0f);
    else
        startAngle_ = fmodf(startAngle_, -360.0f);

    diffAngle_ =dstAngle_ - startAngle_;
    if (diffAngle_ > 180)
        diffAngle_ -= 360;
    if (diffAngle_ < -180)
        diffAngle_ += 360;

    if ( fabs(diffAngle_) < smoothRotationSpeed_*dt ) // finished rotation
    {
        [self setRotation: dstAngle_]; // go to the end position
    }
    else if (diffAngle_ > 0)
    {
        [self setRotation: startAngle_ + smoothRotationSpeed_* dt]; //smooth rotate
    }
    else
    {
        [self setRotation: startAngle_ - smoothRotationSpeed_* dt]; //smooth rotate
    }
}
-(void)setSmoothRotationSpeed:(float)smoothRotationSpeed
{
    smoothRotationSpeed_ = smoothRotationSpeed;
    [self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed
}