0

So, I am working on a 2d tower defense game and need the enemy's sprite to rotate whenever they change direction. I understand this can be done easily using built-in java rotate/affine transform methods, but for the sake of optimization/performance, I want to know whether it's possible to do the rotation with an array of pixels instead.

thanks

GayLord
  • 135
  • 9
  • Duplicate of http://stackoverflow.com/questions/2799755/rotate-array-clockwise – John Kurlak Mar 05 '13 at 03:40
  • this isn't a 2d array. I am using a 1-dimensional array to hold the pixel data. The comments from that post seems pretty irrelevant to my problem. – GayLord Mar 05 '13 at 03:54
  • My bad! Can you give an example input and output? If you're just doing an array rotation from like: [1, 2, 3, 4, 5] to [3, 4, 5, 1, 2], then that is an easy problem to solve. Just let me know what you're looking for. – John Kurlak Aug 04 '13 at 17:05

1 Answers1

0

GayLord, I'm not sure if you're still looking for an answer here but for the sake of other members, here is what I was able to use to solve a similar problem. I've been building a game engine that utilizes a similar graphical structure to what you are describing. The rotation function I use is this: All of the floats could be replaced by doubles and work perhaps better. I used floats to help with performance on lower end computers.

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] )  
}

I know that it is patchy with psuedo code and java code but it should be understandable. If anyone needs some clarification just comment and ask about it.

draco miner
  • 43
  • 1
  • 1
  • 4