Well... my issue comes from a quality of life preference to be honest. I'm currently at my day job so I can't provide specific code; however, I can give the pseudocode now and will provide the actual code when I get home.
I have created a virtual joystick and I want to map an object that circles the screen to the math.atan2 angle.
This works perfectly if I use obj.rotation = angle
.
What I want to do(and have stayed up all night trying to figure out) is make the transition of the object go smoothly to the joystick angle instead of just equaling it so that it isn't all jittery. The only idea I could come up with is getting a delta of the 2 and subtracting the obj.rotation from the delta/4.
That works for the most part, but when the angle from the math.atan2
goes from 359 to 0 it messes everything up.
Has anybody ever run into this and/or is willing to help me out? Is there a way to make math.atan2 spit out numbers > 360? I've been banging my head on my keyboard for too long on this.
Many thanks 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" then
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 then angle = 360 + angle end;
if angle > 360 then angle = angle - 360 end
if paddleAnchor.rotation < 0 then paddleAnchor.rotation = paddleAnchor.rotation + 360 end
if paddleAnchor.rotation > 360 then paddleAnchor.rotation = paddleAnchor.rotation - 360 end
local delta = (angle)-paddleAnchor.rotation
if delta < angle then
paddleAnchor.rotation = paddleAnchor.rotation + delta/5
elseif paddleAnchor.rotation > angle then
paddleAnchor.rotation = paddleAnchor.rotation - delta/5
end
print(delta)
local distance = mSqrt((posX*posX)+(posY*posY));
if distance >= radius then
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" then
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) then
--if (paddleAnchor.rotation < -angle - 90) then
--paddleAnchor.rotation = (paddleAnchor.rotation + (-angle-90)/2)
-- elseif
--end
--paddleAnchor.rotation = -angle - 90
if angle < 0 then angle = angle +360 end;
if angle > 360 then angle = angle - 360 end
if paddleAnchor.rotation < 0 then paddleAnchor.rotation = paddleAnchor.rotation + 360 end
if paddleAnchor.rotation > 360 then paddleAnchor.rotation = paddleAnchor.rotation - 360 end
local testAngle = angle + 360
--if (-angle-90) >= 0 then angle = 270 end
local delta = angle-paddleAnchor.rotation
print(delta)
if paddleAnchor.rotation < angle then
paddleAnchor.rotation = paddleAnchor.rotation + delta/5
elseif paddleAnchor.rotation > angle then
paddleAnchor.rotation = paddleAnchor.rotation - delta/5
end
--paddleAnchor:applyTorque(delta)
local distance = mSqrt((posX*posX)+(posY*posY));
if distance >= radius then
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" then
obj.y = obj.startMoveY
obj.x = obj.startMoveX
display.getCurrentStage():setFocus(nil)
end
return true
end