I am writing a game. I need to know how to rotate point a around point b by a given number of degrees. I am writing this in java and it is going to be part of my class, Point.
Asked
Active
Viewed 1.4k times
7
-
1Sine and cosine may help: http://en.wikipedia.org/wiki/Rotation_matrix – Niklas B. Mar 18 '14 at 21:13
-
1it is the rotation matrix that does it... – Randy Mar 18 '14 at 21:17
-
Check out http://www.willamette.edu/~gorr/classes/GeneralGraphics/Transforms/transforms2d.htm. Transformation matrices are the tool you need here; you can encapsulate rotation, translation, scaling, etc. – Jason C Mar 18 '14 at 21:19
-
You could take a look at [this example](http://stackoverflow.com/questions/12964983/rotate-image-around-character-java/12971987#12971987) – MadProgrammer Mar 18 '14 at 21:19
1 Answers
18
double x1 = point.x - center.x;
double y1 = point.y - center.y;
double x2 = x1 * Math.cos(angle) - y1 * Math.sin(angle));
double y2 = x1 * Math.sin(angle) + y1 * Math.cos(angle));
point.x = x2 + center.x;
point.y = y2 + center.y;
This approach uses rotation matrices. "point" is your point a, "center" is your point b.
-
-
2@user2277362 Why not [look it up](http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html)? – Niklas B. Mar 18 '14 at 22:18
-
1Math.sin and other trig functions use radians. The Math class also has toDegrees() and toRadians() functions. – SDLeffler Mar 18 '14 at 23:25