0
votes

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);

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.

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.

Thank you

2

2 Answers

3
votes

You use a rotation matrix.

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
-1
votes

Ok, find the solution.

First it is important to convert from angle to radian

Then I personly need to negate that radian value.

That's all, this solution is correct