hi, I encountered some complications working with touches.
version: latest beta / any
• touch.timestamp doesn’t play nice with other time functions that I know of
• when tapping a touch multiple times touch.tapCount goes up but the touch.id is not guaranteed to stay the same (which gives complications with advanced ways in which I want to use touches)
for the time issue my workaround of saving the difference in time is prone to error and needs occasional recalibrations, I can probably do better just ignoring touch.timestamp entirely and saving a start timestamp along with my “touchStarts”
I can’t easily come up with a solution to the inconsistency with touch.id over multiple taps without access to lower level code (which I wouldn’t understand anyway, lol)
-- goal: keep track of the time a touch has been held ...over multiple taps(this second part isn't implemented here)
-- complications:
-- touch.timestamp is a different time format from both os.time() and ElapsedTime
-- no consistent touch.id over multiple taps (touch.tapCount)
function setup()
touches = {}
touchStarts = {}
--find the difference by using the first touch as a calibration point
calibrated = false ; timeDifference = 0
parameter.watch("recallibrations") ; recallibrations = 0
end
function draw()
background(40, 40, 50)
if not calibrated then
pushStyle() ; fontSize(40) ; fill(255,128) ; text("touch to calibrate", WIDTH/2,HEIGHT/2) ; popStyle()
end
for k,touch in pairs(touches) do
math.randomseed(touch.id)
local touchColor = color(math.random(64,160),math.random(64,160),math.random(64,160))
local x,y = touch.pos.x, touch.pos.y
fill(touchColor) ; strokeWidth(0) ; ellipse(x,y, 100)
local info = " touch id: "..touch.id.."\ntapCount: "..touch.tapCount--.."\ntimestamp:"..touch.timestamp
if calibrated then
local touchTime = (ElapsedTime+timeDifference)-touchStarts[k].timestamp --calculate the current age of the touch
if touchTime < 0 then calibrateTime(touch.timestamp) end --needs to recallibrate if negative
info = info.."\n duration: "..touchTime
fill(0,0) ; stroke(touchColor*2) ; strokeWidth(3) ; ellipse(x,y, 100+(touchTime*20))
local hand = vec2(0,47+(touchTime*10)):rotate(-touchTime*(math.pi*2))
strokeWidth(4) ; line(x,y,x+hand.x,y+hand.y)
else --first calibration
calibrateTime(touch.timestamp)
print("os.time():\n"..os.time())--this is not the same time as touch uses
print("ElapsedTime:\n"..ElapsedTime)--and neither is ElapsedTime
print("touch.timestamp:\n"..touch.timestamp)
end
fill(255) ; text(info, x, y+100)
end
end
function touched(touch)
if touch.state == BEGAN then
touches[touch.id] = touch ; touchStarts[touch.id] = touch
elseif touch.state == CHANGED then
if touch.id == touches[touch.id].id then touches[touch.id] = touch end
else--if touch.state == ENDED or touch.state == CANCELLED then
--delete touch
touches[touch.id] = nil ; touchStarts[touch.id] = nil
end
end
function calibrateTime(touchTimestamp)
timeDifference = touchTimestamp-ElapsedTime
calibrated = true
recallibrations = recallibrations+1
end
ps:
a small bug I found: vector:rotate() asks for “angle in degrees” but actually needs an angle given in radians.