Rotating the wrong way

Hi gb8,

I got the same problem here and I understand all the logic behind this equation:

function smallestAngleDiff( target, source )
    local a = target - source
    if (a > 180) then
        a = a - 360
    elseif (a < -180) then
        a = a + 360
    end
    return a
end

The thing I don’t understand is how to callback the last angle (source)  I had the move before…

Hi gb8,

I have the same problem here and I understand all the logic behind this:

function smallestAngleDiff( target, source )
    local a = target - source
    if (a > 180) then
        a = a - 360
    elseif (a < -180) then
        a = a + 360
    end
    return a
end

My problem is that i don’t know how to callback the last angle (source) I had the move before…

Hi leo,

– First get the angle between the two points

  local rotationangle =getImageRotation(player.x, player.y, px,py)

– Figure out which way to turn

local angle = smallestAngleDiff( player.rotation, rotationangle)

– do the turn

transition.to( player, { rotation = player.rotation- angle, time=400, transition=easing.linear  } )

function getImageRotation(x1,y1,x2,y2)
    if x1 ~=nil and y1 ~=nil and x2~=nil and y2~=nil then
        local angleInDegrees = (math.deg(math.atan2( (y2-y1), (x2-x1))))
        return angleInDegrees
    else
        return 0
    end
end

Hope that helps, Greg

I got it, it goes like a charm! I had the wrong approach…

I added this to my code to make it works every additional move:

if (player.rotation > 180) 

player.rotation = player.rotation -360

elseif (player.rotation < -180) then

player.rotation = player.rotation - 360

end

Thank you very much Greg!

If you horizontally flip it (set xScale negative) won’t it rotate the other way round ?