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
yScale to a 1 to see the rotation work properly. Thanks in advance for any suggestions you have.