Just junk to pass the time

Nothing better to do, so I took one of my existing programs and modified it a little. Once this is loaded, tap the screen. My wife is working puzzles to pass the time and I showed this to her and said it would make a good puzzle. She said no.

If you have some interesting code, post it here. Try to keep it small.

displayMode(FULLSCREEN)

function setup()
    noSmooth()
    rectMode(CORNER)    
    img=image(600,100)    
    setContext(img)
    background(0, 0, 0, 255)    
    fill(255) 
    rect(0,0,600,100)    
    fill(0, 0, 0, 255) 
    ellipse(50,50,20)    
    ellipse(125,75,20) ellipse(175,25,20)    
    ellipse(525,75,20) ellipse(550,50,20) ellipse(575,25,20)
    ellipse(425,75,20) ellipse(425,25,20) ellipse(475,75,20) ellipse(475,25,20)    
    ellipse(350,50,20) ellipse(325,75,20) ellipse(325,25,20)
    ellipse(375,75,20) ellipse(375,25,20)    
    ellipse(225,75,20) ellipse(250,75,20) ellipse(275,75,20)    
    ellipse(225,25,20) ellipse(250,25,20) ellipse(275,25,20)
    setContext()    
    scene = craft.scene()    
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")  
    viewer = scene.camera:add(OrbitViewer, vec3(0), 20, 0, 2000)
    tab={}
    xx=WIDTH//100
    yy=HEIGHT//100
    for x=-xx,xx do
        for y=-yy,yy do
            table.insert(tab,createDice(x,y,0))
        end
    end    
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw() 
    if ss then
        for a,b in pairs(tab) do        
            b.m.rotation=quat.eulerAngles(b.rx,b.ry,b.rz)
            b.rx=b.rx+b.vx/20
            b.ry=b.ry+b.vy/20
            b.rz=b.rz+b.vz/20
        end
    end
end

function touched(t)
    if t.state==BEGAN then
        ss=not ss       
    end
end

function createDice(px,py,pz)
    local rx,ry,rz=math.random(360),math.random(360),math.random(360)
    local vx,vy,vz=math.random(-10,10),math.random(-10,10),math.random(-10,10)
    local m=scene:entity()
    m.position=vec3(px,py,pz)
    m.rotation=quat.eulerAngles(rx,ry,rz)
    m.model = craft.model.cube(vec3(.9,.9,.9))
    m.material = craft.material(asset.builtin.Materials.Standard)
    m.material.map = img      
    temp=m.model.indices
    for z=#m.model.indices,1,-1 do
        table.insert(temp,m.model.indices[z])
    end
    m.model.indices=temp      
    local uvs1={}
    c=0
    for x=1,6 do
        table.insert(uvs1,vec2(c/6,0))
        table.insert(uvs1,vec2((c+1)/6,0))
        table.insert(uvs1,vec2((c+1)/6,1))
        table.insert(uvs1,vec2(c/6,1))
        c=c+1
    end
    m.model.uvs=uvs1  
    return({m=m,rx=rx,ry=ry,rz=rz,vx=vx,vy=vy,vz=vz})  
end

@dave1707, the isolation is getting to you! Sometimes the numbers on the sides of the die seem to disappear?

@piinthesky The disappearance is just the reflection from the light source. I change the above code for the material from specular to standard. That elimated the disappearance. You still see a flash, but it doesn’t cover the whole side. I’m getting tired of the stay at home order, but that’s better than the alternative.

I modified the above code so you can rotate the wall of dice.

displayMode(FULLSCREEN)

function setup()
    noSmooth()
    rectMode(CORNER)    
    img=image(600,100)    
    setContext(img)
    background(0, 0, 0, 255)    
    fill(255) 
    rect(0,0,600,100)    
    fill(0, 0, 0, 255) 
    ellipse(50,50,20)    
    ellipse(125,75,20) ellipse(175,25,20)    
    ellipse(525,75,20) ellipse(550,50,20) ellipse(575,25,20)
    ellipse(425,75,20) ellipse(425,25,20) ellipse(475,75,20) ellipse(475,25,20)    
    ellipse(350,50,20) ellipse(325,75,20) ellipse(325,25,20)
    ellipse(375,75,20) ellipse(375,25,20)    
    ellipse(225,75,20) ellipse(250,75,20) ellipse(275,75,20)    
    ellipse(225,25,20) ellipse(250,25,20) ellipse(275,25,20)
    setContext()    
    scene = craft.scene()    
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")  
    viewer = scene.camera:add(OrbitViewer, vec3(0), 20, 0, 2000)
    tab={}
    xx=WIDTH//100
    yy=HEIGHT//100
    for x=-xx,xx do
        for y=-yy,yy do
            table.insert(tab,createDice(x,y,0))
        end
    end    
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw() 
    for a,b in pairs(tab) do        
        b.m.rotation=quat.eulerAngles(b.rx,b.ry,b.rz)
        b.rx=b.rx+b.vx/20
        b.ry=b.ry+b.vy/20
        b.rz=b.rz+b.vz/20
    end
end

