tree growht simulator

hi all
i’m new in the programming codea world
I love the codea app best thing happend for this year.
anyway
i’m building a tree growth simulator from examples on this forum .

it works great
the tree is growing but the ipad is slow or the code is not efficient for the work.
got a little idea for wind wenn you touch wind will blow.
and implemented a sun for the power to let the tree grow.

from zero skill still happy with the progress

here is the messy code 0.001

function setup()
    --
    
    --clouds = CloudLevels()
    smooth()
    displayMode(STANDARD)
    Sx = WIDTH/2
    Sy = 0
    -- initialise the drawing angle
    theta = 0    
    -- initial drawing length
    length = 100
    parameter.number("n" ,0.1,18,13.5)
    parameter.number("length",1,200,1)
    parameter.number("factor",0,0.70,0.5)
    
    parameter.number("seed",-1.5,1.5,0.75)
    parameter.number("seed2",-1.5,1.5,0.75)
    parameter.number("theta",-100,100,75)
    backingMode(RETAINED)
    parameter.number("wind",-1,0.5,0)
    wolkx = 0
    wolky = 0
    zony = 0
    parameter.boolean("waaien")
    parameter.boolean("zon")
    parameter.number("zony",0,700,1)
    parameter.number("zonx",0,WIDTH,1)
    parameter.number("Freq",0,5,1)
    parameter.integer("Texture",1,5,1)
-- shader
    allTextures = {
                    
                    "Planet Cute:Tree Ugly",
                    "Cargo Bot:Codea Icon",
                    "Small World:Store Extra Large",
                    "Small World:Windmill",
                    "Tyrian Remastered:Boss D",
                  }

    cameraSource(CAMERA_FRONT)

    m = mesh()
    m.texture = allTextures[Texture]
    m.shader = shader("Effects:Ripple")

    rIdx = m:addRect(0, 0, 0, 0)
    --m:setRectColor(i, 255,0,0)
end

function draw()
--

    background(0, 167, 255, 255)
   stroke(40, 255, 0, 255)
   fill(16, 229, 8, 255)
   rect(0,0,WIDTH,64)
--sound(DATA, "ZgBAHAA/Rw4/PwNL1zPDPjTeZj8sElA9QAAKf0JnYi8/WBtu")
sprite("Small World:Dialog Thought",wolkx,wolky,30,30)
    -- move to horisontal middle of screen at zero height
   -- clouds:draw()
       math.randomseed(wind)
        --wind =  wind * math.random(0.99,1.1)
       -- if seed < 1.2 and seed > -0.8 then
        seed = wind + seed
        seed2  = wind + seed2
    if waaien == true then
      if wind >= -0.5 then
        wind = perturb(wind) --wind  + math.random(0,0.5)
        else
            wind =  perturb(-wind) --wind - math.random(-0.5,0)
            end
            end
    if zon == true then
        if zony <= 700 or zony > 700 then
            zony = zony + 1
            end
            if zonx > 0 then
                zonx = zonx + 1
                end
      
      --  resetStyle()
        sprite("Small World:Mote Happy",zonx,zony - 100,30,30)      
              if zony > HEIGHT/2 then
            
               if factor <= 0.75 then
      factor = factor *1.001
    end
    if length <= 200 then
    length = length *1.002
    end
    end
    end
            
   translate(Sx,Sy + 64)
       
    -- inialise end of line point
    S2x = 0
    S2y =  length
    -- set up line drawing parameters
    stroke(178, 129, 21, 255)
    lineCapMode(ROUND)   
    strokeWidth(length*0.1618)
    -- draw first line and move to top of it
    line(0, 0,S2x,S2y)
    translate(0, length)
    -- call the iterative pattern routine
    dragon(length)
 

    

end

