[java] Rotation about a point

Started by
6 comments, last by Wrathnut 23 years, 7 months ago
I have what seems to be a farily simple question.. although I spent all weekend digging through my calc books to no avail.... Anyway here goes: Does anyone know a formula that allows you to rotate a point about another arbitrary point?(in 2d) I wrote an algorithm, but it only works at points close to zero... Also, Does anyone know how to rotate a point about another arbitrary point in 3d? War doesn't determine who is right, war determines who is left.
Advertisement
ok, i did this once. let me see if i can remember


int x1,y1,x2,y2; //x1,y1 is axis x2,y2 is point u are trying to move


float dist=sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );


float angle=atan( (y2-y1)/(x2-x1) );

//u need to get the real angle from this since there are 4 angles in a circle that will give any one tangent


find what cuadrent x2,y2 are in if x1,y1 is the origin then use the angle from atan and other stuff to get angle

angle=the angle is now the correct angle from x1,y1 to x2,y2;

angle+=rotation_angle;//how far u want to rotate it


//get back into x,y coords from polar
x2=cos(angle)*dist;
y2=sin(angle)*dist;


done, hope this helps

Infested Furby Software

infestedfurby@hotmail.com
infestedfurby.cjb.net
Infested Furbyinfestedfurby@hotmail.cominfestedfurby.cjb.net
You can do rotations simply as a matrix multiplication. Do not use atan and sqrt and other slow functions.

I just lent out my book but I recommend reading a book on graphics theory. I can recommend the very classic and very excellent book

[Foley] Foley, J., van Dam, A, Feiner, S. & Hughes, J. F.: Computer Graphics. Principles and Practice. 2nd Edition in C. Addison-Wesley. 1996.

After reading this you will know everything you want to know in this subject. And if you need more advanced topics you can read

Watt, Alan H., and Mark Watt, Advanced Animation and Rendering Techniques,
Addison-Wesley, 1992.

From here you will have to read SIGGRAPH articles to know more.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
All you need to do is rotate it around 0, and then translate that rotation to the arbitrary point that you mentioned earlier... Make sense? It is really quite logical when you think about it

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-Chris Bennett of Dwarfsoft
"The Philosophers' Stone of Programming Alchemy"
IOL (The list formerly known as NPCAI) - A GDNet production
Our Doc - The future of RPGs
Thanks to all the goblins over in our little Game Design Corner niche
Java2D gives you a really easy way to do translation, rotation and scaling with its AffineTransform construct. Makes it almost trivial. Java3D gives you similar capabilities.

Here''s the Java2D Tutorial, for starters:
http://java.sun.com/docs/books/tutorial/2d/index.html

I highly, highly recommend using Java2D for 2D graphics in Java applications.
I am a Jedi, like my father before me
Hey guy's, Thanks for all of the replies to this post. I procurred a linear algebra book from one of my engineering teachcers the other day and found this equation:

X' = x*cos(theta) - y*sin(theta)
Y' = x*sin(theta) + y*cos(theta)

where theta is the angle that you wish to rotate the point.
and in 2d x and y are exactly that, your x and y coordinates.
X' and Y' are the rotated coordinates.


This function works great for rotating a point about the origin, but I need to translate the functions so that the origin is centered around my arbitrary point, like dwarsoft said.

I guess my porblem is how do I do this.. I spent all night last night reading through all of my old calc books on shifting a function but that doesn't seem to work.

SteveMiester, ^_^ I would be doing everything in java2d and java3d, except I would like everything to be cross compatible. Besides that I think it would really help if I had an understanding of these functions.

Any help would be much appreciated!

War doesn't determine who is right, war determines who is left.

Edited by - Wrathnut on August 29, 2000 2:45:26 PM
Its actually pretty simple, and you''ll probably kick yourself when you see the solution for not realizing it earlier.

You have a point (x1, y1) that you want to rotate by angle theta around another point (x2, y2).

First translate both points by (-x2,-y2). This places your rotational axis point at (0,0), but keeps the relative locations the same.

Now do the rotation as you stated:

x1'' = x1*cos(theta) - y1*sin(theta)
y1'' = x1*sin(theta) + y1*cos(theta)

Now translate both points by (x2, y2). The rotational axis point is back where it started, and the point you wanted to rotate is now rotated.

Of course, if you think about it, you really only need to translate & reverse translate the first point (x1,y1), and not both of them since translating the rotational axis point will always give you (0,0). This whole thing can be simplified down to

x1'' = ( (x1-x2)*cos(theta) - (y1-y2)*sin(theta) ) + x2
y1'' = ( (x1-x2)*sin(theta) + (y1-y2)*cos(theta) ) + y2
x1 = x1''
y1 = y1''

Of course should ideally optimize this so that you do less calculations (especially the sin and cos).
^_^, hehe you are right Lord Kronos, I should kick myself. Thanks alot for your insight though. I tried the could and it works great! Thanks again for eveyones help, you guys are the best.

War doesn't determine who is right, war determines who is left.

This topic is closed to new replies.

Advertisement