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

3
  • Duplicate of stackoverflow.com/questions/2799755/rotate-array-clockwise Commented Mar 5, 2013 at 3: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. Commented Mar 5, 2013 at 3: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. Commented Aug 4, 2013 at 17:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.