Here’s an example of this
--# Main
-- destructable
-- Use this function to perform your initial setup
function setup()
txture = readImage("Documents:ExampleCircle",5,5)
ter = Terrain(vec2(WIDTH/2,HEIGHT/4),vec2(WIDTH,HEIGHT/2))
circ = {}
circn = 0
holding = nil
end
function collide(c)
local cp = circ.position
if c.state == BEGAN and c.normalImpulse > 2 then
for k,v in pairs(ter:getTable()) do
for a,b in pairs(circ) do
if v:dist(b.position) < 50 then
ter:setVert(k, v + (v-b.position):normalize()*(50-v:dist(b.position)))
ter:setVert(k, v + (c.normal*-1)*c.normalImpulse*(3-v:dist(b.position)/19))
end
end
end
end
end
function touched(t)
for a,b in pairs(circ) do
local cp = b.position
if vec2(t.x,t.y):dist(cp) < 40 and t.state == BEGAN then
holding = {b,vec2(t.x,t.y)}
end
if holding and t.state == MOVING then
holding = {b,vec2(t.x,t.y)}
end
if t.state == ENDED then
holding = nil
end
end
if not holding then
ter:touched(t)
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
local n = math.random(20)
if n == 15 and circn < 2 then
circn = circn + 1
circ[circn] = physics.body(CIRCLE,15)
circ[circn].position = vec2(math.random(50,WIDTH-50),math.random(HEIGHT/2+50,HEIGHT-50))
end
for k,v in pairs(circ) do
if holding ~= nil and holding[1] == v then
v:applyForce(holding[2]-v.position-v.linearVelocity/4)
end
end
-- Do your drawing here
ter:draw()
for k,v in pairs(circ) do
sprite("Documents:circle",v.x,v.y,30,30)
end
pushStyle()
fill(255, 255, 255, 255)
text(math.floor(1/DeltaTime),100,100)
popStyle()
end
--# Terrain
Terrain = class()
function Terrain:init(pos,size)
self.pos = pos
self.size = size
local verts = {}
local width,height = (size.x/WIDTH)*20,(size.y/HEIGHT)*20
for x=1,width do
table.insert(verts,vec2(pos.x-size.x/2+(size.x/width)*x,pos.y+size.y/2))
end
for y=1,height do
table.insert(verts,vec2(pos.x+size.x/2,pos.y+size.y/2-(size.y/height)*y))
end
for x=1,width do
table.insert(verts,vec2(pos.x+size.x/2-(size.x/width)*x,pos.y-size.y/2))
end
for y=1,height do
table.insert(verts,vec2(pos.x-size.x/2,pos.y-size.y/2+(size.y/height)*y))
end
self.verts = verts
self.m = mesh()
self.m:setColors(255,255,255,255)
self.m.vertices = triangulate(verts)
self.floor = physics.body(POLYGON,unpack(verts))
self.floor.type = STATIC
self.bound = vec2(width,height):len()
self.tbound = 30
print(self.bound)
self.oe = ElapsedTime
self.oldverts = {}
end
function Terrain:BodyChanged(verts)
local bool = false
if #verts ~= #self.verts then
bool = true
return bool
end
for i=1,#verts do
local vc = verts[i]
local nc = self.verts[i]
if vc ~= nc then
bool = true
end
end
return bool
end
function Terrain:draw()
if ElapsedTime > self.oe+0.2 and 1/DeltaTime > 10 then
self.oe = ElapsedTime
if self:BodyChanged(self.floor.points) then
self.floor:destroy()
self.floor = physics.body(POLYGON,unpack(self.verts))
self.floor.type = STATIC
self.oldverts = self.verts
tween.delay(0.1,function()
self.m.vertices = triangulate(self.verts)
end)
end
end
self.m:draw()
local sv = self.verts
for i=1,#sv do
sprite(txture,sv[i].x,sv[i].y,5,5)
strokeWidth(3)
if i > 1 then
line(sv[i].x,sv[i].y,sv[i-1].x,sv[i-1].y)
end
end
end
function Terrain:getTable()
return self.verts
end
function Terrain:setVert(i,v)
self.verts[i] = v
end
function Terrain:touched(t)
self.t = true
if t.state == ENDED then
self.t = false
end
local p,s = self.pos,self.size
local tp = vec2(t.x,t.y)
local sp = #self.verts
for i=1,sp do
local sv = self.verts[i]
local svi
if i > 1 then
svi = self.verts[i-1]
else
svi = self.verts[#self.verts]
end
if sv and sv:dist(tp) < self.bound then
self.verts[i] = sv + (tp-sv):normalize()*0.2
end
if sv ~= nil then
if sv:dist(svi)>self.tbound*1.5 then
local d = (sv+svi)/2
if d.x > p.x-s.x/2-5 and d.x < p.x+s.x/2+5 and d.y > p.y-s.y/2-5 then
table.insert(self.verts,i,d)
return
else
table.remove(self.verts,i)
end
end
end
for j=1,sp do
if sv and self.verts[j] and sv:dist(self.verts[j])<self.tbound/2 and j~=i then
table.remove(self.verts,i)
end
end
if sv and sv:dist(tp) < self.tbound then
local dir = (sv-tp):normalize()*5+vec2(t.deltaX,t.deltaY)
self.verts[i] = self.verts[i] + dir
end
end
end