About these rotations

Started by
0 comments, last by Craazer 21 years, 4 months ago
Hi I kinda have desing crisis becose im looking for good and darn fast rotation algorithm, and my current archiment doesnt please me at all. I've got two ways. Way one - my way
      
hheight = height/2;
hwidth = width/2;
for(int y = 0; y < height; ++y)
{
 for(int x = 0; x < width; ++x)
 {

    xr = (y-hheight) * sin(angle) + (x-hwidth) * cos(angle);
    yr = (y-hheight) * cos(angle)- (x-hwidth) * sin(angle);
    
    xr += hwidth;
    yr += hheight;
	
    col = source_ptr[int(xr + yr * lpitch)];

    back_buffer[(x + y * back_lpitch)] = col;

 } // end x

} // end y


// I had to modify this so hope no typos

      
Then theres this second option so called corner rotation, wich S1CA showed me some time a go. here is hes tutorial: 0,0 10,10 A \_ \_P \_ \ B 20,20 Consider a line which goes from point A to point B with the origin at 0,0. You can describe the slope of the line. Think of when you did graphs at school - you want to describe the ratio that Y changes in relation to X (or vice versa): Bx - Ax ratio = --------- By - Ay Do that for a diagonal line like the above and you'll get a ratio of 1, i.e. Y increases by one for every X Following on from that, any point P which falls between A and B can be found with: P = A + (B-A) * t Where t is a value between 0 and 1. So for the line above, using 0.5 for t gets us 15,15, i.e. 50% along the line: Px = Ax + (Bx-Ax) * t Py = Ay + (By-Ay) * t 0.25 gets us 25% along the line, 1.0 gets us to point B and 0.0 gets us point A. Apply that to your sprite: A------B | | | | C------D It has four corner points: A,B,C,D Rotate each of A, B, C, D You can find all of the points inbetween A and B with the interpolation above. You can find all of the points between B and D with the interpolation above, you can do the same for all edges of the triangle. The next step is to make the interpolation bi-linear (at the moment it finds one point along a line, it won't find points which don't fall on that line so won't get you what's inside the the rectangle. To do that you simply make a new line from interpolated positions and interpolate along that: P1 A---+--B | | | | Q+ | | | | C---+--D P2 i.e. you interpolate between A and B to find P1, then you interpolate between C and D to find P2. You can then interpolate to find values on the line between P1 and P2. To find the value of Q above using only A, B, C and D, knowing that Q falls say 75% across the sprite (X) and 50% down (Y): P1x = Ax + (Bx-Ax) * 0.75 P1y = Ay + (By-Ay) * 0.75 P2x = Cx + (Dx-Cx) * 0.75 P2y = Cy + (Dy-Cy) * 0.75 Qx = P1x + (P2x-P1x) * 0.75 Qy = P1y + (P2y-P1y) * 0.75 That can be optimised in plenty of ways, and can be achieved in other ways. Look up any reference for affine texture mapping for more methods. The big point though is you only have to use the expensive sin() and cos() functions on four points A,B,C,D and you can find the rest with multiplication, addition and subtraction. Since for texture mapping type applications you'll usually be interpolating in a fixed order, you can take advantage of that too and reduce the calculation further. End S1CAs tutor. Ok i some how understand that i idea but i never couldt make a working code, i dont know how to calculate the corners. for example if i have sprite 50x50 is this then the correct set for corners? Ax=0 Ay=0 Bx=50 By=0 Cx=0 Cy=50 Dx=50 Dy=50 however i didnt find any way to continue from that, how should those values change? (ok now its comfirmed im stupid) Ok lastly the MAIN QUESTON and help whit the corner rotation. Is my(not actualy my math) algorithm better than the second one? If not could u S1CA or anybody show me the idea of looping whit that one? Btw dont flame me i only got pair of socks and underwear for christmas present [edited by - Craazer on December 25, 2002 5:34:29 PM]
Advertisement
I just wanted to have some conversations about rotation becose its important part of dynamic graphics.

I better post this to graphics forum if no one replies here.

This topic is closed to new replies.

Advertisement