Slick Forums
http://slick.ninjacave.com/forum/

Trouble rotating and scaling images at the same time
http://slick.ninjacave.com/forum/viewtopic.php?f=3&t=8362
Page 1 of 1

Author:  742mph [ Sat Jun 25, 2016 4:29 am ]
Post subject:  Trouble rotating and scaling images at the same time

I'm having trouble drawing images with scales other than 1 and angles of rotation other than 0 degrees. Whenever I do, they appear with strange offsets that seem to have something to do with their centers of rotation, but don't actually center it there.

Here's my code. The image's center of rotation is (centerX, centerY); right and bottom are the image's width minus centerX and its height minus centerY, not adjusted to match the scale; xFlip and yFlip are both false; and angle is in degrees.

Code:
int index = 0;
float xOffset, yOffset;
if (xFlip) {
    index += 1;
    xOffset = Math.round(-right*scale);
} else {
    xOffset = Math.round(-centerX*scale);
}
if (yFlip) {
    index += 2;
    yOffset = Math.round(-bottom*scale);
} else {
    yOffset = Math.round(-centerY*scale);
}
images[index].setRotation((float)angle);
if (flashColor == null) {
    images[index].draw(x + xOffset, y + yOffset, (float)scale);
} else {
    images[index].draw(x + xOffset, y + yOffset, (float)scale, flashColor);
}


I've been testing this code using a 24x24 Mega Man sprite with centerX = 12 and centerY = 24 (in other words, centered between Mega Man's feet).

Image
Image
Image

The first image is the result when I draw two sprites at the same x and y coordinates, one at angle = 0 and the other at angle = 180, with scale = 1, and it looks like I want it to. The second is the same thing when scale = 2, and the third is when scale = 0.5. The center of the red cross is the intended location of the center of rotation.

Does anyone know what's wrong here? I've tried setting xOffset and yOffset to 0, by the way, but the images with scales other than 1 and angles other than 0 still appear in odd places, just different odd places than they do here. Changing the center of rotation also changes the effect, but it only works as expected when the center is at (0,0).

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/