Bouncing Balls

This is a little something I threw together to amuse my toddlers. It generates balls at touch point(s) and they bounce


function setup()
    displayMode(FULLSCREEN)
    --myFPSReporter = FPSReporter(4)
    
    noSmooth()
    
    balls = {}
    touches = {}
    
    base = physics.body(EDGE, vec2(100,0), vec2(WIDTH-100,0))
    
    nextball = 0
end

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        --if touches[touch.id] == nil then
        touches[touch.id] = touch
        --end
    end    
end    

function touchActions()
    for k,v in pairs(touches) do 
        if CurrentTouch.state == ENDED then
            --if there are no current touches then we kill all current touches to avoid bugged ball producers
            touches[k] = nil
        else
                
            --add a new ball at the touch location
            size = math.random(1,20)
            tspot = physics.body(CIRCLE, size)
            tspot.position = vec2(v.x+math.random(-1,1), v.y+math.random(-1,1))
            tspot.restitution = 1
        
            balls[nextball] = { tspot = tspot, size = size * 2, r = math.random(30,255), g = math.random(30,255), b = math.random(30,255) }
            
            nextball = nextball + 1
        end    
    end
end

function draw()
    background(0, 0, 0, 255)
    --myFPSReporter:draw(3)
    strokeWidth(0)
    
    for k,v in pairs(balls) do
        if v.tspot.x < -20 or v.tspot.x > WIDTH + 20 or v.tspot.y < -20 then
            balls[k].tspot:destroy()
            balls[k] = nil  
        else
            fill(v.r, v.g, v.b, 255)
            ellipse(v.tspot.x, v.tspot.y, v.size)
        end
    end
    
    touchActions()
end

Hi! I can’t easily copy the code if you declare it LUA. Can you remove the LUA declaration?

Very nice and amusing for adults, too! :wink: I was planning on doing something like this based on an old Java program I wrote. I will be studying this. Thanks for sharing!

Not much to study… I did something similar in another language but that didn’t have a physics engine so was hard work.

The key 2 things are:

Touch handling which I got somewhere else, but I added the cleanup on CurrentTouch as I was sometimes getting bad touches stuck in my array.

Destroy your physics objects when they go off screen, otherwise it slows down a LOT after it’s been running a while.

So much pleasure with so few code… You’re a poet!

@spacemonkey tap on the screen with 3 fingers in a row, one after the other: tap-tap-tap! You’ll see something you probably didn’t expect! Also works if i slide 3 fingers, but not 2 or 4. Maybe it is because i have enabled zoom with 3 fingers double tap?

@spacemonkey you’re too humble and this is a lot of fun for such a small amount of code. I’m new to Codea so this sort of project is exactly what I’m interested in for learning the capabilities of the framework. My 13 year old son wants to help, as well, so I’ll be sharing this with him as a fantastic example. Thanks again!

I agree. Here is the video of some monkey art from your code:
http://www.youtube.com/watch?v=6qP_wt7obSg

.@SpaceMonkey i very much this program. This is another of CODEA gems that are or will be progressively deeply burried into the forum… I’d like to collect some of those gems in my web repo, so others (and I) can find them easily. Do you allow me to put the code in my web repo, crediting you as the author and owner of the code, of course?

Feel free to take any code I’ve put on the forum and stick it in your repo. I have a new physics one that I am hoping to publish soon as well if I can just knock the bugs out of it :wink:

Thanks!

This one is as elegant as the curly lines an drawing apps. Nice work.

edit: Needs collision detection and MINOR sound effects.

HACK

function collide(contact)
    if contact.state == BEGAN then 
        sound(SOUND_HIT, 47766)  --replace this with any fast sound
    end
end

.@aciolino On my iPad1 your sound freezes the fps a little bit…=> not ideal

.@spacemonkey i have put your code on my repo, you can check it there: http://jmv38.comze.com/CODEAbis/server.php#BouncingBalls Thanks for sharing.