所以,這裏是我的2D點類的代碼旋轉:雪碧旋轉偏移不會保留在它所屬的位置。 (SDL)
float nx = (x * cos(angle)) - (y * sin(angle));
float ny = (y * cos(angle)) + (x * sin(angle));
x = nx;
y = ny;
x和y是在點類的局部變量。
這裏是我的精靈類的旋轉代碼:
//Make clip
SDL_Rect clip;
clip.w = width;
clip.h = height;
clip.x = (width * _frameX) + (sep * (_frameX) + osX);
clip.y = (height * _frameY) + (sep * (_frameY) + osY);
//Make a rotated image
col bgColor = image->format->colorkey;
//Surfaces
img *toEdit = newImage(clip.w, clip.h);
img *toDraw = 0;
//Copy the source into the workspace
drawRect(0, 0, toEdit->w, toEdit->h, toEdit, bgColor);
drawImage(0, 0, image, toEdit, &clip);
//Edit the image
toDraw = SPG_Transform(toEdit, bgColor, angle, xScale, yScale, SPG_NONE);
SDL_SetColorKey(toDraw, SDL_SRCCOLORKEY, bgColor);
//Find new origin and offset by pivot
2DVec *pivot = new xyVec(pvX, pvY);
pivot->rotate(angle);
//Draw and remove the finished image
drawImage(_x - pivot->x - (toDraw->w/2), _y - pivot->y - (toDraw->h/2), toDraw, _destination);
//Delete stuff
deleteImage(toEdit);
delete pivot;
deleteImage(toDraw);
代碼使用精靈的起源中心。如果我將樞軸放在(0,0)處,它可以正常工作,但如果我將它移動到其他地方,例如角色的肩膀,它會開始讓Sprite像Spirograph一樣旋轉,而不是在樞軸上停留在人物的肩膀。
圖像旋轉函數來自SPriG,它是一個用於在SDL中繪製圖元和變換圖像的庫。由於樞軸來自圖像的中心,我認爲由旋轉產生的修剪表面的新尺寸應該不重要。
[編輯] 我弄亂了代碼。通過減慢速度,我發現,由於某種原因,矢量旋轉的速度比圖像快60倍,儘管我沒有將任何東西乘以60.因此,我試圖將輸入除以60,直到現在,它是出現所有生澀,不旋轉之間的任何60倍數之間的任何東西。
我發現在這個網站上的矢量旋轉代碼,人們一再證實它的工作原理,爲什麼它只是以60爲增量旋轉?