Page 1 of 1

[bug] sprite rotating towards at the mouse cursor

Posted: Thu Aug 10, 2023 12:59 pm
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

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

Posted: Thu Aug 10, 2023 2:44 pm
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.

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

Posted: Thu Aug 10, 2023 2:56 pm
by BrotSagtMist
Horrible code demands horrible fixes:
horidfix.love
(282.33 KiB) Downloaded 53 times

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

Posted: Thu Aug 10, 2023 6:47 pm
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!

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

Posted: Thu Aug 10, 2023 6:50 pm
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.