有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使精灵平稳旋转

我有一个精灵,我想平稳地旋转,面向相反的方向:

这是我的渲染功能:

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();
}

我的代码所做的是,当它到达目的地(右上角或左下角)时,它会立即转向相反的方向。我怎样才能顺利转弯


共 (1) 个答案

  1. # 1 楼答案

    我假设“平稳”对你来说意味着转弯方向在到达边界时不会直接翻转,而是应该直接停止,并在一段时间内加速

    因此,使用三个浮点值代替rotDegcurrentRotationrotationStepmaxRotation。使用它们,您可以模拟平滑的加速度:

    在每次渲染调用中调用bug.rotate(currentRotation)。到达边界时,设置currentRotation = 0。现在每次你调用bug.rotate()增加currentRotation,只要currentRotation < maxRotation。您必须根据应用程序的速度为它们搜索合适的值

    如果你想让你的bug在屏幕上淡出一点,也就是说不要立即停止旋转,可以使用另一个标志,指示在|currentRotation| < maxRotation的情况下旋转速度是增加还是减少。取决于此标志,按rotationStep递增或递减currentrotation。这会让边框对用户来说像橡胶一样,精灵会淡入、停止并平稳加速