Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

How can I break a brick into wedges? [closed]

Asked by
Zerio920 285 Moderation Voter
9 years ago

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?

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?

1 answer

Log in to vote
5
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.


Simpler Solution

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.

Voronoi Observation

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.

Making Triangles

http://hastebin.com/mejoyaniwo.js

0
That makes loads of sense! But like you mentioned that regular look is a little off... I'll give you best answer if you end up finding anything on that Voroni diagram. If not, I'll still credit you, just get back to me on that please. On the edges, if the problem lies in how irregular the edges are, couldn't you just split those complex pieces into rectangles and wedges? Zerio920 285 — 9y
0
No, the tricky part is that there's no reason it should include the edges at all. I'm pretty sure there's some regular way to fix it, but it hasn't come to mind yet. BlueTaslem 18071 — 9y
0
What are the triangle.render s supposed to be in the code? Zerio920 285 — 9y
0
They make a triangle with the given parameters as the corners, e.g., what this plugin does http://www.roblox.com/Triangle-Workshop-GUI-Tool-item?id=47368728 I will include my implementation as a link at the bottom BlueTaslem 18071 — 9y
View all comments (2 more)
0
Thanks! The script creates a bit too many triangles though... Any way I could adjust that? Zerio920 285 — 9y
0
This makes *general* triangles, so they don't have to be right (90 degrees). That means for every single `render`, you need 2 right triangles. You would have to write your own that simplifies it if you wanted to avoid that (which for the rectangly case, you can) BlueTaslem 18071 — 9y
Ad