Thermal Ball

Hey guys! I was working on a project today where you can drag a ball and fling it at walls to bounce it around. After working on it for a while, I thought it would be cool if it changed speed depending on how fast it was going. It acts kind of like a thermal detector, so it will turn red when it goes faster, and will cool down to purple when it goes slower.

--# Main
-- Thermal Ball

function setup()
    x = WIDTH/2
    y = HEIGHT/2
    swipe = vec2(0,0)
    drag = vec2(0,0)
    follow = false
    average = vec2(0,0)
    averageTab = {}
    
    backingMode(RETAINED)
    drawHeat()
end

function draw()
    noStroke()
    
    if drag ~= nil then heat = math.abs(drag.x) + math.abs(drag.y)
        else heat = math.abs(swipe.x) + math.abs(swipe.y) end
    heat = math.ceil(heat)
    if heat > 60 then heat = 60 end
    if heat == 0 then heat = 1 end
    local r,g,b = heatImg:get(heat,5)
    dim = 20
    heatColor = color(r-dim,g-dim,b-dim,heat*4)
    fill(heatColor)

    if swipe.x > 30 then swipe.x = 30 end
    if swipe.x < -30 then swipe.x = -30 end
    if swipe.y > 30 then swipe.y = 30 end
    if swipe.y < -30 then swipe.y = -30 end
    if follow == false then
        x = x + swipe.x
        y = y + swipe.y
    end
    if x > WIDTH-50 then swipe.x = -swipe.x;x = WIDTH-50 end
    if x < 50 then swipe.x = -swipe.x;x = 50 end
    if y > HEIGHT-50 then swipe.y = -swipe.y;y = HEIGHT-50 end
    if y < 50 then swipe.y = -swipe.y;y = 50 end
    if swipe ~= 0 then swipe = swipe * 0.99 end
    ellipse(x,y,100)
    
    fill(0,0,0,20)
    rect(0,0,WIDTH,HEIGHT)
end

function touched(touch)
    if touch.state == BEGAN and vec2(touch.x,touch.y):dist(vec2(x,y)) <= 50 then
        follow = true
        id = touch.id
    end
    if touch.state ~= ENDED and follow == true and id == touch.id then
        x = touch.x
        y = touch.y
        drag = vec2(touch.deltaX,touch.deltaY)
        for i = 1,4,-1 do averageTab[i+1] = averageTab[i] end
        averageTab[1] = vec2(touch.deltaX,touch.deltaY)
    elseif follow == true and id == touch.id then
        for i = 1,#averageTab do average = average + averageTab[i] end
        drag = nil
        swipe = average/#averageTab
        average = vec2(0,0)
        averageTab = {}
        follow = false
    end

end

function drawHeat()
    local r = color(255,0,0,255)
    local o = color(255, 127, 0, 255)
    local y = color(255, 255, 0, 255)
    local g = color(0, 255, 0, 255)
    local b = color(0, 255, 255, 255)
    local i = color(0, 0, 255, 255)
    local v = color(127, 0, 255, 255)
    
    heatImg = image(60,10)
    heatMesh = mesh()
    heatMesh.vertices = {
        vec2(0,10),vec2(0,0),vec2(10,0), vec2(0,10),vec2(10,0),vec2(10,10),
        vec2(10,10),vec2(10,0),vec2(20,0), vec2(10,10),vec2(20,0),vec2(20,10),
        vec2(20,10),vec2(20,0),vec2(30,0), vec2(20,10),vec2(30,0),vec2(30,10),
        vec2(30,10),vec2(30,0),vec2(40,0), vec2(30,10),vec2(40,0),vec2(40,10),
        vec2(40,10),vec2(40,0),vec2(50,0), vec2(40,10),vec2(50,0),vec2(50,10),
        vec2(50,10),vec2(50,0),vec2(60,0), vec2(50,10),vec2(60,0),vec2(60,10)
    }
    heatMesh.colors = {v,v,i,v,i,i,i,i,b,i,b,b,b,b,g,b,g,g,g,g,y,g,y,y,y,y,o,y,o,o,o,o,r,o,r,r}
    setContext(heatImg)
    heatMesh:draw()
    setContext()
end

Nice work!

Yep, meshes aren’t that scary

unique Thermal Ball