Please help me with a very quick rotation function

Started by
7 comments, last by The_Nerd 18 years, 10 months ago
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.
-- I waz here --
Advertisement
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?
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
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!

-- I waz here --
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.
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?
-- I waz here --
Heh... Just noticed I am converting to degrees and back to rads... I'll have to change that!

:)
-- I waz here --
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(...).

:)
-- I waz here --
Heh... I sped it up another 8-7 fps by taking out two sin and cos function calls! Yay!
-- I waz here --

This topic is closed to new replies.

Advertisement