# Help with 2D Rotation

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Yah... I'm trying to figure out how to do 2D rotation for mah gaem, so I searched this site. Here are some equations that I found:
newx = x*cos(f) - y*sin(f)
newy = y*cos(f) + x*sin(f)
--or--
px = sin(angle) * distance from origin + ox
py = cos(angle) * distance from origin + oy

The problem with these is that they only painted half of the image. Not wanting to come here and cry about it, I wrote my code. Its very messy and slow; it uses a lot more trigonometry than those ^ do. Here it is in its entirety. The problem that I have with it is at the very center (when the angles whould be close to 0, 90, etc.), the thing starts to shift things. And there are random black holes. But I think those are because of rounding.

double xCenter = src->w / 2;
double yCenter = src->h / 2;
double originalAngle;
double newAngle;
double totalDistance;
double oldXDist;
double oldYDist;
double newXDist;
double newYDist;
int newXPt, newYPt;

for(int i = src->w ; i > src->w / 2 - 1; i--){ // we go at the topright corner, cuz there the unit circle is zero and tan is positive
for(int j = 0; j < src->h / 2 + 1; j++){

oldXDist = i - xCenter;
oldYDist = j - yCenter;
totalDistance = sqrt(oldXDist * oldXDist + oldYDist * oldYDist);  // slow probably
if(totalDistance > yCenter){ // the y or x centers are also "radii" of the circle we are turning

// the area is outside of the turnable circle
// do nothing, this is here mainly for the else

}//end if
else{ // it is smaller, so it WILL be moved

if(oldXDist == 0 && oldYDist < 0) originalAngle = PI / 2; // 90 degrees
else if(oldXDist == 0 && oldYDist >= 0) originalAngle = 3 * PI / 2;
else originalAngle = atan( oldYDist / oldXDist);   // replace this with acos or asin cuz we know the hypotynuse
newAngle = originalAngle + (degrees * DEG2RAD) ;
newXDist = cos(newAngle) * totalDistance; // totaldistance is like the hypotynuse
newYDist = sin(newAngle) * totalDistance;

bufpSrc = (Uint16 *)src->pixels + j * src->pitch / 2 + i;
newXPt = (int)(xCenter + newXDist);
newYPt = (int)(yCenter + newYDist);
bufpDest = (Uint16 *)dest->pixels + newYPt * dest->pitch / 2 + newXPt;
*bufpDest = *bufpSrc;

bufpSrc = (Uint16 *)src->pixels + (src->h - j) * src->pitch / 2 + i;
newYPt = (int)(xCenter + newXDist);
newXPt = (int)(yCenter - newYDist);
bufpDest = (Uint16 *)dest->pixels + newYPt * dest->pitch / 2 + newXPt;
*bufpDest = *bufpSrc;

bufpSrc = (Uint16 *)src->pixels + (src->h - j) * src->pitch / 2 + (src->w - i) ;
newXPt = (int)(xCenter - newXDist);
newYPt = (int)(yCenter - newYDist);
bufpDest = (Uint16 *)dest->pixels + newYPt * dest->pitch / 2 + newXPt;
*bufpDest = *bufpSrc;

bufpSrc = (Uint16 *)src->pixels + j * src->pitch / 2 + (src->w - i) ;
newYPt = (int)(xCenter - newXDist);
newXPt = (int)(yCenter + newYDist);
bufpDest = (Uint16 *)dest->pixels + newYPt * dest->pitch / 2 + newXPt;
*bufpDest = *bufpSrc;

}//end else

} // end j loop
} // i loop



##### Share on other sites
Here are two pics to help describe the problem:

http://www.freewebs.com/myyellowdart3/dest at 0 degrees.jpg

See how the things get weirded?

http://www.freewebs.com/myyellowdart3/dest at around.jpg

See how they remain in the same spot? Makes no sense to this camper.

##### Share on other sites
Nevermind. I figured it out.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 16
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632928
• Total Posts
3009268
• ### Who's Online (See full list)

There are no registered users currently online

×