很抱歉,如果之前有人问过这个问题,我确实搜索过,但没有找到答案。
我的问题是,我想在相机的 X 和 Y 旋转相关的所有 3 个轴上进行移动。
这就是我所做的:
private static void fly(int addX, int addY){ //parameters are the direction change relative to the current rotation
float angleX = rotation.x + addX; //angle is basically the direction, into which we will be moving(when moving forward this is always the same as our actual rotation, therefore addX and addY would be 0, 0)
float angleY = rotation.y + addY;
float speed = (moveSpeed * 0.0002f) * delta;
float hypotenuse = speed; //the length that is SUPPOSED TO BE moved overall on all 3 axes
/* Y-Z side*/
//Hypotenuse, Adjacent and Opposite side lengths of a triangle on the Y-Z side
//The point where the Hypotenuse and the Adjacent meet is where the player currently is.
//OppYZ is the opposite of this triangle, which is the ammount that should be moved on the Y axis.
//the Adjacent is not used, don't get confused by it. I just put it there, so it looks nicer.
float HypYZ = speed;
float AdjYZ = (float) (HypYZ * Math.cos(Math.toRadians(angleX))); //adjacent is on the Z axis
float OppYZ = (float) (HypYZ * Math.sin(Math.toRadians(angleX))); //opposite is on the Y axis
/* X-Z side*/
//Side lengths of a triangle on the Y-Z side
//The point where the Hypotenuse and the Adjacent meet is where the player currently is.
float HypXZ = speed;
float AdjXZ = (float) (HypXZ * Math.cos(Math.toRadians(angleY))); //on X
float OppXZ = (float) (HypXZ * Math.sin(Math.toRadians(angleY))); //on Z
position.x += AdjXZ;
position.y += OppYZ;
position.z += OppXZ;
}
我只在向前移动(参数:0、90)或向后移动(参数:180、270)时实现此方法,因为横向移动时不会在 Y 轴上发生移动,因为您不会在 Z 轴上旋转。(横着走(扫射)的方法很好用,我就不加了。)
问题是,当我向上看 90 度或向下看 -90 度然后向前移动时,我应该只在 Y 轴(垂直)上移动,但由于某种原因,我也向前移动(这意味着在 Z 轴上,作为 X 轴是扫射)。
我确实意识到这种方式的移动速度不是恒定的。如果您对此有解决方案,我也很乐意接受。