function createDice(px,py,pz)
    local rx,ry,rz=math.random(360),math.random(360),math.random(360)
    local vx,vy,vz=math.random(-10,10),math.random(-10,10),math.random(-10,10)
    local m=scene:entity()
    m.position=vec3(px,py,pz)
    m.rotation=quat.eulerAngles(rx,ry,rz)
    m.model = craft.model.cube(vec3(1,1,1))
    m.material = craft.material(asset.builtin.Materials.Standard)
    m.material.map = img      
    temp=m.model.indices
    for z=#m.model.indices,1,-1 do
        table.insert(temp,m.model.indices[z])
    end
    m.model.indices=temp      
    local uvs1={}
    c=0
    for x=1,6 do
        table.insert(uvs1,vec2(c/6,0))
        table.insert(uvs1,vec2((c+1)/6,0))
        table.insert(uvs1,vec2((c+1)/6,1))
        table.insert(uvs1,vec2(c/6,1))
        c=c+1
    end
    m.model.uvs=uvs1  
    return({m=m,rx=rx,ry=ry,rz=rz,vx=vx,vy=vy,vz=vz})  
end

Here’s another change.

displayMode(FULLSCREEN)

function setup()
    noSmooth()
    rectMode(CORNER)    
    img=image(600,100)    
    setContext(img)
    background(0, 0, 0, 255)    
    fill(255) 
    rect(0,0,600,100)    
    fill(0, 0, 0, 255) 
    ellipse(50,50,20)    
    ellipse(125,75,20) ellipse(175,25,20)    
    ellipse(525,75,20) ellipse(550,50,20) ellipse(575,25,20)
    ellipse(425,75,20) ellipse(425,25,20) ellipse(475,75,20) ellipse(475,25,20)    
    ellipse(350,50,20) ellipse(325,75,20) ellipse(325,25,20)
    ellipse(375,75,20) ellipse(375,25,20)    
    ellipse(225,75,20) ellipse(250,75,20) ellipse(275,75,20)    
    ellipse(225,25,20) ellipse(250,25,20) ellipse(275,25,20)
    setContext()    
    scene = craft.scene()    
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")  
    viewer = scene.camera:add(OrbitViewer, vec3(0), 20, 0, 2000)
    tab={}
    for x=-10,10 do
        for y=-10,10 do
            table.insert(tab,createDice(x,y,math.random(-10,10)))
        end
    end    
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw() 
    for a,b in pairs(tab) do        
        b.m.rotation=quat.eulerAngles(b.rx,b.ry,b.rz)
        b.rx=b.rx+b.vx/20
        b.ry=b.ry+b.vy/20
        b.rz=b.rz+b.vz/20
    end
end

function createDice(px,py,pz)
    local rx,ry,rz=math.random(360),math.random(360),math.random(360)
    local vx,vy,vz=math.random(-10,10),math.random(-10,10),math.random(-10,10)
    local m=scene:entity()
    m.position=vec3(px,py,pz)
    m.rotation=quat.eulerAngles(rx,ry,rz)
    m.model = craft.model.cube(vec3(1,1,1))
    m.material = craft.material(asset.builtin.Materials.Standard)
    m.material.map = img      
    temp=m.model.indices
    for z=#m.model.indices,1,-1 do
        table.insert(temp,m.model.indices[z])
    end
    m.model.indices=temp      
    local uvs1={}
    c=0
    for x=1,6 do
        table.insert(uvs1,vec2(c/6,0))
        table.insert(uvs1,vec2((c+1)/6,0))
        table.insert(uvs1,vec2((c+1)/6,1))
        table.insert(uvs1,vec2(c/6,1))
        c=c+1
    end
    m.model.uvs=uvs1  
    return({m=m,rx=rx,ry=ry,rz=rz,vx=vx,vy=vy,vz=vz})  
end

@dave1707 - you certainly use your time well and produce some interesting code. Very impressive both in terms of the idea and the output. Seeing this will drive my grandkids wild - they only ever get to handle two dice at most.

Seriously - your demo has made me think of finishing off some old code so thanks for the prompt.

nice!

@dave1707 that’s awesome, wasn’t expecting such an interesting effect from such a small amount of code

@Simeon Thanks for the comment.

Here’s another version of the code. This one takes a little time before it starts.

displayMode(FULLSCREEN)

function setup()
    rectMode(CORNER)    
    scene = craft.scene()    
    assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")  
    viewer = scene.camera:add(OrbitViewer, vec3(0), 25, 0, 2000)
    tab={}
    for x=-10,10 do
        for y=-10,10 do
            createImg()
            table.insert(tab,createDice(x,y,math.random(-10,10)))
        end
    end   
end
    
