Any drag/flick Velocity code out there?

If I flick or swipe an object the object stops where my finger stops. I really like the velocity effect that the iphone uses that slowly eases out of these motions.

Is that any code out there for this. It would save me a lot of time trying to figure something that runs smoothly. It would also be a nice feature to add if you find some time for it Corona.

thanks,
Jason [import]uid: 5687 topic_id: 1326 reply_id: 301326[/import]

Depends on what you use for your movement. If you use transitions, then there are already the ease functionalities. [import]uid: 5712 topic_id: 1326 reply_id: 3596[/import]

I guess he means the interia based scrolling… which may be simulated with the easing but I think they are a different cup of tea…

I am going to implement something like this in some days for scrolling of my menues. I think of simulating a rolling ball which is dampened and gets its rotation by swiping the finger… so that it gets faster if you swipe faster than dampening occurs and stops eventually if you stop moving or touch it without swiping (stopping)… Well… should be fun :slight_smile: [import]uid: 6928 topic_id: 1326 reply_id: 3602[/import]

I found this code in the code exchange, it might help you, though I am still trying to find out how to make the ball a physic body. I made a transition animation on the left hand side, I encountered a bug that if you do not make contact with the ball the transition stops. If you find out how to make the ball a physic body, it is much appreciated to let me know how to do it,
here is the code:

local physics = require (“physics”)
physics.start(true)
physics.setDrawMode “hybrid”
physics.setGravity(15, 0)
local square = display.newRect( 0, 0, 100, 100 )
square:setFillColor( 255,255,255 )
local w,h = display.contentWidth, display.contentHeight
physics.addBody( square, “static”,{friction=0.3, boucne=0.3})
local loopPhase1
local loopPhase2

loopPhase1 = function()
transition.to( square, { time=1500, loopCount = 10, x=(w-275), y=(h-50), onComplete=loopPhase2} )
end

loopPhase2 = function()
transition.to( square, { time=1500, delay=30, x=(w±275), y=(h-427), onComplete=loopPhase1 } )
end

–to trigger it to start:
loopPhase1()

local screenW, screenH = display.stageWidth, display.stageHeight
local friction = 0.8
local gravity = .9
local speedX, speedY = 0, 0
local prevX, prevY = 0, 0

local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(255, 255, 0)
ball.x = screenW*.5
ball.y = 20

function onMoveCircle(event)
speedY = speedY + gravity

ball.x = ball.x + speedX
ball.y = ball.y + speedY

if( ball.x >= line1 - ball.width*.5 ) then
ball.x = line1 - ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
elseif( ball.x <= ball.width*.5) then
ball.x = ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
end
if( ball.y >= screenH - ball.height*.5 ) then
ball.y = screenH - ball.height*.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 --change direction
elseif( ball.y <= ball.height*.5 ) then
ball.y = ball.height*.5
speedY = speedY*friction
speedY = speedY*-1 --change direction
end
end

– A general function for dragging objects
local function startDrag( event )
local t = event.target

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

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Stop current motion, if any
Runtime:removeEventListener(“enterFrame”, onMoveCircle)
– Start tracking velocity
Runtime:addEventListener(“enterFrame”, trackVelocity)

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

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

Runtime:removeEventListener(“enterFrame”, trackVelocity)
Runtime:addEventListener(“enterFrame”, onMoveCircle)

end
end

– Stop further propagation of touch event!
return true
end

function trackVelocity()
speedX = ball.x - prevX
speedY = ball.y - prevY +10 – try changing these numbers, the flick might be more or less sensible if you change this number.

prevX = ball.x
prevY = ball.y -5 – same here.
end

ball:addEventListener(“touch”, startDrag)
Runtime:addEventListener(“enterFrame”, onMoveCircle) [import]uid: 23689 topic_id: 1326 reply_id: 42510[/import]

You dug up, almost an year old thread there mate… [import]uid: 48521 topic_id: 1326 reply_id: 42545[/import]