rotation question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 68 guests