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.
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.
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!
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).
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.
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.
No problem!
In the meantime I actually downloaded your project, and this works perfectly:
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