旋转后Android获取新坐标 [英] Android get new coordinates after rotation

查看:635
本文介绍了旋转后Android获取新坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发简单的游戏.我有cca. 50个矩形,分为10列和5行.放置它们以适合整个屏幕并不是什么问题.但是,当我旋转画布时,假设角度约为7°,则旧坐标不适合新的坐标位置.在构造函数中,我已经创建并定义了这些矩形的位置,在onDraw方法中,我正在绘制此矩形(当然还有旋转的区域),我需要一些与当前矩形发生冲突的方法.我尝试使用类似的方法(我绕着屏幕的中心点旋转了

Im developing simple game. I have cca. 50 rectangles arranged in 10 columns and 5 rows. It wasn't problem to put them somehow to fit the whole screen. But when I rotate the canvas, let's say about 7° angle, the old coordinates does't fit in the new position of the coordinates. In constructor I already create and define the position of that rectangles, in onDraw method I'm drawing this rectangles (of course there are aready rotated) bud I need some method that colliding with the current rectangle. I tried to use something like this (i did rotation around the center point of the screen)

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 是屏幕的旋转角度.

but it doesn't works (becuase it gives me absolute wrong new coordinates). x and y are coordinates of the touch that I'm trying to calculate new position in manner of rotation. ROTATE_ANGLE is the angle of rotation the screen.

有人知道如何解决这个问题吗,我已经遍历了许多文章,Wiki,wolframalpha类别,但还没有走运.也许我只需要一些链接即可了解更多问题.

Does anybody know how to solve this problem, I already go thorough many articles, wiki, wolframalpha categories but not luck. Maybe I just need some link to understand problem more.

谢谢

推荐答案

您使用旋转矩阵.

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

这篇关于旋转后Android获取新坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