Path drawing

Hi @wave I like how you have the multiple paths working. If I do things slow, the program seems to work just fine, so you have everything set up. Each of the balls will follow their path, but if I try to move one while another one is moving then things get messed up. Also, every now and then, things just get messed up moving one ball. So I see what kind of problem you’re having. I’ll look into it today while I’m watching football and I should have the problem worked out later.

Hi @wave All 8 circles can be moved at the same time now without problems so far. I made changes that allowed code to be removed. Compare this to what you had to see what I changed.

EDIT: added limit to allow changing number of circles. Changed from 8 to 15 circles. Also changed constant speed from 5 to 2.


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()   
    init()
end

function init()
    path=0    -- path that's being drawn
    limit=15
    tab1={}
    offset = {}
    mx = {}
    my = {}
    dx = {}
    dy = {}
    move = {}
    for w = 1, limit do
        tab1[w] = {}
        move[w] = false
        offset[w] = 1
        dx[w] = 0
        dy[w] = 0
        mx[w] = 60*w
        my[w] = 125
    end
end

function draw()
    background(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    for w = 1, limit do
        local a; local b
        -- draw remaining dots of each path
        for z=offset[w]+1,#tab1[w] do
            stroke(255, 255, 255, 255)
            strokeWidth(3)
            ellipse(tab1[w][z-1].x,tab1[w][z-1].y,3)
        end 
        -- calculate distance to move circles at constant speed
        if move[w] then
            a=math.sqrt(dx[w]*dx[w]+dy[w]*dy[w])
            b=2/a
            mx[w] = mx[w] + dx[w]*b
            my[w] = my[w] + dy[w]*b
            moveCircle(w)
        end 
        -- move circles along path    
        ellipse(mx[w],my[w],40,40)
    end
end

function calcDist(hx,hy) -- don't save points within 20 pixels of the previous point
    local t=#tab1[path]
    local a; local b
    if t>1 then
        a=hx-tab1[path][t].x
        b=hy-tab1[path][t].y
        if a*a+b*b>400 then
            return true    -- less than 20 pixels from last point
        end
        return false    -- point to close to last point
    end
    return true    -- allow first point to be saved
end

function touched(touch)
    if touch.state==BEGAN then
        path=0    -- clear which path was being drawn
        for w = 1, limit do    -- determine which circle is being selected
            if touch.x > (mx[w] - 40) and touch.x < (mx[w] + 40) and
               touch.y > (my[w] - 40) and touch.y < (my[w] + 40) and
                not move[w] then
                    path=w     -- save the current path that's being drawn
            end       
        end
    elseif touch.state==MOVING and path>0 then 
        if calcDist(touch.x,touch.y) then
            table.insert(tab1[path],vec2(touch.x,touch.y))    -- insert points in table
        end
    elseif touch.state==ENDED and path>0 then
            move[path]=true
            dx[path]=(tab1[path][offset[path]].x-mx[path])
            dy[path]=(tab1[path][offset[path]].y-my[path])          
    end
end

function moveCircle(w)
    local a; local b; local abh
    if move[w] then
        if offset[w]==#tab1[w] then
            tab1[w] = {}
            offset[w]=1
            dx[w]=0
            dy[w]=0
            move[w]=false
            return
        end
        a=math.abs(tab1[w][offset[w]].x-mx[w])
        b=math.abs(tab1[w][offset[w]].y-my[w])
        ab=math.sqrt(a*a+b*b)
        if ab<5 then
            offset[w] = offset[w] + 1
            dx[w]=tab1[w][offset[w]].x-mx[w]
            dy[w]=tab1[w][offset[w]].y-my[w]
        end
    end
end

@dave1707

Thank you again for helping me. It’s now run very well.

(I wonder in the previous code what does the line “and not en” do after touch.state == BEGAN)

@wave I don’t remember what that was originally for. It might have been a typo or something else that didn’t affect the way the program ran, so I never caught it. I’m going to try and contact you thru the send message option that shows when I tap on your name. I want to know what you’re going to try and do with this code. I already have a game written that uses multiple paths, but I don’t want to post it if it’s similar to something you’re trying to write and show here.