[bug] sprite rotating towards at the mouse 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.
Post Reply
ignaciorojas16
Prole
Posts: 3
Joined: Thu Aug 10, 2023 10:46 am

[bug] sprite rotating towards at the mouse cursor

Post by ignaciorojas16 »

Hi!

i am working in a project where i use physics. in it, a circle body with a sprite of a ship that rotates towards the mouse cursor using trigonometry.
Works by applying torque to the body according to the position of the mouse cursor, while the sprite rotates at the same angle of the body.
Preview:

Image

When i put mi cursor here the body rotates towards it correctly.

Image

But the problem is when i lower mi cursor below this red line, the body do a entire 360 degress turn.

Image

Could you bring me a help how i can resolve this issue with an explanation of why is this happening?

the place where is happening this in specifically in this function inside of "Jugador.lua" file.

Code: Select all

-- the sprite of the ship will point to mouse position 
function Jugador:rotacionDelSpriteHaciaMouse()
  
  -- calculates the velocity of the rotation
  local velocidadRotacion = self.velocidadDeRotacion * self.multiplicadorDeRotacion
  
  -- the position of the body 
  local cuerpoX = self.cuerpo:getX()
  local cuerpoY = self.cuerpo:getY()
  
  -- the angle in degress
  local anguloCuerpo = math.deg(self.cuerpo:getAngle())
  
  -- LM means love.mouse 
  local anguloMouse  = math.deg(math.atan2(LM.getY() - cuerpoY, LM.getX() - cuerpoX))
  
  -- applying torque left or right 
  if anguloMouse > anguloCuerpo then
    self.cuerpo:applyTorque(velocidadRotacion)
  else
    self.cuerpo:applyTorque(-velocidadRotacion)
  end
end


Here is the love file so you can test it yourself.
game.love
(321.57 KiB) Downloaded 36 times
Attachments
entire turn.png
entire turn.png (27.72 KiB) Viewed 925 times
test1.png
test1.png (17.8 KiB) Viewed 925 times
1691669225.png
1691669225.png (5.23 KiB) Viewed 925 times
User avatar
darkfrei
Party member
Posts: 1196
Joined: Sat Feb 08, 2020 11:09 pm

Re: [bug] sprite rotating towards at the mouse cursor

Post by darkfrei »

You are need to calculate the smallest absolute angle as

Code: Select all

local clockwise = (360 + (angle1 - angle2)) % 360
local counterclockwise = (360 - (angle1 - angle2)) % 360
if clockwise > counterclockwise then
  rotate (deltaAngle)
else
  rotate (-deltaAngle)
end
Not tested, maybe change direction or change 360 to 180.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
BrotSagtMist
Party member
Posts: 655
Joined: Fri Aug 06, 2021 10:30 pm

Re: [bug] sprite rotating towards at the mouse cursor

Post by BrotSagtMist »

Horrible code demands horrible fixes:
horidfix.love
(282.33 KiB) Downloaded 53 times
obey
ignaciorojas16
Prole
Posts: 3
Joined: Thu Aug 10, 2023 10:46 am

Re: [bug] sprite rotating towards at the mouse cursor

Post by ignaciorojas16 »

darkfrei wrote: Thu Aug 10, 2023 2:44 pm You are need to calculate the smallest absolute angle as

Code: Select all

local clockwise = (360 + (angle1 - angle2)) % 360
local counterclockwise = (360 - (angle1 - angle2)) % 360
if clockwise > counterclockwise then
  rotate (deltaAngle)
else
  rotate (-deltaAngle)
end
Not tested, maybe change direction or change 360 to 180.
Thanks, i appreciate it!
ignaciorojas16
Prole
Posts: 3
Joined: Thu Aug 10, 2023 10:46 am

Re: [bug] sprite rotating towards at the mouse cursor

Post by ignaciorojas16 »

BrotSagtMist wrote: Thu Aug 10, 2023 2:56 pm Horrible code demands horrible fixes:horidfix.love
you're right, i am new in this of game making, i just need to practice so i can improve, thanks also for helping me.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests