In animating a ball we have 2 ways to do it. One is to make sprites for it that can be used to represent a moving object OR the lesser stupid, more obvious way is to rotate an image. But rotation usually means around a point and contrary to popular belief the computers are not smart and can’t read brains so by default they don’t rotate it around the center of the circle. Thankfully we humans came up with offset value. Where we can offset the rotation by a certain x distance and y distance.
Here’s what’s given in the wiki as explanation for the love.graphics.draw() function:
Synopsis
love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy )
Arguments
Drawable drawable
A drawable object.
number x
The position to draw the object (x-axis).
number y
The position to draw the object (y-axis).
number r
(0)Orientation (radians).
number sx
(1)Scale factor (x-axis). Can be negative.
number sy
(sx)Scale factor (y-axis). Can be negative.
number ox
(0)Origin offset (x-axis). (A value of 20 would effectively move your drawable object 20 pixels to the left.)
number oy
(0)Origin offset (y-axis). (A value of 20 would effectively move your drawable object 20 pixels up.)
Returns
Nothing.
Note
love.graphics.draw() anchors from the top left corner by default.
Now the oy and ox are the ones that you need to take care about. If you can get the exact value that you need to increase the offset with, you can actually get it to rotate about it’s own axis passing through the center of the circle.
--[[
Author: Pronoy Chopra
Topic : Image rotation on its own axis
]]--
local angle = 0
function love.load( ... )
-- body
ball = {}
ball.img = love.graphics.newImage("images/ball_play.png")
ball.height = ball.img:getHeight()
ball.width = ball.img:getWidth()
end
function love.update( ... )
-- body
if love.keyboard.isDown('d') then
angle = angle + math.pi * 0.02
elseif love.keyboard.isDown('a') then
angle = angle - math.pi * 0.02
end
end
function love.draw( ... )
-- body
love.graphics.setColor(255,255,255,255)
love.graphics.draw(ball.img,ball.x,ball.y,angle,1,1,ball.width/2,ball.height/2)
end
Now, pressing the keys a and d on the keyboard will make it rotate left and right respectively. If you need to increase the speed of rotation simply change the value 0.02 to something bigger. That’s it for now, see you soon.
Source:
love2d.org