我正在使用当前的鼠标位置来瞄准玩家精灵,但我没有得到最好的结果。我希望我的精灵在光标在屏幕上的任何地方指向方向。这是我想要的结果。 (当我移动鼠标时,旋转速度很慢)
但这就是我得到的:1)当我的鼠标位置y = 0时,喷射精灵旋转非常缓慢(但旋转从不停止)
2)当我的y =(屏幕高度)精灵旋转非常快(并且它也永远不会停止) 这是我的代码:我的更新方法
public void update(float dt){
time += dt;
float yInput = (Gdx.graphics.getHeight() - Gdx.input.getY());
vec.set(Gdx.input.getX() - position.x, yInput - position.y).nor();
//position is a Vector2 update sprite coordinates
position.x += vec.x * 15f;
position.y += vec.y * 15f;
}
这是我的绘制方法
public void draw(){
batch.begin();
sprite.setPosition(position.x - sprite.getWidth()/2, position.y - sprite.getHeight()/2);
float xInput = Gdx.input.getX();
float yInput = (Gdx.graphics.getHeight() - Gdx.input.getY());
float angle = MathUtils.radiansToDegrees * MathUtils.atan2(yInput - position.y, xInput - position.x);
if(angle < 0){
angle += 360;
}
sprite.rotate(angle);
sprite.draw(batch);
batch.end();
}
答案 0 :(得分:2)
在draw
方法中,将sprite.rotate(angle);
替换为sprite.setRotation(angle);
。
rotate()
将相对于当前旋转旋转精灵。 atan2
会返回绝对角度,这可能会导致您的问题。