function dragon(length)
    -- reduce the line length each iteration
    local size = (length*0.9)* factor
    if size > n then
        -- draw lines in one direction
        pushMatrix()
            --rotate(theta)
            rotate(seed*theta * seed)
           strokeWidth(size*0.1618)
             stroke(164, 102, 73, 255)
          line(0,0,0,size)
            translate(0,size)
            -- recursive self call
            dragon(size)
        popMatrix()
        -- now draw them in the opposite direction
        pushMatrix()
           -- rotate(-theta)
            rotate(seed2*(-theta ))
           strokeWidth(size*0.1618)
             stroke(173, 83, 54, 255)
           line(0,0,0,size)
            translate(0,size)
            -- recursive self call            
            dragon(size)
        popMatrix()
        else -- sprite or green ellipse
           --noStroke()
     -- stroke(0, 255, 61, 22)
    -- fill(53, 255, 0, 22)
     -- ellipse(0, 0, S2y * 0.25, S2y * 0.25)
   -- tint(3, 255, 0, 33)
    --sprite("Planet Cute:Tree Ugly",0,0,S2y*0.75,S2y*0.75)
    --shader sprie zone
         m.texture = allTextures[Texture]
    local cw,ch = spriteSize(allTextures[Texture])
    m:setRect(rIdx, 0, 0, cw, ch)

    -- Configure out custom uniforms for the ripple shader
    m.shader.time = ElapsedTime
    m.shader.freq = Freq
    
    -- Draw the mesh
    m:draw()
    end
    parameter.watch("wind")
end

function touched(touch)
    -- adjust the line drawing angle based on x value from CurrentTouch
    --theta = 90*-((-CurrentTouch.x)/(WIDTH)) 
    --wind = 
   -- seed = 2.5*((CurrentTouch.y)/(HEIGHT))
  --  seed2 = 2.5*((CurrentTouch.y)/(HEIGHT))
    -- adjust theta as you approach extremes due to my fat fingers
   wolkx = CurrentTouch.x
wolky = CurrentTouch.y
   -- if theta > 88 then theta = theta + 1  end
   -- seed = seed +  1
if touch.state == BEGAN or touch.state == MOVING  then
        if touch.y > HEIGHT then
    seed = -((CurrentTouch.y)/(HEIGHT))
    seed2 = ((CurrentTouch.y)/(HEIGHT))
    
   -- wind  =  

        else
              --  wind = wind * ((CurrentTouch.y)/(HEIGHT))
    seed = -((CurrentTouch.y)/(HEIGHT))
    seed2 = ((CurrentTouch.y)/(HEIGHT))
        end
        if touch.x > WIDTH/2 then
      if theta > -100 then
        theta = theta - 5
        end
        else
            if theta < 100 then
            theta = theta + 5
            end
        end
    elseif touch.state == ENDED then
        --wind = 1
        --self.leftJoint.motorSpeed = 0
    end
end

    
 
function perturb(v)
    return v * math.random(0.9, 1.1)
end

nice :smiley: i would like to see a more complicated version in 3D … hmm

i tried already something out in the simple 3d example

--main code --



-- Use this function to perform your initial setup
function setup()
        
    
    --smooth()
    displayMode(STANDARD)
    blendMode(NORMAL)   
    
    Sx = WIDTH/2
    Sy = 0
    -- initialise the drawing angle
    theta = 0    
    -- initial drawing length
    length = 100
    parameter.number("n" ,0.1,18,13.5)
    parameter.number("length",1,200,1)
    parameter.number("factor",0,0.70,0.5)
    
    parameter.number("seed",-1.5,1.5,0.75)
    parameter.number("seed2",-1.5,1.5,0.75)
    parameter.number("theta",-100,100,75)
    backingMode(RETAINED)
    parameter.number("wind",-1,0.5,0)
    wolkx = 0
    wolky = 0
    zony = 0
    parameter.boolean("waaien")
    parameter.boolean("zon")
    parameter.number("zony",0,700,1)
    parameter.number("zonx",0,WIDTH,1)
    parameter.number("Freq",0,5,1)
    parameter.integer("Texture",1,5,1)
