lua - Issue using math.atan2 when rotating an object -


well... issue comes quality of life preference honest. i'm @ day job can't provide specific code; however, can give pseudocode , provide actual code when home.

i have created virtual joystick , want map object circles screen math.atan2 angle.

this works if use obj.rotation = angle.

what want do(and have stayed night trying figure out) make transition of object go smoothly joystick angle instead of equaling isn't jittery. idea come getting delta of 2 , subtracting obj.rotation delta/4.

that works part, when angle math.atan2 goes 359 0 messes up.

has ever run and/or willing me out? there way make math.atan2 spit out numbers > 360? i've been banging head on keyboard long on this.

many in advance.

local function movepaddle(event) local obj = right local mmin = math.min; local mcos = math.cos; local msin = math.sin; local matan2 = math.atan2; local msqrt = math.sqrt; local mfloor = math.floor; local mpi = math.pi; local radtodeg = 180/mpi; local degtorad = mpi/180; local radius = 40 if event.phase == "began" display.getcurrentstage():setfocus(obj) --startmovex = obj.x; startmovey = obj.y; local parent = obj.parent;     local posx, posy = parent:contenttolocal(event.x, event.y)     obj.x = posx; obj.y = posy;     local angle = (matan2( posx, posy )*radtodeg)-90;     local testangle = angle + 360      --paddleanchor:applytorque(delta*100)          if angle < 0 angle = 360 + angle end;          if angle > 360 angle = angle - 360 end          if paddleanchor.rotation < 0 paddleanchor.rotation =      paddleanchor.rotation + 360 end          if paddleanchor.rotation > 360 paddleanchor.rotation =   paddleanchor.rotation - 360 end          local delta = (angle)-paddleanchor.rotation           if delta < angle              paddleanchor.rotation = paddleanchor.rotation + delta/5         elseif paddleanchor.rotation > angle              paddleanchor.rotation = paddleanchor.rotation - delta/5         end             print(delta)             local distance = msqrt((posx*posx)+(posy*posy));             if distance >= radius                 local radangle = angle*degtorad;                 distance = radius;                 obj.x, obj.y = distance*mcos(radangle), -    distance*msin(radangle)             else                 obj.x, obj.y = posx, posy;             end  elseif event.phase == "moved"     local parent = obj.parent;     local posx, posy = parent:contenttolocal(event.x, event.y)         obj.x = (event.x - event.xstart) + posx         obj.y = (event.y - event.ystart) + posy     local angle = (matan2( posx, posy )*radtodeg)-90;     --local testangle = angle + 360     --if (paddleanchor.rotation ~= -angle - 90)         --if (paddleanchor.rotation < -angle - 90)              --paddleanchor.rotation = (paddleanchor.rotation + (-angle-90)/2)     --  elseif      --end     --paddleanchor.rotation = -angle - 90       if angle < 0 angle = angle +360 end;       if angle > 360 angle = angle - 360 end       if paddleanchor.rotation < 0 paddleanchor.rotation = paddleanchor.rotation + 360 end       if paddleanchor.rotation > 360 paddleanchor.rotation = paddleanchor.rotation - 360 end       local testangle = angle + 360          --if (-angle-90) >= 0 angle = 270 end        local delta = angle-paddleanchor.rotation       print(delta)          if paddleanchor.rotation < angle              paddleanchor.rotation = paddleanchor.rotation + delta/5         elseif paddleanchor.rotation > angle              paddleanchor.rotation = paddleanchor.rotation - delta/5         end     --paddleanchor:applytorque(delta)              local distance = msqrt((posx*posx)+(posy*posy));             if distance >= radius                 local radangle = angle*degtorad;                 distance = radius;                 obj.x, obj.y = distance*mcos(radangle), -distance*msin(radangle)             else                 obj.x, obj.y = posx, posy;             end elseif event.phase == "ended" or event.phase == "cancelled"     obj.y = obj.startmovey     obj.x = obj.startmovex     display.getcurrentstage():setfocus(nil) end return true end 

atan2(y, x) gives angle or argument arg of x + * y. in cartesian plane positive orientation. screen coordinates different.

the angle arg2 - arg1 between 2 points argument of fraction (x2+i*y2)/(x1+i*y1). since argument not depend on real positive factors, same argument of

(x2+i*y2)*(x1-i*y1) = (x1*x2+y1*y2) + i*(x1*y2-y1*x2) 

which can computed

delta_angle = atan2(x1*y2-y1*x2, x1*x2+y1*y2) 

thus compute angle increment last position (x1, y1) new position (x2, y2) , add total angle,

angle += delta_angle.  

this should give non-jumping angle measure.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -