2014-10-18 49 views
0

所以,這裏是我的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爲增量旋轉?

回答

1

我很久沒有碰到SPriG的來源,但我可以給你一些信息。

如果SPriG存在旋轉中心問題,那麼遷移到SDL_gpu可能會更快,更容易(我建議使用SDL 2.0)。這樣你得到一個類似的API,但性能要好得多(它使用圖形卡)。

我可以猜測,矢量不是旋轉比圖像快60倍,而更像是57倍更快!這是因爲你用sin()和cos()來旋轉矢量,它接受以弧度表示的值。圖像正在以度數旋轉一個角度。弧度轉換爲度數爲180/pi,大約爲57. SPriG可以使用度數或弧度,但默認使用度數。使用SPG_EnableRadians(1)切換該行爲。或者,您可以通過將參數乘以sin()和cos()pi/180來堅持角度變量中的度數測量。