rotation

I’m working on a project that involves a clock, and a set_alarm mechanism that works by dragging a rotating pointer (clock is classic like analog with pointers, not digital) that is attached to the center of the clock, so by rotating pointer (that sets alarm) you can set the alarm. Here is the source:

local function rotateObj2(event)
local t = event.target
local phase = event.phase

if (phase == “began”) then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position of finger
t.x1 = event.x
t.y1 = event.y

elseif t.isFocus then
if (phase == “moved”) then
animateAlarm = true
t.x2 = event.x
t.y2 = event.y

angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
print("angle1 = "…angle1)
rotationAmt = angle1 - angle2

–rotate it
t.rotation = t.rotation - rotationAmt
print ("t.rotation = "…t.rotation)

t.x1 = t.x2
t.y1 = t.y2
alarm.rotation = t.rotation
setAlarm.rotation = t.rotation
print ("setAlarm.rotation = "…t.rotation)
if(alarm.rotation >=0)then
local hourValue = math.floor(t.rotation/30)
local minuteValue = math.floor(t.rotation*2)
local hour = hourValue
local minute = minuteValue

if(minuteValue>=720 and minuteValue<780)then
minute = minuteValue-720
elseif(minuteValue>=660 and minuteValue<720)then
minute = minuteValue-660
elseif(minuteValue>=600 and minuteValue<660)then
minute = minuteValue-600
elseif(minuteValue>=540 and minuteValue<600)then
minute = minuteValue-540
elseif(minuteValue>=480 and minuteValue<540)then
minute = minuteValue-480
elseif(minuteValue>=420 and minuteValue<480)then
minute = minuteValue-420
elseif(minuteValue>=360 and minuteValue<420)then
minute = minuteValue-360
elseif(minuteValue>=300 and minuteValue<360)then
minute = minuteValue-300
elseif(minuteValue>=240 and minuteValue<300)then
minute = minuteValue-240
elseif(minuteValue>=180 and minuteValue<240)then
minute = minuteValue-180
elseif(minuteValue>=120 and minuteValue<180)then
minute = minuteValue-120
elseif(minuteValue>=60 and minuteValue<120)then
minute = minuteValue-60
end

if (hour < 10) then hour = “0” … hour end
if (minute < 10) then minute = “0” … minute end

hourField.text = hour
minuteField.text = minute

end
elseif (phase == “ended”) then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

print ("ENDEDsetAlarm.rotation = "…setAlarm.rotation)
end
end

– Stop further propagation of touch event
return true
end
The problem is that when I try to define alarm clock value (like 03.45) it’s ok. I transform the rotation of the pointer as you can see. But, if the rotation goes below -90 or over 270 i dont get results. That’s because i did not define what to do with that (-rotation)

PROBLEM:

after some time, the rotation shifts. So i dont have rotation = 0 to 270 and -90 to 0, but -90 to -360+(-90) = -450;

sometimes it becomes from 60 to 360+60=420

help please, i can’t get around that, Because these “shifts” seem to happen randomly(or I just cant get why they happen)
[import]uid: 185906 topic_id: 32891 reply_id: 332891[/import]

the rotation “shifts” become from the point my pointer is at. and either add 360 or decrease 360

is there a workaround? I feel comfortable to use this rotation to set the alarm, i dont think i will be able to do it otherwise [import]uid: 185906 topic_id: 32891 reply_id: 130692[/import]

I’d have to run your code to completely understand how you’re doing things, but this may work to re-align the clock until you can figure out where the error is happening.

if t.rotation \> 360 then t.rotation = t.rotation%360 elseif t.rotation \< 0 then t.rotation = math.abs(t.rotation)%360 end [import]uid: 181948 topic_id: 32891 reply_id: 130693[/import]

if you drop me your email, or email me at 4eslau at gmail.com I will send you full pack [import]uid: 185906 topic_id: 32891 reply_id: 130696[/import]

basically, I have all clock parts aligned at center of the app.

so my pointers are square png with positioned actual graphical pointers to fit the clock.
I did not do the alarm yet.
1 for hours
2 for minutes
3 for seconds
4 for alarm pointer
5 is the alarm pointer thingy (they have the same rotation)
6 is the invisible circle, that i have event listener attached to

so when user starts to drag around the clock(or the clock itself) the rotation of the
alarm.rotation = t.rotation (4)
setAlarm.rotation = t.rotation (5)

changes, and i try to read those values

and there is digital time - 2 text fields that represent when the alarm will fire.

that’s it.

I noticed that when i do a couple of circles around the center it sometimes shifts. [import]uid: 185906 topic_id: 32891 reply_id: 130698[/import]

https://itunes.apple.com/sg/app/sleep-time-alarm-clock-sleep/id555564825?mt=8

it has the exact same mechanism of setting the alarm as this app [import]uid: 185906 topic_id: 32891 reply_id: 130699[/import]

Just sent you an email [import]uid: 181948 topic_id: 32891 reply_id: 130700[/import]

the rotation “shifts” become from the point my pointer is at. and either add 360 or decrease 360

is there a workaround? I feel comfortable to use this rotation to set the alarm, i dont think i will be able to do it otherwise [import]uid: 185906 topic_id: 32891 reply_id: 130692[/import]

I’d have to run your code to completely understand how you’re doing things, but this may work to re-align the clock until you can figure out where the error is happening.

if t.rotation \> 360 then t.rotation = t.rotation%360 elseif t.rotation \< 0 then t.rotation = math.abs(t.rotation)%360 end [import]uid: 181948 topic_id: 32891 reply_id: 130693[/import]

if you drop me your email, or email me at 4eslau at gmail.com I will send you full pack [import]uid: 185906 topic_id: 32891 reply_id: 130696[/import]

basically, I have all clock parts aligned at center of the app.

so my pointers are square png with positioned actual graphical pointers to fit the clock.
I did not do the alarm yet.
1 for hours
2 for minutes
3 for seconds
4 for alarm pointer
5 is the alarm pointer thingy (they have the same rotation)
6 is the invisible circle, that i have event listener attached to

so when user starts to drag around the clock(or the clock itself) the rotation of the
alarm.rotation = t.rotation (4)
setAlarm.rotation = t.rotation (5)

changes, and i try to read those values

and there is digital time - 2 text fields that represent when the alarm will fire.

that’s it.

I noticed that when i do a couple of circles around the center it sometimes shifts. [import]uid: 185906 topic_id: 32891 reply_id: 130698[/import]

https://itunes.apple.com/sg/app/sleep-time-alarm-clock-sleep/id555564825?mt=8

it has the exact same mechanism of setting the alarm as this app [import]uid: 185906 topic_id: 32891 reply_id: 130699[/import]

Just sent you an email [import]uid: 181948 topic_id: 32891 reply_id: 130700[/import]