hi, so i have a problem with atan2, i want to make my rectangle to face mouse and rotate towards it, i try to use atan2, but it didnt work, it shows a very weird movement instead,
(in the update function)
angle = angle + math.atan2(y - love.mouse.getY, love.mouse.getX() - x) * dt
end
the rotation is uncontrollable
atan2 problem
Re: atan2 problem
That's because you're adding up to rotation angle every frame. What you need to do is to apply it as is.
Re: atan2 problem
Yes, like raidho said, it should be:
If you're looking for gradual rotation they you need the find the arc between the "current" and "target" angle.
Just make sure to check if the "current angle" is near the target to avoid jitter (and division by 0):
Then you can normalize the arc and use that to rotate at a constant speed:
Code: Select all
target_angle = math.atan2(y - love.mouse.getY, love.mouse.getX() - x)
Code: Select all
arc = (current_angle - target_angle + math.pi)%(2*math.pi) - math.pi
Code: Select all
if math.abs(arc) < rotation_speed*dt then
-- already facing the target!
return
end
Code: Select all
direction = arc/math.abs(arc)
current_angle = current_angle + direction*rotation_speed*dt
Who is online
Users browsing this forum: No registered users and 1 guest