问题:我的玩家模型应该转向最后一次鼠标点击的方向,但它不是缓慢转动,而是以所有可能的方式旋转(游戏有等距视图,模型应该只绕 Y 轴旋转,但它会旋转也围绕 X 轴和 Z 轴)。
以下方法(在 render() 中调用)负责模型的转动行为:
public static void turnUnit(){
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
mX = Gdx.input.getX();
mY = Gdx.input.getY();
angle = (float) (180+Math.atan2(mX-Gdx.graphics.getWidth()/2, mY-Gdx.graphics.getHeight()/2)*(180/Math.PI));
newAngle = ((((currentAngle - angle) % 360) + 540) % 360) - 180;
turning = newAngle/60*turnRate;
currentAngle = currentAngle-turning;
}
TestGround.player.transform.setToRotation(Vector3.Y, currentAngle).setTranslation(posX,0,posZ);
}
和运动方法(也称为渲染()):
public static void movement(){
if(northM==true){
TestGround.player.transform.trn(0,0,-1f);
}
if(southM==true){
TestGround.player.transform.trn(0,0,1f);
}
if(westM==true){
TestGround.player.transform.trn(-1f,0,0);
}
if(eastM==true){
TestGround.player.transform.trn(1f,0,0);
}
posX = TestGround.player.transform.getTranslation(Vector3.X).x;
posY = TestGround.player.transform.getTranslation(Vector3.Y).y;
posZ = TestGround.player.transform.getTranslation(Vector3.Z).z;
}
试图在最后一行使用“旋转”,但它只是旋转得更快。
此外,即使这对我来说毫无意义,但经过一些测试后,似乎运动方法会以某种方式干扰转向方法(沿某个方向移动会以某种方式旋转模型)。
我在这里做一些根本错误的事情吗?
附加信息:
- 最初我使用简单的轮询来获取所有键盘和鼠标输入
- 以一种大方法计算运动/旋转,一切正常
- 决定使用 libgdx 的 inputprocessor 使代码更具可读性和开放性