使精灵旋转顺畅

问题描述:

我有我要平稳地转动,面对相反方向的精灵:使精灵旋转顺畅

这是我的渲染功能:

public void render() { 

    Gdx.gl.glClearColor(0.7f, 0.7f, 0.2f, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    spriteBatch.begin();   

    if(bug.getX() >= (int)(Gdx.graphics.getWidth() - 100) && bug.getY() >= (int)(Gdx.graphics.getHeight() - 100)){ 
     bug.rotate(rotDeg); 
     turn = !turn; 
    } 
    else if(bug.getX() <= 0 && bug.getY() <= 0){ 
     bug.rotate(rotDeg); 
     turn = !turn; 
    } 

    if(!turn){   
     bug.translate(v.x * Gdx.graphics.getDeltaTime(), v.y * Gdx.graphics.getDeltaTime()); 
    } 
    else{ 
     bug.translate(-(v.x * Gdx.graphics.getDeltaTime()), -(v.y * Gdx.graphics.getDeltaTime())); 
    } 


    bug.draw(spriteBatch); 
    spriteBatch.end(); 
} 

什么我的代码做的是,当它到达目的地(右上角或左下角),它立即转向相反的方向。我将如何顺利完成这一转变?

我认为“顺利”意味着你的转向不会在到达边界时直接翻转,而是应该直接停止并加速一段时间。

因此使用三个浮点值而不是rotDegcurrentRotation,rotationStepmaxRotation。有了它们,您可以建模一个平滑的加速度:

您在每个呈现调用中调用bug.rotate(currentRotation)。到达边界时,设置currentRotation = 0。现在每当您拨打bug.rotate()增加currentRotationrotationStep只要currentRotation < maxRotation。您必须根据您的应用程序速度搜索适当的值。

如果您想让您的bug稍微淡出屏幕,意味着不立即停止旋转,请使用另一个标志,指示在|currentRotation| < maxRotation的情况下旋转速度是递增还是递减。根据此标志,可以通过rotationStep增加或减少currentrotation。这会让边界感觉像使用者的橡皮,精灵淡入,停止并平滑加速。