我头痛了好几个小时,试图弄清楚我的旋转算法出了什么问题,最后我发现 java 对此类计算的解释存在问题:
float playerCenterX = 248.0f;
float playerCenterY = 248.0f;
playerCenterX = (float)((playerCenterX - (float)512) * Math.cos(angle) - (playerCenterY - (float)300) * Math.sin(angle) + (float)512);
playerCenterY = (float)((playerCenterX - (float)512) * Math.sin(angle) + (playerCenterY - (float)300) * Math.cos(angle) + (float)300);
上面的代码给出了真正意想不到的结果,而不是围绕 (512, 300) 旋转指定角度的点,而是给我形成无穷大符号的坐标(角度从 0 到 2PI)。
现在这样一个看似不重要且无关紧要的微小更改修复了上面的代码:
float playerCenterX = 248.0f;
float playerCenterY = 248.0f;
float playerCenterX2 = (float)((playerCenterX - (float)512) * Math.cos(angle) - (playerCenterY - (float)300) * Math.sin(angle) + (float)512);
float playerCenterY2 = (float)((playerCenterX - (float)512) * Math.sin(angle) + (playerCenterY - (float)300) * Math.cos(angle) + (float)300);
因此,将计算结果添加到方程右侧未使用的新变量可以解决问题。现在...有人可以解释一下这里发生了什么吗?
最佳答案
当playerCenterY计算时,它使用修改后的playerCenterX。在第二个版本中,您使用原始值。
关于Java奇怪的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24650996/