Page 1 of 1

How to make an object rotate towards the mouse position.

Posted: Wed Feb 17, 2021 9:22 pm
by PenguinKing
Summary:
Essentially, I've looked up several different ways to look this up and I get the gist of what's going on with the math.atan2 and various other ways of getting the angle to face towards the mouse position. Though; I'm still having a bit of trouble with trying to get the object to face completely towards the mouse and not spin all over the place like it's currently doing right now.

Problem:
The object that I'm trying to get to rotate towards the mouse position spins around rapidly whenever I move the mouse (even a miniscule amount makes it spin out of control.) As you can see in the gif here, I'm using degrees instead of radians with the math.deg() method, however; even that doesn't prevent the spinning for some reason.

Attempted Fixes:
  • Reversing math.atan2 parameters
  • Attempted to make the first parameter of math.atan2 negative.

    Code: Select all

    local y = -(self.y -(love.mouse.getY()/5))
    - ohai
Files:
TowerDefense Files

Re: How to make an object rotate towards the mouse position.

Posted: Wed Feb 17, 2021 11:12 pm
by ohai
I have not looked at your code, but this works for me:

Code: Select all

local dx = game.mouseX - x
local dy = -(game.mouseY - y)
love.graphics.draw(image, x, y, math.atan2(dx, dy), 1, 1, image:getWidth() / 2, image:getHeight() / 2)

Re: How to make an object rotate towards the mouse position.

Posted: Wed Feb 17, 2021 11:23 pm
by PenguinKing
ohai wrote: Wed Feb 17, 2021 11:12 pm I have not looked at your code, but this works for me:

Code: Select all

local dx = game.mouseX - x
local dy = -(game.mouseY - y)
love.graphics.draw(image, x, y, math.atan2(dx, dy), 1, 1, image:getWidth() / 2, image:getHeight() / 2)
I tried your suggestion and it didn't seem to do anything at all. Thanks for trying though! :awesome:

Re: How to make an object rotate towards the mouse position.

Posted: Wed Feb 17, 2021 11:24 pm
by ohai
I've looked at your code now and I think your problem is in turret.lua:18.
You are needlessly converting radians to degrees.
The draw function takes radians in the rotation parameter.
love2d wiki wrote: number r (0)
Orientation (radians).

Re: How to make an object rotate towards the mouse position.

Posted: Wed Feb 17, 2021 11:27 pm
by PenguinKing
ohai wrote: Wed Feb 17, 2021 11:24 pm I've looked at your code now and I think your problem is in turret.lua:18.
You are needlessly converting radians to degrees.
The draw function takes radians in the rotation parameter.
love2d wiki wrote: number r (0)
Orientation (radians).
Works perfectly! Thanks for the help! Now that that's done I need to get the orientation of the sprite correct when it turns to face the mouse, but I can do that on my own. Thanks for the help, once again! Have a good day. :awesome: :awesome: :awesome:

Re: How to make an object rotate towards the mouse position.

Posted: Wed Feb 17, 2021 11:36 pm
by ohai
PenguinKing wrote: Wed Feb 17, 2021 11:27 pm Works perfectly! Thanks for the help! Now that that's done I need to get the orientation of the sprite correct when it turns to face the mouse, but I can do that on my own. Thanks for the help, once again! Have a good day. :awesome: :awesome: :awesome:
No problem!
In the meantime I actually downloaded your project, and this works perfectly:

Code: Select all

function Turret:update(dt)
    local x = (love.mouse.getX()/5) - self.x
    local y = -((love.mouse.getY()/5) - self.y)
    self.angle = math.atan2(x, y)
end

function Turret:draw()
    [...]
    love.graphics.draw(self.image, self.x, self.y, self.angle, 1, 1, self.width/2, self.height/2)
    [...]
end
Have a nice day too!