Smoothly rotate to face the cursor

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.
Snake174rus
Prole
Posts: 32
Joined: Thu Aug 28, 2014 8:38 am
Location: Russia
Contact:

Re: Smoothly rotate to face the cursor

Post by Snake174rus »

Insert this code in update function:

Code: Select all

local mousePos = vector( love.mouse.getX(), love.mouse.getY() )
local dir = self.pos - mousePos

local targetAngle = -math.atan2( dir.x, dir.y ) / (math.pi / 180)
local curAngle = self.top_angle

if curAngle < 0 then
	curAngle = curAngle + 360
end

if targetAngle < 0 then
	targetAngle = targetAngle + 360
end

local a = curAngle - targetAngle

if a > 180 then
	a = a - 360
elseif a < -180 then
	a = a + 360
end

local s = dt * 70

if a >= -s - 0.5 and a <= s + 0.5 then
	self.top_angle = targetAngle
elseif a > s + 0.5 then
	self.top_angle = self.top_angle - s
elseif a < -s - 0.5 then
	self.top_angle = self.top_angle + s
end
I use HUMP Vectors for stored self position (self.pos)

Code: Select all

local s = dt * 70
70 - rotation speed
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Smoothly rotate to face the cursor

Post by ivan »

iPoisonxL wrote:This makes sense, thanks a lot.
There is another solution which uses the dot product of 2 vectors.

Code: Select all

function dot ( x, y, x2, y2 )
  return x * x2 + y * y2
end
The dot product tells us about the angle (R) between the two vectors. It is positive, when the angle is acute and negative if it's obtuse. When the dot product is zero, the two vectors are perpendicular.

For two normalized vectors:

Code: Select all

cos(R) = dot(ax, ay, bx, by)
R = acos(dot(ax, ay, bx, by)
R = arc between the two vectors

For vectors that may not be normalized:

Code: Select all

function length(ax, ay)
  return math.sqrt(ax*ax + ay*ay)
end

cos(R) = dot(ax, ay, bx, by)/(length(ax, ay)*length(bx, by)
R = acos(dot(ax, ay, bx, by)/(length(ax, ay)*length(bx, by)))
R = arc between the two vectors

In this case:

Code: Select all

ax, ay = cos(H), math.sin(H)
bx, by = px - tx, py - ty
H = current heading
px, py = position of the "cursor"
tx, ty = target position
danjoemybro
Prole
Posts: 2
Joined: Mon Apr 26, 2021 9:34 am

Re: Smoothly rotate to face the cursor

Post by danjoemybro »

micha wrote: Tue Dec 17, 2013 9:55 pm iPoisonxL's code is almost correct. One thing is missing, namely the two angles zero and 2pi are the same, even though the two numbers differ a lot. To determine whether to turn left or right, you need calculate diff as follows:

Code: Select all

local diff = (goal-current+math.pi)%(2*math.pi)-math.pi
That way diff is always between -pi and pi.
Thank you so much iPoisonxL for this amazing comment so long ago. You've saved me countless hours of trying to figure this out.

I adapted this function so that the speed of rotation is affected by the difference in angle, giving a very smooth feel, whilst also reducing the amount of code considerably.

Code: Select all

function smoothAngle(dt, goal, current, speed)
    local diff = (goal-current+math.pi)%(2*math.pi)-math.pi
    return current + (diff * speed) * dt
end
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Smoothly rotate to face the cursor

Post by togFox »

Nice necro. Seven years. :) How did you even find this thread?
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
danjoemybro
Prole
Posts: 2
Joined: Mon Apr 26, 2021 9:34 am

Re: Smoothly rotate to face the cursor

Post by danjoemybro »

Haha, google search pretty much, whilst typing in a bunch on nonsence around the topic of the problem which I was facing. I never would have worked this out by myself.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Smoothly rotate to face the cursor

Post by zorg »

At least you did contribute something to the thread. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest