2013-03-05 91 views
0

所以,我正在研究一個2d塔防遊戲,並且需要敵人的精靈在他們改變方向時進行旋轉。我知道這可以使用內置的Java旋轉/仿射變換方法輕鬆完成,但爲了優化/性能,我想知道是否可以用像素數組來旋轉。旋轉像素陣列

感謝

+0

重複http://stackoverflow.com/questions/2799755/rotate-array-clockwise – 2013-03-05 03:40:00

+0

這不是一個2d數組。我正在使用一維數組來保存像素數據。該帖子的評論似乎與我的問題毫無關係。 – GayLord 2013-03-05 03:54:30

+0

我的不好!你能舉一個例子輸入和輸出嗎?如果你只是從像[1,2,3,4,5]到[3,4,5,1,2]這樣的數組旋轉,那麼這是一個容易解決的問題。請讓我知道你在找什麼。 – 2013-08-04 17:05:56

回答

0

蓋洛德, 我不知道,如果你還在這裏尋找一個答案,但對其他成員的緣故,這裏是我能夠用它來解決類似的問題。我一直在構建一個遊戲引擎,它利用了與你所描述的相似的圖形結構。我使用的旋轉功能是這樣的: 所有的花車都可以用雙打替換,而且可能更好。我使用浮點數來幫助低端計算機的性能。

float cos = (float)Math.cos(-angle);//The angle you are rotating (in radians) 
float sin = (float)Math.sin(-angle);//The angle you are rotating (in radians) 
float sWidth = screenWidth/2; 
float sHeight = screenHeight/2; 
float pWidth = pixelArrayWidth/2; 
float pHeight = pixelArrayHeight/2; 

for each pixel in screen{ 
    int xPosition = pixelIndexX - sWidth; 
    int yPosition = pixelIndexY - sHeight; 
    int tx = (int)(xPosition * cos - yPosition * sin); 
    int ty = (int)(xPosition * sin + yPosition * cos); 
    xPosition = tx + pWidth; 
    yPosition = ty + pHeight; 
    if((xPosition is out of bounds) or (yPosition is out of bounds)) continue; 
    setPixelAt(screenPixelX, screenPixelY, pixelArray[xPosition + yPosition * pixelArrayWidth]) 
} 

我知道它與psuedo代碼和java代碼是拼湊的,但它應該是可以理解的。如果任何人需要澄清,只需發表評論並詢問。