Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@NathanFlurry
Created December 24, 2013 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanFlurry/8108191 to your computer and use it in GitHub Desktop.
Save NathanFlurry/8108191 to your computer and use it in GitHub Desktop.
Shatter a mesh into physical triangles
--# Main
function setup()
m = mesh()
m.visible = true
fragments = {}
parameter.number("meshScale",10,400,150)
parameter.integer("meshShape",1,4,1)
parameter.action("Generate Mesh",setMeshPoints)
parameter.action("Shatter Mesh",shatterMesh)
ground = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
end
function draw()
background(40, 40, 50)
if m.visible then
pushMatrix()
translate(WIDTH/2,HEIGHT/2)
m:draw()
popMatrix()
end
for i,v in ipairs(fragments) do
pushMatrix()
translate(v.x,v.y)
rotate(v.angle)
v.m:draw()
popMatrix()
end
for i = #fragments,1,-1 do
if fragments[i].y < -200 then
fragments[i]:destroy()
table.remove(fragments,i)
end
end
end
function setMeshPoints()
local p
if meshShape == 1 then
p = star(5,15,meshScale*.3,meshScale)
elseif meshShape == 2 then
p = randPoly(10,25,meshScale*.6,meshScale)
elseif meshShape == 3 then
p = stackIt()
for i,v in ipairs(p) do
v.x = v.x * meshScale
v.y = v.y * meshScale
end
else --if meshShape == 4 then
p = cow()
for i,v in ipairs(p) do
v.x = v.x * meshScale
v.y = v.y * meshScale
end
end
m.vertices = triangulate(p)
local colors = {}
for i = 1,#m.vertices do
table.insert(colors, color(math.random()*255,math.random()*255,math.random()*255))
end
m.colors = colors
--m:setColors(255,255,255)
m.visible = true
end
function shatterMesh()
for i = 1,#m.vertices,3 do
local p1 = m.vertices[i]
local p1Col = m.colors[i]
local p2 = m.vertices[i+1]
local p2Col = m.colors[i+1]
local p3 = m.vertices[i+2]
local p3Col = m.colors[i+2]
p1Col.r = p1Col.r*255
p1Col.g = p1Col.g*255
p1Col.b = p1Col.b*255
p1Col.a = p1Col.a*255
p2Col.r = p2Col.r*255
p2Col.g = p2Col.g*255
p2Col.b = p2Col.b*255
p2Col.a = p2Col.a*255
p3Col.r = p3Col.r*255
p3Col.g = p3Col.g*255
p3Col.b = p3Col.b*255
p3Col.a = p3Col.a*255
local f = physics.body(POLYGON,
vec2(p1.x,p1.y),
vec2(p2.x,p2.y),
vec2(p3.x,p3.y))
f.position = vec2(WIDTH/2,HEIGHT/2)
f.linearVeloctiy = vec2(math.random(),math.random())*500
f.m = mesh()
f.m.vertices = {p1,p2,p3}
f.m.colors = {p1Col,p2Col,p3Col}
--f.m:setColors(255,255,255)
table.insert(fragments,f)
end
m.visible = false
end
--# MeshPresets
function randPoly(minSides,maxSides,min,max)
local t = {}
local v,m
local nSides = math.random(minSides,maxSides)
for i = 1,nSides do
v = vec2(math.cos(((math.pi*2)/nSides)*i),math.sin(((math.pi*2)/nSides)*i))
m = math.random(min*100,max*100)/100
table.insert(t,v*m)
end
return t
end
function star(minSides,maxSides,minSize,maxSize)
local rad = math.pi * 2
local sides = math.random(minSides,maxSides)
local points = {}
for i = 1,sides do
table.insert(points, vec2(
math.cos((rad/sides)*i)*minSize,
math.sin((rad/sides)*i)*minSize
)
)
table.insert(points, vec2(
math.cos((rad/sides)*i+(rad/sides/2))*maxSize,
math.sin((rad/sides)*i+(rad/sides/2))*maxSize
)
)
end
return points
end
function stackIt()
return {vec2(0.6740823136818688, -0.5539488320355951), vec2(0.3581757508342603, -0.5539488320355951), vec2(0.4961067853170189, -0.5139043381535039), vec2(0.4961067853170189, -0.3225806451612903), vec2(0.389321468298109, -0.41156840934371525), vec2(0.22469410456062291, -0.407119021134594), vec2(0.12680756395995552, -0.26918798665183535), vec2(0.14905450500556172, -0.10901001112347053), vec2(0.27363737486095663, -0.024471635150166853), vec2(0.3982202447163515, -0.028921023359288096), vec2(0.4961067853170189, -0.12235817575083426), vec2(0.4961067853170189, 0.07341490545050056), vec2(0.25139043381535037, 0.11345939933259176), vec2(0.08231368186874305, 0.028921023359288096), vec2(-0.011123470522803115, -0.11345939933259176), vec2(-0.006674082313681869, -0.3581757508342603), vec2(0.10456062291434928, -0.4916573971078977), vec2(0.2335928809788654, -0.5539488320355951), vec2(-0.18020022246941045, -0.5539488320355951), vec2(-0.18020022246941045, 0.09566184649610679), vec2(-0.3270300333704116, 0.09566184649610679), vec2(-0.3270300333704116, 0.020022246941045607), vec2(-0.4961067853170189, 0.11345939933259176), vec2(-0.6340378197997776, 0.10456062291434928), vec2(-0.7274749721913237, 0.03337041156840934), vec2(-0.8120133481646273, -0.10011123470522804), vec2(-0.8253615127919911, -0.27363737486095663), vec2(-0.7853170189098999, -0.407119021134594), vec2(-0.6518353726362626, -0.5317018909899889), vec2(-0.5583982202447163, -0.5494994438264739), vec2(-1.0211345939933258, -0.5539488320355951), vec2(-1.0211345939933258, -0.03781979977753059), vec2(-0.9098998887652948, -0.03781979977753059), vec2(-0.9098998887652948, 0.09566184649610679), vec2(-1.0211345939933258, 0.09566184649610679), vec2(-1.0211345939933258, 0.3270300333704116), vec2(-1.167964404894327, 0.3270300333704116), vec2(-1.167964404894327, 0.09121245828698554), vec2(-1.2347052280311457, 0.09121245828698554), vec2(-1.2347052280311457, -0.04671857619577308), vec2(-1.1635150166852057, -0.04671857619577308), vec2(-1.1635150166852057, -0.5539488320355951), vec2(-1.5906562847608454, -0.5539488320355951), vec2(-1.4126807563959956, -0.44271412680756395), vec2(-1.3459399332591768, -0.25139043381535037), vec2(-1.385984427141268, -0.09566184649610679), vec2(-1.5150166852057843, 0.028921023359288096), vec2(-1.6974416017797553, 0.10456062291434928), vec2(-1.7953281423804226, 0.19799777530589543), vec2(-1.7864293659621802, 0.28698553948832034), vec2(-1.6796440489432702, 0.3448275862068966), vec2(-1.5639599555061179, 0.3225806451612903), vec2(-1.492769744160178, 0.25139043381535037), vec2(-1.385984427141268, 0.3181312569521691), vec2(-1.457174638487208, 0.42491657397107896), vec2(-1.617352614015573, 0.4872080088987764), vec2(-1.7686318131256953, 0.4694104560622914), vec2(-1.89321468298109, 0.38487208008898777), vec2(-1.9510567296996664, 0.26473859844271413), vec2(-1.9110122358175752, 0.0867630700778643), vec2(-1.7597330367074526, -0.028921023359288096), vec2(-1.546162402669633, -0.14015572858731926), vec2(-1.5061179087875418, -0.22024471635150167), vec2(-1.5595105672969967, -0.3804226918798665), vec2(-1.6796440489432702, -0.42046718576195774), vec2(-1.8086763070077865, -0.35372636262513907), vec2(-1.8442714126807565, -0.2335928809788654), vec2(-2, -0.26473859844271413), vec2(-1.9377085650723025, -0.43381535038932145), vec2(-1.8309232480533926, -0.5272525027808677), vec2(-1.7330367074527253, -0.5583982202447163), vec2(-0.5406006674082313, -0.5583982202447163), vec2(-0.5406006674082313, -0.42936596218020023), vec2(-0.625139043381535, -0.371523915461624), vec2(-0.6740823136818688, -0.26473859844271413), vec2(-0.660734149054505, -0.12680756395995552), vec2(-0.5050055617352615, -0.020022246941045607), vec2(-0.38487208008898777, -0.06896551724137931), vec2(-0.3225806451612903, -0.1757508342602892), vec2(-0.3670745272525028, -0.3670745272525028), vec2(-0.4471635150166852, -0.42046718576195774), vec2(-0.5317018909899889, -0.42936596218020023), vec2(-0.5317018909899889, -0.5539488320355951), vec2(-0.44271412680756395, -0.5450500556173526), vec2(-0.38487208008898777, -0.5183537263626251), vec2(-0.3314794215795328, -0.4783092324805339), vec2(-0.3314794215795328, -0.5583982202447163), vec2(0.8209121245828699, -0.5583982202447163), vec2(0.8209121245828699, -0.2914349276974416), vec2(0.8431590656284761, -0.27363737486095663), vec2(1.074527252502781, -0.5583982202447163), vec2(1.8843159065628476, -0.5583982202447163), vec2(1.8843159065628476, -0.03781979977753059), vec2(2, -0.03781979977753059), vec2(2, 0.09121245828698554), vec2(1.8843159065628476, 0.09121245828698554), vec2(1.8843159065628476, 0.3270300333704116), vec2(1.7374860956618465, 0.3270300333704116), vec2(1.7374860956618465, 0.09566184649610679), vec2(1.675194660734149, 0.09566184649610679), vec2(1.675194660734149, -0.042269187986651836), vec2(1.7374860956618465, -0.042269187986651836), vec2(1.7374860956618465, -0.5539488320355951), vec2(1.5194660734149055, -0.5539488320355951), vec2(1.5150166852057843, 0.4694104560622914), vec2(1.363737486095662, 0.4649610678531702), vec2(1.363737486095662, -0.5539488320355951), vec2(1.2436040044493881, -0.5539488320355951), vec2(0.9454949944382648, -0.17130144605116795), vec2(1.2258064516129032, 0.09566184649610679), vec2(1.025583982202447, 0.09566184649610679), vec2(0.8209121245828699, -0.10456062291434928), vec2(0.8209121245828699, 0.5628476084538376), vec2(0.6740823136818688, 0.5628476084538376)}
end
function cow()
return {vec2(0.9524901661340315, 0.7117695110936235), vec2(-0.14460593205695357, 0.6342909730857855), vec2(-1.1859174828822951, 0.70867036957331), vec2(-1.3842625401823603, 0.5785064257201423), vec2(-1.4834350688323927, -0.39462401165830197), vec2(-1.6290947202871282, -0.952469485314735), vec2(-1.6135990126855606, -1.2561853543054595), vec2(-1.5702110314011712, -1.2251939391023245), vec2(-1.5454178992386631, -1.2871767695085947), vec2(-1.5082282009949008, -1.2375905051835785), vec2(-1.4896333518730198, -1.3119699016711028), vec2(-1.3904608232229874, -0.6177622011208752), vec2(-1.4276505214667494, -0.40082229469892905), vec2(-1.3656676910604792, 0.2437991415262825), vec2(-1.334676275857344, -0.47520169118645345), vec2(-1.3842625401823603, -0.6177622011208752), vec2(-1.4772367857917656, -1.3119699016711028), vec2(-1.4400470875480036, -1.4049441472805084), vec2(-1.2417020302479385, -1.4483321285648976), vec2(-1.347072841938598, -1.2685819203867137), vec2(-1.2788917284917007, -0.8780900888272105), vec2(-1.15492606767916, -0.6735467484865185), vec2(-1.161124350719787, -0.7572235695349835), vec2(-1.0619518220697546, -1.3863492981586274), vec2(-1.0154646992650518, -1.4421338455242707), vec2(-0.9069947460540787, -1.5289098080930492), vec2(-0.8078222174040461, -1.4793235437680328), vec2(-0.9131930290947057, -1.3119699016711028), vec2(-0.9627792934197219, -0.964866051395989), vec2(-0.9069947460540787, -0.7448270034537294), vec2(-0.8574084817290624, -0.7417278619334159), vec2(-0.8078222174040461, -0.8532969566647024), vec2(-0.7892273682821651, -0.747926144974043), vec2(-0.7458393869977759, -0.8223055414615673), vec2(-0.7210462548352677, -0.7417278619334159), vec2(-0.4173303858445431, -0.6053656350396212), vec2(-0.2871664419913753, -0.6673484654458915), vec2(0.010351143958722156, -0.6921415976083997), vec2(0.4256361076807335, -0.4999948233489616), vec2(0.38844640943697134, -0.7355295788927889), vec2(0.43803267376198757, -0.9214780701115998), vec2(0.4008429755182254, -1.2437887882242056), vec2(0.43803267376198757, -1.3119699016711028), vec2(0.6115845988995445, -1.3677544490367461), vec2(0.6115845988995445, -1.2437887882242056), vec2(0.524808636330766, -1.2004008069398162), vec2(0.5310069193713931, -1.0764351461272756), vec2(0.5805931836964093, -0.8161072584209402), vec2(0.6115845988995445, -1.0516420139647675), vec2(0.6177828819401716, -1.342961316874238), vec2(0.6611708632245608, -1.4607286946461517), vec2(0.8223262222808637, -1.4669269776867788), vec2(0.7479468257933393, -1.3615561659961193), vec2(0.692162278427696, -1.1198231274116648), vec2(0.7603433918745933, -0.8223055414615673), vec2(0.8223262222808637, -0.5929690689583671), vec2(0.9896798643777936, -0.5557793707146049), vec2(1.1074472421497072, -0.3760291625364209), vec2(1.231412902962248, 0.0640489333480985), vec2(1.398766545059178, 0.2995836888919258), vec2(1.5723184701967348, 0.2809888397700447), vec2(1.7644652444561728, 0.3677648023388232), vec2(1.9008274713499675, 0.32437682105443394), vec2(2, 0.4111527836232124), vec2(1.981405150878119, 0.5041270292326179), vec2(1.8202497918218161, 0.6342909730857855), vec2(1.8016549426999349, 0.7396617847764452), vec2(1.9132240374312215, 0.8512308795077318), vec2(1.807853225740562, 0.87602401167024), vec2(1.6962841310092753, 0.8512308795077318), vec2(1.6900858479686482, 0.9380068420765103), vec2(1.7768618105374268, 1.0061879555234077), vec2(1.634301300603005, 0.9751965403202725), vec2(1.4545510924248213, 0.9504034081577644), vec2(1.5351287719529727, 1.049575936807797), vec2(1.398766545059178, 1.0061879555234077), vec2(1.411163111140432, 0.9132137099140022)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment