Page 2 of 3

Re: rotation question

Posted: Sun Dec 17, 2017 5:49 pm
by josephjohn
mr_happy wrote: Sun Dec 17, 2017 5:26 pm atan2 takes (y, x)
I know, that's why im inpitting tryY - y and tryX - x

Re: rotation question

Posted: Sun Dec 17, 2017 5:54 pm
by mr_happy
Here you go, quick and dirty:

Code: Select all


function love.load()

  love.mouse.setVisible(false)
  lAngle = -90
  dAngle = 0
end



function love.draw()

  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.ellipse("fill", mX, mY, 4, 4)
  love.graphics.ellipse("fill", 400, 200, 10, 10)

  love.graphics.setColor(255, 0, 0, 255)
  lX = 400 + 100 * math.cos(math.rad(lAngle))
  lY = 200 + 100 * math.sin(math.rad(lAngle))
  love.graphics.line(400, 200,  lX, lY)


  at2 = math.deg(math.atan2(mY-200, mX-400))
  love.graphics.print(at2 , mX, mY)

  if lAngle < at2 and mY < 200 then
    dAngle = 0.5
  else
    dAngle = -0.5
  end

  lAngle = lAngle + dAngle

end

function love.update(dt)
  mX, mY = love.mouse.getPosition()
end


Re: rotation question

Posted: Sun Dec 17, 2017 6:00 pm
by josephjohn
mr_happy wrote: Sun Dec 17, 2017 5:54 pm Here you go, quick and dirty:

Code: Select all


function love.load()

  love.mouse.setVisible(false)
  lAngle = -90
  dAngle = 0
end



function love.draw()

  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.ellipse("fill", mX, mY, 4, 4)
  love.graphics.ellipse("fill", 400, 200, 10, 10)

  love.graphics.setColor(255, 0, 0, 255)
  lX = 400 + 100 * math.cos(math.rad(lAngle))
  lY = 200 + 100 * math.sin(math.rad(lAngle))
  love.graphics.line(400, 200,  lX, lY)


  at2 = math.deg(math.atan2(mY-200, mX-400))
  love.graphics.print(at2 , mX, mY)

  if lAngle < at2 and mY < 200 then
    dAngle = 0.5
  else
    dAngle = -0.5
  end

  lAngle = lAngle + dAngle

end

function love.update(dt)
  mX, mY = love.mouse.getPosition()
end

But the problem I still there :\

Wait until the line is at about 1 o'clock, and then put the curser at about 4 o'clock. Instead of going the shortest route(1, 2, 3, 4), the line spins around(1, 12, 11, 10, 9, 8, 7, 6, 5, 4).

Re: rotation question

Posted: Sun Dec 17, 2017 6:11 pm
by mr_happy
I did type this in blind... Um, just look at the logic at the end of love.draw() , its in that area!

Re: rotation question

Posted: Sun Dec 17, 2017 6:40 pm
by Ref
Something like this?
Ops, wrong file.

Re: rotation question

Posted: Sun Dec 17, 2017 6:48 pm
by Ref
Something like this?

Re: rotation question

Posted: Sun Dec 17, 2017 8:50 pm
by mr_happy
Now at computer with love installed. This does the trick (but still quick and dirty)...

Code: Select all



function love.load()

  love.mouse.setVisible(false)
  lAngle = 90
  dAngle = 0
end


function love.draw()

  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.ellipse("fill", mX, mY, 4, 4)
  love.graphics.ellipse("fill", 400, 200, 10, 10)

  love.graphics.setColor(255, 0, 0, 255)
  lX = 400 + 100 * math.cos(math.rad(lAngle))
  lY = 200 + 100 * math.sin(math.rad(lAngle))
  love.graphics.line(400, 200,  lX, lY)


  at2 = math.deg(math.atan2(mY-200, mX-400))
  diff = lAngle -at2
  if diff > 360 then diff = diff - 360 end
  love.graphics.print(diff , mX, mY)


  if diff > 180 or diff < 0 then 
    dAngle = 0.5
  else
    dAngle = -0.5
  end

  lAngle = lAngle + dAngle

end


function love.update(dt)
  mX, mY = love.mouse.getPosition()
end

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

Re: rotation question

Posted: Sun Dec 17, 2017 8:53 pm
by mr_happy
And you did have params wrong way round in your code:

io.write(i, ' : ', math.atan2(x, y), '\t')

Re: rotation question

Posted: Sun Dec 17, 2017 9:11 pm
by mr_happy
EDIT Still not right (lol) missed another check, shoudl be ok now:

Code: Select all



function love.load()

  love.mouse.setVisible(false)
  lAngle = 90
  dAngle = 0
end


function love.draw()

  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.ellipse("fill", mX, mY, 4, 4)
  love.graphics.ellipse("fill", 400, 200, 10, 10)

  love.graphics.setColor(255, 0, 0, 255)
  lX = 400 + 100 * math.cos(math.rad(lAngle))
  lY = 200 + 100 * math.sin(math.rad(lAngle))
  love.graphics.line(400, 200,  lX, lY)


  at2 = math.deg(math.atan2(mY-200, mX-400))
  diff = lAngle -at2
  if diff > 360 then diff = diff - 360 end
  if diff < -180 then diff = diff + 360 end
  love.graphics.print(diff , mX, mY)


  if diff >= 180 or diff < 0 then 
    dAngle = 0.5
  else
    dAngle = -0.5
  end

  lAngle = lAngle + dAngle

end


function love.update(dt)
  mX, mY = love.mouse.getPosition()
end

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

Re: rotation question

Posted: Sun Dec 17, 2017 10:11 pm
by josephjohn
Thanks, works now!