verlets integration code example

Example: verlet integration code

local apart = game.Workspace.AnchorPart;local ppart = game.Workspace.PlanePart;local previousTime = tick(); function cloth(corner, width, height, length)		local points = {};	local constraints = {};	local m = math.sqrt(length^2 + length^2);	for y = 0, height do		for x = 0, width do			points[y] = points[y] or {};			points[y][x] = point.new((corner * CFrame.new(x, y, 0)).p);			if x ~= 0 then				local c = constraint.new(points[y][x], points[y][x-1], length);				c.line.Parent = game.Workspace.CurrentCamera;				table.insert(constraints, c);			end;			if y ~= 0 then				local c = constraint.new(points[y][x], points[y-1][x], length);				c.line.Parent = game.Workspace.CurrentCamera;				table.insert(constraints, c);				--[[				-- structure makes a difference!				if x < width then					local c = constraint.new(points[y][x], points[y-1][x+1], m);					c.line.Parent = game.Workspace.CurrentCamera;					table.insert(constraints, c);				end;				--]]			end;			if y == 0 then				points[y][x].anchored = true;			end;		end;	end;	return points, constraints;	end; local width, height, length = 10, 10, 2;local cpoints, cconstraints = cloth(apart.CFrame, width, height, length); function update()	local delta = tick() - previousTime;	previousTime = tick();	local col = collision.new(ppart);	for i = 0, height do		if i == 0 then			for i2 = 0, width do				local x = cpoints[i][i2];				x.position = (apart.CFrame * CFrame.new(i2 * length, 0, 0)).p;			end;		end;		for i2 = 0, width do			local x = cpoints[i][i2];			x:update(delta, 0.9985);			--[[			-- collision			local lide = col:pointToPlanes(x.position);			if lide then				x.position = lide;			end;			--]]		end;	end;	for i = 1, 15 do		for _, c in ipairs(cconstraints) do			c:solve();		end;	end;	for _, c in ipairs(cconstraints) do		c:draw();	end;end; game:getService("RunService").Stepped:connect(update);

Tags:

Misc Example