Here’s my multiple dice program with the quat change suggested by @RonJeffries.
@Simeon If I let this program run, it eventually crashes Codea because of memory. 2 things I noticed. It pauses for different lengths of time at about 15 seconds intervals. Sometimes it’s barely noticeable, other times it’s about .5 seconds. Also I’m displaying the memory usage at the top middle of the screen. That constantly increases until it crashes. If I uncomment the collectgarbage() in the draw function, memory usage remains low and I don’t notice the pauses. It’s like the auto collectgarbage can’t keep up.
viewer.mode=FULLSCREEN
-- display a bunch of dice
function setup()
sp=.01
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)
createImg()
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()
noSmooth()
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)
noStroke()
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)
stroke(200)
strokeWidth(2)
noFill()
for z=0,5 do
rect(z*100,0,101,101)
end
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=b.m.rotation*quat.eulerAngles(b.rx,b.ry,b.rz)
end
fill(255)
text(collectgarbage("count")//1,WIDTH/2,HEIGHT-25)
--collectgarbage()
end
function createDice(px,py,pz)
local rx,ry,rz=math.random(-100,100)*sp,math.random(-100,100)*sp,math.random(-100,100)*sp
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.Basic)
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