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:
When i put mi cursor here the body rotates towards it correctly.
But the problem is when i lower mi cursor below this red line, the body do a entire 360 degress turn.
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.