Fling effect when moving the camera

Hi, I’m using a script for touch movements controls, like pan or pinch to zoom. But the pan movement is not the smoothest because even when you swipe super fast, when you release the touch It instantly stops the movement. What I want to do is adding a fling effect to give a velocity to the touch when you swipe fast.

The concept is that when the touch phase is ended, the game calculates the velocity and compares it with a value and if the condition is true, it activate an enter frame function which keeps moving the touch movement until It finishes (maybe with a lerp func). The problem is that idk how to direct the velocity to the same angle as the prev movement. Can somebody help me?

The term you’re thinking of is momentum.
 
Try doing some searches on that.
 
As far as angle, you need to accumulate points to form a vector and then calculate a vector from the last two points.
 
Something like ( may contain typos; and requires SSK2 because I’m not doing long hand vector math)…

local getTimer = system.getTimer local momentumTime = 500 local cap = 500 -- Uses SSK2 Math2D: -- https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/ -- local function onTouch( self, event ) self.x = event.x -- NOT BEST WAY TO DO THIS self.y = event.y -- NOT BEST WAY TO DO THIS if( event.phase == "began" ) then self.touches = {} transition.cancel(self) elseif( event.phase == "moved" ) then self.touches[#self.touches+1] = { x = event.x, y = event.y, t = getTimer() } elseif(event.phase == "ended" ) then if( #self.touches \>= 4 ) then local p0 = self.touches[#self.touches-3] local p1 = self.touches[#self.touches] local vec = ssk.math2d.sub( p1, p0 ) local len = ssk.math2d.length( vec ) if( len \> 1 ) then local dt = p1.t - p0.t -- NOT BEST WAY BUT WILL WORK OK... local speed = 1000 \* len/dt if( speed \> cap ) then speed = cap end local distance = momentumTime \* speed / 1000 vec = ssk.math2d.normalize( vec ) vec = ssk.math2d.scale( vec, distance ) vec.x = vec.x + self.x vec.y = vec.y + self.y transition.to( self, { x = vec.x, y = vec.y, time = momentumTime, transition = easing.outCirc } ) end end end return false end local obj = display.newRect( 100, 100, 100, 100 ) obj.touch = onTouch obj:addEventListener("touch")

The term you’re thinking of is momentum.
 
Try doing some searches on that.
 
As far as angle, you need to accumulate points to form a vector and then calculate a vector from the last two points.
 
Something like ( may contain typos; and requires SSK2 because I’m not doing long hand vector math)…

local getTimer = system.getTimer local momentumTime = 500 local cap = 500 -- Uses SSK2 Math2D: -- https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/ -- local function onTouch( self, event ) self.x = event.x -- NOT BEST WAY TO DO THIS self.y = event.y -- NOT BEST WAY TO DO THIS if( event.phase == "began" ) then self.touches = {} transition.cancel(self) elseif( event.phase == "moved" ) then self.touches[#self.touches+1] = { x = event.x, y = event.y, t = getTimer() } elseif(event.phase == "ended" ) then if( #self.touches \>= 4 ) then local p0 = self.touches[#self.touches-3] local p1 = self.touches[#self.touches] local vec = ssk.math2d.sub( p1, p0 ) local len = ssk.math2d.length( vec ) if( len \> 1 ) then local dt = p1.t - p0.t -- NOT BEST WAY BUT WILL WORK OK... local speed = 1000 \* len/dt if( speed \> cap ) then speed = cap end local distance = momentumTime \* speed / 1000 vec = ssk.math2d.normalize( vec ) vec = ssk.math2d.scale( vec, distance ) vec.x = vec.x + self.x vec.y = vec.y + self.y transition.to( self, { x = vec.x, y = vec.y, time = momentumTime, transition = easing.outCirc } ) end end end return false end local obj = display.newRect( 100, 100, 100, 100 ) obj.touch = onTouch obj:addEventListener("touch")