   Controlling Polygon Rotation
  For the last few days , I've been trying to write some poly rotation code which is almost done,except that I'm having trouble controlling how it rotates.the polygon data structure:struct vertex{float x,y;};struct polygon{int vertnum; //number of verticesvertex *v;   //vertex list}poly;In this Program...i made a square centred on (320,240)The main event loop: void render(){   //some code drawpoly(buffer,ddsd.lPitch,poly);if(KEYDOWN(VK_LEFT))   theta-=1;if(KEYDOWN(VK_RIGHT))   theta+=1;if(theta>360)theta = 0;if(theta<0)theta=360;rotatepoly(poly,theta*3.14159/180);    //some code}And the rotate Function:void rotatepoly(polygon &p,float ang){float xr,yr;int i;translatepoly(p,-320,-240); //simply translate entire poly to the originfor(i=0;i<p.vertnum;i++){xr = (p.v.x)*cos(ang)-(p.v.y)*sin(ang);//store result in yr = (p.v.y)*cos(ang)+(p.v.x)*sin(ang);//temp varsp.v.x = xr;//put them backp.v.y = yr;}translatepoly(p,320,240); //translate poly back to its center}Problem 1)If i press left/right just once..the polygon continues to rotate(even after letting go of the keys)I fixed problem 1 using this(which seems kind of inefficient)void rotatepoly(polygon &p,float ang){float xr,yr;int i;static float oldang = 0; if(oldang==theta) return;//if the angle is the same..dont rotate   translatepoly(p,-320,-240); //simply translate entire polyfor(i=0;i<p.vertnum;i++){xr = (p.v.x)*cos(ang)-(p.v.y)*sin(ang);//store result in yr = (p.v.y)*cos(ang)+(p.v.x)*sin(ang);//temp varsp.v.x = xr;//put them backp.v.y = yr;}oldang=theta;translatepoly(p,320,240);}Problem 2)If the left/right key is pressed for some time,the speed of the rotation keeps on increasing..and after that slows down while ROTATING IN THE DIRECTION OPPOSITE to which it had started.. Also suppose that i rotated it some x degrees anticlockwise......and now try to do it clockwise.What i get is that the poly still goes some degrees anticlockwise before going clockwise..and that too with increased speedPlease Help
  The poly rotation is not slowing down and changing direction. Its getting so fast that it is turning more than a half rotation in a frame. How do you suppose you may represent a poly spinning at a scrillion times per second? You could suggest it with some blurring effects, but the computer will not do that automatically! It is drawing a poly that has been rotated by a certain amount - on any given frame, it will only be drawing a poly, regardless of how much you have just rotated it.It's easy to visualize the problem. If the poly is not rotating, it will look the same every frame. From one frame to the next, its rotation has not changed. If you rotate it by a small amount each frame, you will see it rotate as the frames flick by.But what if you rotate it a full 360 degrees between each frame? The actual orientation of the poly will be the same. It has rotated a full circle, yet it is back where it started. How would you see this rotation when it is running? You just can't, because the properties of the poly at the instant the frame is rendered are identical to the last frame, regardless of how many calculations have just taken place.If you were rotating by 359 degrees per frame, that would be exactly the same as rotating by -1 degree per frame. So it would appear to be going backwards. The only ways to work around this side effect of persistence of vision are to (1) apply effects, or (2) don't let things rotate that fast!   ;Quote:Original post by ManpreetProblem 1)If i press left/right just once..the polygon continues to rotate(even after letting go of the keys)That's because you're calling rotatepoly() every frame.Suppose that initially the value of theta is zero. Then the call to rotatepoly() does nothing, and the polygon doesn't rotate.Then you press a key and theta changes, for example to 5. Now each call to rotatepoly() will rotate the polygon by 5 degrees, and since rotatepoly() is called every frame, the polygon will rotate constantly.If you later press the key again, increasing theta to, for example 10, the polygon will rotate faster, because now it is rotated by 10 degrees each frame, not 5.There are basically two ways to fix this:1) call rotatepoly() only when you actually want to rotate the polygon:   if(KEYDOWN(VK_LEFT)) {   theta-=1; rotatepoly(poly,theta*3.14159/180);   }if(KEYDOWN(VK_RIGHT)) {   theta+=1; rotatepoly(poly,theta*3.14159/180);   }but this is not a good solution because constantly transforming the polygon vertices will eventually introduce round-off errors.2) Store 2 sets of vertices: the original vertices and the transformed vertices, and each time apply the transformation to the original vertices (which remain intact). This way you will not suffer from round-off errors and your transformations will not have this cummulative effect.   ;  Hi,Thanks for the informative reply...that really helped .So what i did was implement your 2nd idea and instead of blindly just incrementing the angle..i set it to a constant +/- value depending on which key was pressed.And Presto! it worked...rotating the poly at a constant rate every frameThank you very muchEDITED -oh didnt see your post gage64..and yes now im using what you suggested in method 2Thanks!   