Creating a bouncing ball with a set velocity

I’ve seen many examples about how to make an object bounce around on a screen, but i still can’t figure out how to have the user “flick” the screen and have the ball bounce at a certain speed. Im used an object to create the ball so I think it has something to do with linearVelocity but i’m not sure. It always goes in one direction that way.

the touch object has a lot of information in in (check the online help).

This includes touch.deltaX and touch.deltaY, which tell you how much the finger moved in the x and y direction since last redraw. If you change your x, y velocity to these values, your ball will react to finger flicks

. @evanlws12 Here is something I wrote a while back. I flick an object at another object. Maybe this will help you.


    supportedOrientations(PORTRAIT)
    displayMode(FULLSCREEN)
    lineCapMode(SQUARE)

function setup()
    tab={}
    rcount=0
    scount=0
    screated=false
    rcreated=false
    createEdges()
end

function draw()
    background(40, 40, 50)
    showLines()
    showText()
    drawEdge()
    drawRandom()
    drawShots()
end

function showLines()
    stroke(0,255,0)
    strokeWidth(4)
    line(0,300,WIDTH,300)
    stroke(255,0,0)
    strokeWidth(20)
    line(300,HEIGHT,450,HEIGHT)  
end

function showText()
    fontSize(40)
    fill(255,0,0)
    if not rcreated then
        text("Double tap screen for red ball.",WIDTH/2,HEIGHT/2+300)
    end
    str=string.format("Goals %4d            Shots %4d",rcount,scount)
    fontSize(20)
    fill(255)
    text(str,WIDTH/2,HEIGHT/2-50)
    str=string.format("Score %6.4f         (Goals / shot)",rcount/scount)
    text(str,WIDTH/2,HEIGHT/2-100)
    text("Flick shots from below this line to push",WIDTH/2,320)
    text("the red ball through the red line at the top.",WIDTH/2,280)   
end

function createEdges()
    e1=physics.body(EDGE,vec2(0,0),vec2(0,1200))
    e1.type=STATIC
    e2=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,1200))
    e2.type=STATIC  
    e3=physics.body(EDGE,vec2(0,HEIGHT),vec2(300,HEIGHT))
    e3.type=STATIC 
    e4=physics.body(EDGE,vec2(450,HEIGHT),vec2(WIDTH,HEIGHT))
    e4.type=STATIC             
end

function drawEdge()
    stroke(255)
    strokeWidth(10)
    line(0,0,0,HEIGHT) 
    line(WIDTH,0,WIDTH,HEIGHT) 
    line(0,HEIGHT,300,HEIGHT)
    line(450,HEIGHT,WIDTH,HEIGHT)
end


function createRandom()
    r1=physics.body(CIRCLE,30)
    r1.gravityScale=.5
    r1.x=math.random(WIDTH) 
    r1.y=HEIGHT-200
    rcreated=true
end  

function drawRandom()
    if rcreated then
        strokeWidth(0)
        fill(255,0,0)
        ellipse(r1.x,r1.y,60,60)
        destroyRandom()
    end
end

function destroyRandom()
    if r1.y < 0 or r1.y>HEIGHT then
        rcreated=false       
        tab={}
        if r1.y>HEIGHT then
            rcount = rcount + 1
        end        
        r1:destroy()
    end 
end 

function createShots()
    scount = scount + 1
    z=#tab+1
    tab[z]=physics.body(CIRCLE,15)
    tab[z].gravityScale=-.1
end
    
function drawShots()
    fill(255) 
    for z=1,#tab do
        ellipse(tab[z].x,tab[z].y,30,30)
    end
    destroyShots()
end

function destroyShots()
    for z=1,#tab do
        if tab[z].y > 1000 or tab[z].y < 0 then
            tab[z]:destroy()
            table.remove(tab,z)
            return
        end
    end
end   

function touched(t)
    if not rcreated then
        if t.tapCount==2 then
            createRandom()
        end
        return
    end
    if t.state==BEGAN and t.y< 300 and rcreated then
        createShots()
        screated=true
        z=#tab
        tab[z].x=t.x
        tab[z].y=t.y
        x1=t.x
        y1=t.y
    end
    if t.state==MOVING and screated then
        if #tab>0 then
            z=#tab
            tab[z].x=t.x
            tab[z].y=t.y
        end
    end
    if t.state==ENDED and screated then
        screated=false
        if #tab>0 then
            z=#tab
            x2=t.x
            y2=t.y
            xd=x2-x1
            yd=y2-y1
            tab[z].linearVelocity=vec2(xd*5,yd*5)
        end
    end    
end

There’s a tutorial out here
https://bitbucket.org/TwoLivesLeft/core/wiki/TouchTutorial but it doesn’t include physics objects

That tutorial is worth reading for the last bit about flicks where it explains why not to just use the last touch information.

@dave1707 I do like the game! I do see you made the linear velocity (x2-x1)*5 however so when the user flicks very slowly, the ball also moves slowly. I’m still trying to figure out if there is a way that no matter how slowly the flick is, the ball moves at the same speed?

.@evanlws12 Replace the code “if t.state==ENDED” with the code below. Change the number 500 in the code below to get the speed that you want. The larger the number the faster it will go, but it should go at the same speed no matter how fast or slow you flick.


    if t.state==ENDED and screated then
        screated=false
        if #tab>0 then
            z=#tab
            x2=t.x
            y2=t.y
            xd=x2-x1
            yd=y2-y1
            s=500/math.sqrt(xd^2+yd^2)
            tab[z].linearVelocity=vec2(xd*s,yd*s)
        end
    end    

It works! Thank you!