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.
josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

Re: rotation question

Post 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
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post 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

josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

Re: rotation question

Post 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).
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post 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!
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: rotation question

Post by Ref »

Something like this?
Ops, wrong file.
Attachments
turret.png
turret.png (65.67 KiB) Viewed 4692 times
Last edited by Ref on Sun Dec 17, 2017 6:49 pm, edited 1 time in total.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: rotation question

Post by Ref »

Something like this?
Attachments
turret.love
(66.16 KiB) Downloaded 75 times
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post 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
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post by mr_happy »

And you did have params wrong way round in your code:

io.write(i, ' : ', math.atan2(x, y), '\t')
User avatar
mr_happy
Citizen
Posts: 84
Joined: Fri Mar 18, 2016 8:57 pm

Re: rotation question

Post 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
josephjohn
Prole
Posts: 10
Joined: Sat Dec 16, 2017 8:34 pm

Re: rotation question

Post by josephjohn »

Thanks, works now!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], whatevers and 39 guests