Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
end

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

function love.draw()
    love.graphics.draw(sb)
end

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.

share|improve this question
1  
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. –  RobCurr Mar 21 '13 at 21:17
    
I hadn't thought of that, but will try it out when I get home. Thanks for the suggestion. I'll report back soon. –  C. Griffin Mar 21 '13 at 22:03
    
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! –  C. Griffin Mar 21 '13 at 23:26

1 Answer 1

up vote 1 down vote accepted

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 ;)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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