I have a large, flat brick that represents a pane of glass, and I'd like to find some way to have it break into large wedges.
local base = script.Parent:FindFirstChild("Glass") local points = {} for loop = 1,math.random(5,20) do local point = base.Position + Vector3.new((math.random(-base.Size.X/2,base.Size.X/2)),0,(math.random(-base.Size.Z/2,base.Size.Z/2))) for i = 1,#points do if points[i] == point then table.remove(points[i]) end end points[loop] = point end
This script generates some random points on the brick. Now all I need is to find some way to use these points to generate wedgeparts, which will position in place of the brick, so I can unanchor them to have the glass appear to break. I'm stuck on the figuring out how to generate the wedgeparts from these points, though. Any help please?
This is a very interesting, and very hard problem.
Unfortunately, there isn't any neat solution to turn a set of points into triangles. What exactly would the points represent? One option is the that they would be the corners of the triangles. But how do you assign which three form a triangle? One way would be a Delaunay triangulation though this is not straightforward to compute.
I'm going to consider other ways to get this effect, first.
In my discussion of the solution, I'm going to assume you have a function triangle(a, b, c)
which makes a triangle with the corners a
, b
, and c
. You can look up functions that do this on freemodels.
I'm also going to assume that we're working around the origin or in some other "nice" place, so that things are really simple. Moving it to somewhere else wouldn't ever be terribly challenging.
We can recursively break the rectangle up into smaller rectangles -- then the rectangles eventually get broken into two triangles: http://i.imgur.com/8woT1Tg.png
The result looks like this: http://i.imgur.com/LSZWv2X.png
Here is the code I used to make it:
function rectangle(left, bottom, right, top) wait(); local stop = math.random() < 0.2 or right - left < 2 or top - bottom < 2 -- 20% of the time -- OR when its too thing -- Stop -- turn into two triangles if stop then if math.random(2) == 1 then -- From bottom left to top right triangle.render( Vector3.new(left, bottom), Vector3.new(right, top), Vector3.new(right, bottom) ); triangle.render( Vector3.new(left, bottom), Vector3.new(right, top), Vector3.new(left, top) ); else -- From top left to bottom right triangle.render( Vector3.new(left, top), Vector3.new(right, bottom), Vector3.new(left, bottom) ); triangle.render( Vector3.new(left, top), Vector3.new(right, bottom), Vector3.new(right, top) ); end else -- Split it into 9 panels -- Horizontally, 3: local h1 = (right - left) * math.random() + left local h2 = (right - left) * math.random() + left -- h1 must be left of h2 h1, h2 = math.min(h1, h2), math.max(h1, h2) -- Vertically, 3: local v1 = (top - bottom) * math.random() + bottom local v2 = (top - bottom) * math.random() + bottom -- v1 must be below v2 v1, v2 = math.min(v1, v2), math.max(v1, v2) -- Dispatch the 9 new rectangles: rectangle(left, bottom, h1, v1); -- Bottom left rectangle(h1, bottom, h2, v1); -- Bottom middle rectangle(h2, bottom, right, v1); -- Bottom right rectangle(left, v1, h1, v2); -- Bottom left rectangle(h1, v1, h2, v2); -- Bottom middle rectangle(h2, v1, right, v2); -- Bottom right rectangle(left, v2, h1, top); -- Top left rectangle(h1, v2, h2, top); -- Top middle rectangle(h2, v2, right, top); -- Top right end end rectangle(-6, -6, 6, 6);
This turns out better than I expected it to! In the picture I included, you'll notice that most of the triangles aren't quite horizontal / vertical. That's because the triangle drawing routine I have splits all triangles along the altitude from their hypotenuse -- that wouldn't be necessary, and you could just cut all the rectangles into precisely 2 WedgeParts (instead of 4)
Unfortunately, that will make it look fairly regular, which probably isn't what you want. Depending on circumstance, it's possible no one would notice.
There's something called a Voronoi diagram which groups a plane by which control point is closest.
The idea is based on using the control points, and the vertices of their regions.
... The problem is the edges, which I don't have an answer for. I can edit with a little more later.
Locked by NinjoOnline, Spongocardo, NotsoPenguin, and Redbullusa
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?