Page 3 of 3

Re: rotation question

Posted: Mon Dec 18, 2017 8:59 am
by mr_happy
Tidied that up a little in case anyone else finds it useful:

Code: Select all

local LINELENGTH = 40
local CENTREX, CENTREY = 400, 200
local TURNSPEED = 80
  
local lineAngle = 90

function love.draw()
  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.ellipse("fill", CENTREX, CENTREY, 10, 10)
  love.graphics.setColor(255, 0, 0, 255)
  local lineEndX = CENTREX + LINELENGTH * math.cos(math.rad(lineAngle))
  local lineEndY = CENTREY + LINELENGTH * math.sin(math.rad(lineAngle))
  love.graphics.line(CENTREX, CENTREY,  lineEndX, lineEndY)
end

function love.update(dt)
  
  local mouseX, mouseY = love.mouse.getPosition()
  local at2 = math.deg( math.atan2(mouseY - CENTREY, mouseX -CENTREX ) )
  local deltaAngle = 0
  local diff = lineAngle - at2
  
  if diff > 360 then diff = diff - 360 end
  if diff < -180 then diff = diff + 360 end

  if diff >= 180 or diff < 0 then 
    deltaAngle = TURNSPEED * dt
  else
    deltaAngle = -TURNSPEED * dt
  end

  lineAngle = lineAngle + deltaAngle  

end

function love.keypressed(key)
  if key == 'escape' then love.event.quit() end
end