How to make an object rotate towards the mouse position.

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.
Post Reply
PenguinKing
Prole
Posts: 8
Joined: Wed Feb 17, 2021 9:07 pm

How to make an object rotate towards the mouse position.

Post 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
Last edited by PenguinKing on Wed Feb 17, 2021 11:25 pm, edited 1 time in total.
User avatar
ohai
Prole
Posts: 20
Joined: Thu Jan 21, 2021 9:06 pm

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

Post 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)
Last edited by ohai on Wed Feb 17, 2021 11:24 pm, edited 2 times in total.
PenguinKing
Prole
Posts: 8
Joined: Wed Feb 17, 2021 9:07 pm

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

Post 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:
User avatar
ohai
Prole
Posts: 20
Joined: Thu Jan 21, 2021 9:06 pm

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

Post 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).
PenguinKing
Prole
Posts: 8
Joined: Wed Feb 17, 2021 9:07 pm

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

Post 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:
User avatar
ohai
Prole
Posts: 20
Joined: Thu Jan 21, 2021 9:06 pm

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

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

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 6 guests