# Cloth Simulation, and as a 'Physics Lab' test

As I have mentioned in other posts, I have been working through (copies of) the Cloth Simulation and Physics Lab example projects. My notes on the former are added to the wiki here.

The code below does something similar to Cloth Simulation, as a new ‘Test 11’ for the Physics Lab, replacing the constraints by ‘soft’ DISTANCE joints:

``````
Test11 = class()

function Test11:init()
self.title = "Cloth simulation (use the accelerometer)"
self.freq = 10  -- Softness of all distance joints (constraints)
self.damp = 0.5 -- Damping for all distance joints
end

function Test11:setup()
local b = {}
for y = 0, 10 do
for x = 0, 10 do
local i = (y * 11) + x
b[i] = createCircle(120 + x * 40, 600 - y * 40, 5)
if y == 0 then b[i].type = STATIC end
end
end
for y = 0, 9 do
for x = 0, 9 do
local i = (y * 11) + x
local ir = i + 1
local ib = i + 11
local ibr = ib + 1
local j = physics.joint(DISTANCE, b[i], b[ir],
b[i].position, b[ir].position)
j.frequency = self.freq
j.dampingRatio = self.damp
j = physics.joint(DISTANCE, b[i], b[ib],
b[i].position, b[ib].position)
j.frequency = self.freq
j.dampingRatio = self.damp
j = physics.joint(DISTANCE, b[i], b[ibr],
b[i].position, b[ibr].position)
j.frequency = self.freq
j.dampingRatio = self.damp
end
local ix = 10 * 11 + y -- Use 'y' to index x positions, here,
-- to save another for...end loop.
local ixr = ix + 1
local iy = y * 11 + 10
local iyb = iy + 11
local j = physics.joint(DISTANCE, b[ix], b[ixr],
b[ix].position, b[ixr].position)
j.frequency = self.freq
j.dampingRatio = self.damp
j = physics.joint(DISTANCE, b[iy], b[iyb],
b[iy].position, b[iyb].position)
j.frequency = self.freq
j.dampingRatio = self.damp