I'm working on a falling blocks type of game with triangular pieces. I'm using a SpriteBatch to accumulate and remember the pieces and their locations.

The problem is that when I rotate a scaled Image of the triangle, I get unexpected results, that is the origin of the sprite rotation is wrong -- but if I rotate a full-size image (no scaling) it works fine. The sprite should rotate from the center of the image (x + xScale * imgWidth / 2) and (y + yScale * imgWidth / 2). When xScale and yScale == 1, everything is fine. But as soon as it scales down to a fraction of the original size, the origin seemingly freaks out to weird sizes. Here's some code to inspect:

function love.load()
    img = love.graphics.newImage("triangle.png")
    sb = love.graphics.newSpriteBatch(img, 1024)
    imgWidth = img:getWidth()
    imgHeight = img:getHeight()
    x = 250
    y = 250
    xScale = 0.4
    yScale = 0.4
    xOrig = (xScale * imgWidth / 2)
    yOrig = (yScale * imgHeight / 2)
    angleDirection = 0
    id = sb:add(x, y, math.rad(angleDirection), xScale, yScale, xOrig, yOrig, 0, 0)

function love.update(dt)
    if love.keyboard.isDown(" ") then
        angleDirection = angleDirection + 180
        if angleDirection >= 360 then
            angleDirection = 0
    sb:set(id, x, y, math.rad(angleDirection), xScale, yScale, xOrig, yOrig, 0, 0)

function love.draw()

Now, to see the problem in action, run the preceding code. Change the xScale and yScale to a 1 to see the rotation work properly. Thanks in advance for any suggestions you have.

  • 1
    \$\begingroup\$ Have you tried not scaling the origin of the texture? It may be internally scaling it already in which case you are double doing it. \$\endgroup\$
    – RobCurr
    Mar 21 '13 at 21:17
  • \$\begingroup\$ I hadn't thought of that, but will try it out when I get home. Thanks for the suggestion. I'll report back soon. \$\endgroup\$
    – C. Griffin
    Mar 21 '13 at 22:03
  • \$\begingroup\$ That actually worked! I was overthinking it, and for some reason it never dawned on me to not scale the origin. If you "answer" my question, I can boost your rep by voting your answer as correct. Thanks again! \$\endgroup\$
    – C. Griffin
    Mar 21 '13 at 23:26

When setting the origin of a scaled image, use the original dimensions of the image instead of the final scaled size. For instance to set the origin to the center of a 200x200 image, you would use 100x100 even if it is scaled as the 'add' and 'set' functionality scale those values internally.

Glad I could help ;)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.