-- shader
    allTextures = {
                    
                    "Planet Cute:Tree Ugly",
                    "Cargo Bot:Codea Icon",
                    "Small World:Store Extra Large",
                    "Small World:Windmill",
                    "Tyrian Remastered:Boss D",
                  }

    cameraSource(CAMERA_FRONT)

    m = mesh()
    m.texture = allTextures[Texture]
    m.shader = shader("Effects:Ripple")

    rIdx = m:addRect(0, 0, 0, 0)
    --m:setRectColor(i, 255,0,0)
    displayMode(STANDARD)
 
    availableTests = { Test1(), Test2() }
    currentTest = availableTests[1]

    parameter.integer("SceneSelect",1,#availableTests,1)
    parameter.number("hoek",0,46,10)
    parameter.number("factor",0,20,0)
    parameter.number("Size",50,500,150)
    parameter.number("CamHeight", 0, 1000, 300)
    parameter.number("Angle",-360, 360, 0)
    parameter.number("FieldOfView", 10, 140, 45)
    
    
    the3DViewMatrix = viewMatrix()
    parameter.watch("the3DViewMatrix")
    
    -- These watches are evaluated after draw() is finished
    -- Thus the contents are not as interesting
    parameter.watch("viewMatrix()")
    parameter.watch("modelMatrix()")
    parameter.watch("projectionMatrix()")
end

-- This function gets called once every frame
function draw()
    -- Set the currentTest to the selected scene
    currentTest = availableTests[SceneSelect]
    
    -- First arg is FOV, second is aspect
    perspective(FieldOfView, WIDTH/HEIGHT)
 
    -- Position the camera up and back, look at origin
    camera(0,CamHeight,-300, 0,0,0, 0,1,0)
    
    -- Write this into a variable so we can watch() it
    -- at this point in time
    the3DViewMatrix = viewMatrix()
    
    -- This sets a dark background color 
    background(39, 39, 50, 0)

    -- Do your drawing here
    currentTest:draw()    
    
    -- Restore orthographic projection
    ortho()
    
    -- Restore the view matrix to the identity
    viewMatrix(matrix())
    
    -- Draw a label at the top of the screen
    fill(255)
    font("MyriadPro-Bold")
    fontSize(30)
    
    text(currentTest:name(), WIDTH/2, HEIGHT - 30)
end


-- end main code --
put this in a class

--like class = test1 code beneat
-------

Test1 = class()

function Test1:name()
    return "Codea Primitives in 3D"
end

function Test1:init()
    -- you can accept and set parameters here
end

function Test1:draw()    
    -- Preserve existing transform and style
    pushMatrix()
    pushStyle()
    
    -- This sets the line thickness
    strokeWidth(5)
 
    smooth()
    rectMode(CENTER)
    lineCapMode(PROJECT)
 
    -- Make a floor
    translate(0,-Size/2,0)
    rotate(Angle,0,1,0)
    rotate(90,1,0,0)
    sprite("Small World:Grass Patch", 0, 0, 300, 300)
 
    -- Rotate and translate the square
    resetMatrix()
    rotate(Angle,0,1,0)
    --translate(0, 0, -100)
           math.randomseed(wind)
        --wind =  wind * math.random(0.99,1.1)
       -- if seed < 1.2 and seed > -0.8 then
        seed = wind + seed
        seed2  = wind + seed2
    if waaien == true then
      if wind >= -0.5 then
        wind = perturb(wind) --wind  + math.random(0,0.5)
        else
            wind =  perturb(-wind) --wind - math.random(-0.5,0)
            end
            end
    if zon == true then
        if zony <= 700 or zony > 700 then
            zony = zony + 1
            end
            if zonx > 0 then
                zonx = zonx + 1
                end
      
      --  resetStyle()
        sprite("Small World:Mote Happy",zonx,zony - 100,30,30)      
              if zony > HEIGHT/2 then
            
               if factor <= 0.75 then
      factor = factor *1.001
    end
    if length <= 200 then
    length = length *1.002
    end
    end
    end
            
   --translate(Sx,Sy + 64)
       
    -- inialise end of line point
    S2x = 0
    S2y =  length
    -- set up line drawing parameters
    stroke(178, 129, 21, 255)
    lineCapMode(ROUND)   
    strokeWidth(length*0.1618)
    -- draw first line and move to top of it
    line(0, 0,S2x,S2y)
    translate(0, length)
    -- call the iterative pattern routine
    dragon(length)
 
    --fill(44, 97, 161, 255)
    --rect(0, 0, Size, Size)
 
    --resetMatrix()
    --rotate(Angle,0,1,0)
    --fill(191, 26, 26, 255)
    ----ellipse(0, 0, Size*0.8)
    
    -- Restore transform and style
    popStyle()
    popMatrix()
end
 
 
 
function touched(t)
    seed = seed + 1
end
 
function drawTree(depth, length, angle)
    if depth > 0 then
        strokeWidth(length/5)
        stroke(133, 55, 29, 255)
        line(0, 0, 0, length)
        
        pushMatrix()
        translate(0, length)
        pushMatrix()
        rotate(perturb(angle))
        drawTree(depth-1, length*0.8, angle)
        popMatrix()
        pushMatrix()
        rotate(perturb(-angle))
        drawTree(depth-1, length*0.8, angle)
        popMatrix()
        popMatrix()
    else
        noStroke()
        stroke(24, 164, 41, 255)
        fill(23, 164, 40, 93)
        ellipse(0, 0, 32, 32)
    end
end

function dragon(length)
    -- reduce the line length each iteration
    local size = (length*0.9)* factor
    if size > n then
        -- draw lines in one direction
        pushMatrix()
            --rotate(theta)
            rotate(seed*theta * seed)
           strokeWidth(size*0.1618)
             stroke(164, 102, 73, 255)
          line(0,0,0,size)
            translate(0,size)
            -- recursive self call
            dragon(size)
        popMatrix()
        -- now draw them in the opposite direction
        pushMatrix()
           -- rotate(-theta)
            rotate(seed2*(-theta ))
           strokeWidth(size*0.1618)
             stroke(173, 83, 54, 255)
           line(0,0,0,size)
            translate(0,size)
            -- recursive self call            
            dragon(size)
        popMatrix()
        else -- sprite or green ellipse
           --noStroke()
     -- stroke(0, 255, 61, 22)
    -- fill(53, 255, 0, 22)
     -- ellipse(0, 0, S2y * 0.25, S2y * 0.25)
   -- tint(3, 255, 0, 33)
    --sprite("Planet Cute:Tree Ugly",0,0,S2y*0.75,S2y*0.75)
    --shader sprie zone
         m.texture = allTextures[Texture]
    local cw,ch = spriteSize(allTextures[Texture])
    m:setRect(rIdx, 0, 0, cw, ch)

    -- Configure out custom uniforms for the ripple shader
    m.shader.time = ElapsedTime
    m.shader.freq = Freq
    
    -- Draw the mesh
    m:draw()
    end
    parameter.watch("wind")
end

function touched(touch)
    -- adjust the line drawing angle based on x value from CurrentTouch
    --theta = 90*-((-CurrentTouch.x)/(WIDTH)) 
    --wind = 
   -- seed = 2.5*((CurrentTouch.y)/(HEIGHT))
  --  seed2 = 2.5*((CurrentTouch.y)/(HEIGHT))
    -- adjust theta as you approach extremes due to my fat fingers
   wolkx = CurrentTouch.x
wolky = CurrentTouch.y
   -- if theta > 88 then theta = theta + 1  end
   -- seed = seed +  1
if touch.state == BEGAN or touch.state == MOVING  then
        if touch.y > HEIGHT then
    seed = -((CurrentTouch.y)/(HEIGHT))
    seed2 = ((CurrentTouch.y)/(HEIGHT))
    
   -- wind  =  

        else
              --  wind = wind * ((CurrentTouch.y)/(HEIGHT))
    seed = -((CurrentTouch.y)/(HEIGHT))
    seed2 = ((CurrentTouch.y)/(HEIGHT))
        end
        if touch.x > WIDTH/2 then
      if theta > -100 then
        theta = theta - 5
        end
        else
            if theta < 100 then
            theta = theta + 5
            end
        end
    elseif touch.state == ENDED then
        --wind = 1
        --self.leftJoint.motorSpeed = 0
    end
end
       --"rotate(perturb(-angle))"
 --math.randomseed(seed)
function perturb(v)
    return v * math.random(0.95, 1.05)
end

still not true 3d but you can look at a 2d tree in 3d :slight_smile:
but the sprites give black stuff at the edges…

Oof! Triple ~ in front and behind your code, please…