Rotation of objects and radians

I’m using this code to rotate a clock hand:
http://developer.anscamobile.com/code/rotate-object-finger

and it works very nicely. But after the object is done rotating, and I go to check the radians of the hour hand t’s often in the 4,000-7,000 or more range… What I can’t figure out is why it’s so high, or how to then convert it back to a normal value (or in degrees) that I can use to determine where the clock hand is pointing… Anyone have any advice? [import]uid: 19193 topic_id: 14969 reply_id: 314969[/import]

Here is the code I’m using. You can see that if you touch and spin the hour hand, that around line 80 I show the radians on the screen, and they’ll be all over the place and make no sense. And if I take it, and use math.deg() on it, it will give me a value like 20,000. What am I doing wrong? I just want to get it to a scale of 360 degrees so I can figure out what number the hand is pointing at on the clock.

Thanks for any help if anyone has any insight. I am officially 100% stuck on it.

[lua]-- Analog Clock App
– Developed by Carlos Yanez

– Background

local background = display.newImageRect(“background.png”)

– Clock hands

local hourHand = display.newImage(“hourHand.png”, 152, 185)
local minuteHand = display.newImage(“minuteHand.png”, 152, 158)
local center = display.newImage(“center.png”, 150, 230)
local secondHand = display.newImage(“secondHand.png”, 160, 155)
local myText = display.newText(“Waiting…”,(display.viewableContentWidth / 2), 40, native.systemFont, 20)
myText:setTextColor(255, 255, 255)

hourHand.id = ‘hourHand’
minuteHand.id = ‘minuteHand’

hourHand:setReferencePoint(display.BottomCenterReferencePoint)
minuteHand:setReferencePoint(display.BottomCenterReferencePoint)
secondHand:setReferencePoint(display.BottomCenterReferencePoint)

– Set clock hands position at start

local timeTable = os.date("*t")

hourHand.rotation = timeTable.hour * 15 + (timeTable.min * 0.5)
myText.text = hourHand.rotation…’ : '…timeTable.hour
minuteHand.rotation = timeTable.min * 6
secondHand.rotation = timeTable.sec * 6

timeTable = nil

– Reposition every second

local function moveHands(e)
local timeTable = os.date("*t")
hourHand.rotation = timeTable.hour * 15 + (timeTable.min * 0.5)
minuteHand.rotation = timeTable.min * 6
secondHand.rotation = timeTable.sec * 6
end

local function rotateObj(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
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
print ("t.rotation = "…t.rotation)

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

t.x1 = t.x2
t.y1 = t.y2

elseif (phase == “ended”) then

display.getCurrentStage():setFocus( nil )
t.isFocus = false

–Prints out the current rotation on screen:
myText.text = t.id…’ || R: ‘…hourHand.rotation…’ || D: '…math.deg(hourHand.rotation)

end
end
return true
end

hourHand:addEventListener(“touch”, rotateObj)[/lua] [import]uid: 19193 topic_id: 14969 reply_id: 55651[/import]

I have tried something, plz. check the following code


– Analog Clock App
– Developed by Carlos Yanez

– Background

local background = display.newImageRect(“background.png”, 640, 960)
background.x = display.contentWidth/2;
background.y = display.contentHeight/2;

– Clock hands

local hourHand = display.newImage(“hourHand.png”, 152, 185)
local minuteHand = display.newImage(“minuteHand.png”, 152, 158)
local center = display.newImage(“center.png”, 150, 230)
local secondHand = display.newImage(“secondHand.png”, 160, 155)
local myText = display.newText(“Waiting…”,(display.viewableContentWidth / 2), 40, native.systemFont, 30)
myText:setTextColor(0, 0, 0)

hourHand.id = ‘hourHand’
minuteHand.id = ‘minuteHand’

hourHand:setReferencePoint(display.BottomCenterReferencePoint)
minuteHand:setReferencePoint(display.BottomCenterReferencePoint)
secondHand:setReferencePoint(display.BottomCenterReferencePoint)

hourHand.x = display.contentWidth/2;
hourHand.y = display.contentHeight/2;
minuteHand.x = display.contentWidth/2;
minuteHand.y = display.contentHeight/2;
secondHand.x = display.contentWidth/2;
secondHand.y = display.contentHeight/2;
center.x = display.contentWidth/2;
center.y = display.contentHeight/2;

– Set clock hands position at start

local timeTable = os.date("*t")

hourHand.rotation = timeTable.hour * 30 + (timeTable.min * 0.5)
myText.text = “Drag to rotate”
minuteHand.rotation = timeTable.min * 6
secondHand.rotation = timeTable.sec * 6

timeTable = nil

– Reposition every second

local function moveHands(e)
local timeTable = os.date("*t")
hourHand.rotation = timeTable.hour * 30 + (timeTable.min * 0.5)
minuteHand.rotation = timeTable.min * 6
secondHand.rotation = timeTable.sec * 6
timeTable = nil
end

–timer.performWithDelay(1000, moveHands, 0)

local function rotateObj(event)

local t = event.target
local phase = event.phase
local angle

if (phase == “began”) then

display.getCurrentStage():setFocus( t )
t.isFocus = true

elseif t.isFocus then

if (phase == “moved”) then

angle = math.atan2(event.y - display.contentHeight/2, event.x - display.contentWidth/2); --get angle in radians

angle = angle * 180/math.pi + 90; --convert to degrees , the 90 is to have it point to the mouse

myText.text = angle + 90

t.rotation = angle; --rotate

elseif (phase == “ended”) then

display.getCurrentStage():setFocus( nil )
t.isFocus = false
angle = nil
t = nil
phase = nil

–myText.text = t.id…’ || R: ‘…hourHand.rotation…’ || D: '…math.deg(hourHand.rotation)
end
end
return true
end

hourHand:addEventListener(“touch”, rotateObj) [import]uid: 88119 topic_id: 14969 reply_id: 57277[/import]