我正在开发简单的游戏。我有卡。 50 个矩形排列成 10 列 5 行。以某种方式放置它们以适应整个屏幕并不是问题。但是当我旋转 Canvas 时,比如说大约 7° 角度,旧坐标不适合新坐标位置。在构造函数中,我已经创建并定义了该矩形的位置,在 onDraw 方法中,我正在绘制这个矩形(当然有已经旋转的),但我需要一些与当前矩形碰撞的方法。我尝试使用类似的东西(我围绕屏幕中心点旋转)
int newx = (int) ((x * Math.cos(ROTATE_ANGLE) - (y * Math.sin(ROTATE_ANGLE))) + width / 2);
int newy = (int) ((y * Math.cos(ROTATE_ANGLE) + (x * Math.sin(ROTATE_ANGLE))) + height / 2);
但它不起作用(因为它给了我绝对错误的新坐标)。 x 和 y 是我试图以旋转方式计算新位置的触摸坐标。 ROTATE_ANGLE 是屏幕旋转角度。
有谁知道如何解决这个问题,我已经仔细阅读了很多文章、wiki、wolframalpha 类别,但运气不佳。也许我只是需要一些链接来更多地了解问题。
谢谢
最佳答案
您使用旋转矩阵。
Matrix mat = new Matrix(); //mat is identity
mat.postRotate(ROTATE_ANGLE); //mat is a rotation matrix of ROTATE_ANGLE degrees
float point[] = {10.0, 20.0}; //create a new float array representing the point (10, 20)
mat.mapPoints(point); //rotate the point by the requested amount
关于java - Android获取旋转后的新坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24583220/