function createImg()
    img=image(600,100)    
    setContext(img)
    background(0, 0, 0, 255)    
    fill(math.random(255),math.random(255),math.random(255)) 
    rect(0,0,600,100) 
    fill(math.random(255),math.random(255),math.random(255)) 
    ellipse(50,50,20)    
    ellipse(125,75,20) ellipse(175,25,20)    
    ellipse(525,75,20) ellipse(550,50,20) ellipse(575,25,20)
    ellipse(425,75,20) ellipse(425,25,20) ellipse(475,75,20) ellipse(475,25,20)    
    ellipse(350,50,20) ellipse(325,75,20) ellipse(325,25,20)
    ellipse(375,75,20) ellipse(375,25,20)    
    ellipse(225,75,20) ellipse(250,75,20) ellipse(275,75,20)    
    ellipse(225,25,20) ellipse(250,25,20) ellipse(275,25,20)
    setContext()    
end

function update(dt)
    scene:update(dt)
end

function draw()
    update(DeltaTime)
    scene:draw() 
    for a,b in pairs(tab) do        
        b.m.rotation=quat.eulerAngles(b.rx,b.ry,b.rz)
        b.rx=b.rx+b.vx/20
        b.ry=b.ry+b.vy/20
        b.rz=b.rz+b.vz/20
    end
end

function createDice(px,py,pz)
    local rx,ry,rz=math.random(360),math.random(360),math.random(360)
    local vx,vy,vz=math.random(-10,10),math.random(-10,10),math.random(-10,10)
    local m=scene:entity()
    m.position=vec3(px,py,pz)
    m.rotation=quat.eulerAngles(rx,ry,rz)
    m.model = craft.model.cube(vec3(1,1,1))
    m.material = craft.material(asset.builtin.Materials.Standard)
    m.material.map = img      
    temp=m.model.indices
    for z=#m.model.indices,1,-1 do
        table.insert(temp,m.model.indices[z])
    end
    m.model.indices=temp      
    local uvs1={}
    c=0
    for x=1,6 do
        table.insert(uvs1,vec2(c/6,0))
        table.insert(uvs1,vec2((c+1)/6,0))
        table.insert(uvs1,vec2((c+1)/6,1))
        table.insert(uvs1,vec2(c/6,1))
        c=c+1
    end
    m.model.uvs=uvs1  
    return({m=m,rx=rx,ry=ry,rz=rz,vx=vx,vy=vy,vz=vz})  
end

@dave1707 - excellent, very impressive. Couple of problems though. Firstly, when I initially ran I waited for several minutes looking at a grey screen. Had to close Codea as the bottom corner controls not working. Then set it up with a variable for number of dice and started at 4. The problem then manifested as an error came up for setting up the camera as a dependency. Not seen with -10 to 10.

Then ran at -10 to 10 and Codea bombed. At -8 to 8 it ran without bombing, above that Codea bombed.

@dave1707 - added noSmooth() to your texture routine to remove edge lines on dice.

@Simeon - noticed a trivial issue, not a bug. When you rename a project the commented title in the project at the top of the code retains the old name. Does it need changing - probably not. To be honest only just noticed this as I rarely rename a project.

@Bri_G It doesn’t crash on my iPad Air 3, but I figured it was taking a lot of memory since it’s creating a new img for each dice. I wonder if collectgarbage in createImg() would stop the crash. I took noSmooth() out of this version because I thought it gave the dice a different look. I’ll try this on my older iPad and see if the collectgarbage helps.

PS. It takes about 3 seconds before the dice show on my Air 3.

@dave1707 - my iPad Pro has 37.4 gb when started, 37.2 gb when running Codea and a running dice project (-8 to 8).the collectgarbage() doesn’t work when added to the draw() or setup() (after the dice texture definitions). Still bombs.

Try switching off the camera dependency on your iPad whilst running this.

Edit: Just thinking about this, if you delete your dependency it will probably come straight back with a message on your iPad, the slide on parameter window working. On mine it freezes, no parameter window access no controls at bottom just a grey screen and a long wait for nothing to happen.

@Bri_G The code crashes on my iPad Air 1. I can only do a range of -6 to 6 for it to run. That only has a total of 16GB of memory. On my iPad Pro 1 with 128 GB memory, I have no problems at all. It does the -10 to 10 range and loads the dice in about 5 sec. That’s without the garbagecollect. The garbagecollect doesn’t make any difference on my iPad Air 1. It still crashes at -10,10.

PS. On my iPad Air 1, it gave me the dependency error message the first time. After I added the dependency, it then crashed.

@Simeon @Bri_G When I run my latest dice code, I notice a slight pause after the code has been running about 20-30 seconds. It repeats the pause later also. Sometimes the pause is so slight that if you blink you miss it. Other times it’s more noticeable. It’s so slight that if you’re not looking for it you’ll miss it. Would it be the automatic garbagecollect kicking in.

@Simeon @dave1707 - interesting, I have been working on a project with a star field and I noticed a slight flicker in that. Haven’t pursued it further yet to see if the flicker is due to transitions in the code at regular intetvals. Will checkout further.

@Simeon did you check out the colour change issue?

definitely pauses on my older ipad. at first it seems every ten second, but after a few pauses, less frequently but still approx 10*n seconds

I went back and watched the first program and that one also has a slight pause about 20-30 seconds after I tap the screen to start it.

Looked at the second dice project and that flickered after about 30 seconds, my own star field flickered after 15 seconds.