Sign in to follow this  
The_Nerd

Please help me with a very quick rotation function

Recommended Posts

I have a Polygon class in my program that stores a list of vertecies. It is very simple. I then have open gl draw it via glBegin(GL_POLYGON). Anyhow... Down to the point. I need to make my own rotation function to rotate the vertecies of the polygon by a certain amount. I need this to be very quick. I know how to do it with some trig, but the problem with that is that I would have to get the distance from the center to each vertex, which will require a sqrt call, and that is to slow. glRotate would be fine if it wasn't for the fact that I have to do collision testing on the polygon(s). Any help, pointers, webpages, tuts, anything of that sort will be very welcome. Thanks in advance.

Share this post


Link to post
Share on other sites
I'm not sure i'm understanding you properly but if you're going to rotate this polygon around a center, won't the distance from the center to each vertex be allways the same? Can't you just calculate it once?

Share this post


Link to post
Share on other sites
Well, I'm inexperienced with 3D and OpenGL, but I've always moved the polygon to the origin, done all transformations there, and then moved it back to where it was initially.

HTH,
nilkn

Share this post


Link to post
Share on other sites
Ah! No one got my question again... Man I am bad at explaining things (I really am... sorry! :) ).

No! The distance ISN'T always the same. I have a polygon, can be any shape, have any amount of vertecies, and I already told you I can't use the OpenGL rotate function because I need the actual positions of the vertecies to be rotated around the polygons center point!

Share this post


Link to post
Share on other sites
You don't need the distance from the center to the vertices. A standard equation to rotate a point about the origin is:

x' = cos(angle)x-sin(angle)y
y' = sin(angle)x+cos(angle)y

If the polygon is positioned such that its center is not at the origin, you will have to move it to the origin, rotate it, and move it back. For this reason, polygons, meshes, and other objects used for rendering and collision detection are usually stored with the origin as their center, and then translated to position them in the world. So to orient and position an object in the world, you first rotate it, and then translate it. This can be combined into a matrix-vector multiplication for efficiency and convenience.

Share this post


Link to post
Share on other sites
Thanks jyk!!! But in the process I got it working with my vector class as well... I sped up my vector class quite a bit (length function anyhow). At least I think. Aren't sin and cos faster than sqrt? Here is the code:


inline double Length()
{
if (this->dX == 0) return fabs(this->dY);
if (this->dY == 0) return fabs(this->dX);
double dTan = atan(this->dY / this->dX) * ENG_TODEG;

return fabs(dX / cos(dTan * ENG_TORAD));
}


That used to be


return sqrt(dX * dX + dY * dY);


Don't you think this would be faster?

Share this post


Link to post
Share on other sites
jyk... Just to let you know your method is faster by about 5 fps... I am doing my timing calculations by doing 65000 rotations every frame, I get about 6-7 fps with my way, and 12 with yours! Thanks again bud!

By the way, just to let everyone know, my above method for getting the length of the vector IS NOT faster than sqrt(...).

:)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this