A Jostling Amusement

So I came up with this sketch this evening and thought I might share it and perhaps learn how I might optimize it.

It seems to work – most of the time – on my iPad 2. Not sure about the first generation device. I am using the DragMe class that @Simeon showed me a while back.

Edit: I should point out that for me the fun began when I wrote the checkDist() function – which now, more appropriately, could be renamed: jostle().


--  a Jostling Amusement

function setup()
    displayMode(FULLSCREEN_NO_BUTTONS)
    --displayMode(FULLSCREEN)

    num=20
    boxes={}
    for i=1,num do
        boxes[i]=DragMe()
    end
 end

function draw()
    background(0)
    for i=1,num do
        boxes[i]:draw()
    end 
    connect()  
    quiver()
    checkDist()
end

function connect() --draw a line connecting box centers
    for i=1,num-1 do
        strokeWidth(3)
        stroke(255)
        line(boxes[i].pos.x,boxes[i].pos.y,boxes[i+1].pos.x,boxes[i+1].pos.y)
    end   
end

function quiver()
    for i=1,num do
        boxes[i].pos.x=boxes[i].pos.x + (math.sin(boxes[i].pos.x*5))
        boxes[i].pos.y=boxes[i].pos.y + (math.sin(boxes[i].pos.y*5))
    end
end

function checkDist()
    for i=1,num do
        for j=1,num do
            if boxes[i] ~= boxes[j] then
                distX=math.abs(boxes[i].pos.x - boxes[j].pos.x)
                distY=math.abs(boxes[i].pos.y - boxes[j].pos.y)
                while distX <= 100 and distY <= 100 do
                    boxes[i].pos.x=boxes[i].pos.x+math.random(-1,1)
                    boxes[j].pos.x=boxes[j].pos.x+math.random(-1,1)
                    boxes[i].pos.y=boxes[i].pos.y+math.random(-1,1)
                    boxes[j].pos.y=boxes[j].pos.y+math.random(-1,1)
                    distX=math.abs(boxes[i].pos.x - boxes[j].pos.x)
                    distY=math.abs(boxes[i].pos.y - boxes[j].pos.y)
                end
            end
        end
    end
end


function touched(touch)  
    for i=1,num do
        boxes[i]:touched(touch)
    end
end

-- DragMe

DragMe = class()

function DragMe:init()
    self.pos = vec2(math.random(50,WIDTH-50),math.random(50,HEIGHT-50))
    self.fill=color(math.random(255),math.random(255),math.random(255),255)
    self.size=75 --math.random(25,100)
end

function DragMe:draw()
    pushStyle()
    fill(self.fill)
    rectMode(CENTER)
    rect(self.pos.x, self.pos.y, self.size, self.size)
    popStyle()
end

function DragMe:hit(point)
    if point.x > (self.pos.x - self.size/2) and
       point.x < (self.pos.x + self.size/2) and
       point.y > (self.pos.y - self.size/2) and
       point.y < (self.pos.y + self.size/2) then
        return true
    end        
    return false
end

function DragMe:touched(touch)
    if self:hit( vec2(touch.x, touch.y) ) and
       touch.state == MOVING then
        self.pos=self.pos+vec2(touch.deltaX,touch.deltaY)
    end
end

It works fine on iPad1, I don’t see any optimizations. You could use the vec2 function dist yo do a round distance rather than square

Thanks for looking at it and for your tip! I’ll experiment with the dist function for calculating proximity and compare it with what I’m using now. It’s definitely time to go back and redo your vec2 tutorial. :slight_smile:

I’m also considering:

(1) changing the color of the blocks (warm vs cool) based on their degree of being jostled

(2) making some blocks more sensitive/aggressive

very funny idea :smiley:

also a possibility:

high aggressivity → warm color

low aggressivity → cold color