Slick Forums

Discuss the Slick 2D Library
It is currently Sat Dec 15, 2018 6:56 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Sat Jun 25, 2016 4:29 am 
Offline
User avatar

Joined: Sat Jun 25, 2016 3:58 am
Posts: 3
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).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group