Re: Smoothly rotate to face the cursor
Posted: Thu Nov 06, 2014 3:08 am
Insert this code in update function:
I use HUMP Vectors for stored self position (self.pos)
70 - rotation speed
Code: Select all
local mousePos = vector( love.mouse.getX(), love.mouse.getY() )
local dir = self.pos - mousePos
local targetAngle = -math.atan2( dir.x, dir.y ) / (math.pi / 180)
local curAngle = self.top_angle
if curAngle < 0 then
curAngle = curAngle + 360
end
if targetAngle < 0 then
targetAngle = targetAngle + 360
end
local a = curAngle - targetAngle
if a > 180 then
a = a - 360
elseif a < -180 then
a = a + 360
end
local s = dt * 70
if a >= -s - 0.5 and a <= s + 0.5 then
self.top_angle = targetAngle
elseif a > s + 0.5 then
self.top_angle = self.top_angle - s
elseif a < -s - 0.5 then
self.top_angle = self.top_angle + s
end
Code: Select all
local s = dt * 70