Hi. I made this just for fun. It’s just a proof, intended for noobs and math interested people (on normal distributions, etc.) :-B
Modifications and refinements, like showing the normal distribution of the balls and so, can be made very easy.
I hope you enjoy it. ~O)
Edit: touch the screen to release a ball at a time.
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
-- Board:
walls={}
db = 20 -- distance from the edges
x2 = HEIGHT-300 -- board's height
CreateWalls() -- board's walls
-- Nails:
clavos = {}
xc = 30 -- space between nails
ncc = 46 -- number of nails for each pair of nails' lines
numc = 9 -- *2=nails' lines
dc = 3 -- nail's diameter
for i=1,numc do
clavos[i]={}
for j=1,ncc do
clavos[i][2*j-1] = Pins(dc,j*xc+24,x2-xc*(2*i-1))
clavos[i][2*j] = Pins(dc,j*xc+9,x2-2*i*xc)
end
end
-- Balls:
m = 0 -- touches' counter
p = {} -- balls' vector
dp = 10 -- balls' diameter
end
-- This function gets called once every frame
function draw()
background(142, 198, 218, 255)
stroke(0, 0, 0, 255)
-- nails...
for i=1,numc do
for j=1,ncc do
strokeWidth(2)
fill(136, 135, 124, 255)
ellipse(clavos[i][j].x,clavos[i][j].y,clavos[i][j].radius*2)
end
end
-- balls...
for z=1,m do
fill(255, 86, 0, 255)
ellipse(p[z].x,p[z].y,p[z].radius*2)
end
-- board's walls...
strokeWidth(5)
stroke(137, 95, 44, 255)
for k=1,7 do
drawpoints(walls[k])
end
end
function Pins(d,x,y)
local pin=physics.body(CIRCLE,d)
pin.x=x
pin.y=y
pin.type=STATIC
return pin
end
function shoot(x,xp)
p[x]=physics.body(CIRCLE,dp)
p[x].x=xp
p[x].y=HEIGHT-p[x].radius*2
p[x].linearVelocity=vec2(0,0)
p[x].friction=0.1
p[x].gravityScale=1
p[x].restitution=0.1
end
function touched(t)
if t.state == BEGAN then
m = m + 1
local px=t.x
shoot(m,px)
end
end
function CreateWalls()
walls[1] = CreateWall(db,db,db,x2,STATIC)
walls[2] = CreateWall(WIDTH-db,x2,WIDTH-db,db,STATIC)
walls[3] = CreateWall(db,db,WIDTH-db,db,STATIC)
walls[4] = CreateWall(db,HEIGHT-db,WIDTH/2-20,x2,STATIC)
walls[5] = CreateWall(WIDTH/2+20,x2,WIDTH-db,HEIGHT-db,STATIC)
walls[6] = CreateWall(db,x2,WIDTH/2-20,x2,STATIC)
walls[7] = CreateWall(WIDTH/2+20,x2,WIDTH-db,x2,STATIC)
end
--this function creates a "wall"
function CreateWall(x,y,x1,y1,wtype)
local w = physics.body(EDGE,vec2(x,y),vec2(x1,y1))
w.type=wtype
return w
end
function drawpoints(body)
pushMatrix()
translate(body.x, body.y)
rotate(body.angle)
local points = body.points
local a, b
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
popMatrix()
end