Page 1 of 1

Detecting split in array

Posted: Tue Jun 10, 2014 12:26 pm
by rdlaitila
Hello Everyone,

I'm currently working on some procedural destruction code for objects in a space scene. The basic premise is to map the destructible object to a grid of destructible blocks that destroy when other objects make contact.

In the first iteration, I took an array of blocks and generated a body/shape/fixture for each. Using WeldJoints, each block was welded to other blocks it touched on its side's. This worked well except for the jitter seen in the weld joints when a high speed object made contact with the body group as a whole.

I've decided instead to map all of my object blocks to a single common body to stop the jitter. The only issue I have during destruction if the grid of blocks ever gets cut in half (high speed object cuts straight through) then both halfs float together with the same body.

I need help with a solver to detect when an array of blocks splits into two distinct groups. Example:

Starting Block Grid:

Code: Select all

objectBlocks = {
    {{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK},{BLOCK}},
}
Now after a number of collisions, the object is cut in half with all middle blocks destroyed:

Code: Select all

objectBlocks = {
    {{BLOCK},{BLOCK},{EMPTY},{EMPTY},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{EMPTY},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{EMPTY},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{EMPTY},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{EMPTY},{BLOCK},{BLOCK}},
    {{BLOCK},{BLOCK},{BLOCK},{EMPTY},{EMPTY},{BLOCK}},
}
How would I go about an efficient solver to build out groups of blocks from the above array so I can generate additional floating bodies depending on how many groups there are within the array. The solver applied to the above array should generate two groups but hopefully would be powerful enough to find N groups of connected blocks within the arr

Thanks so much!

Re: Detecting split in array

Posted: Wed Jun 11, 2014 8:07 am
by bartbes
Repeatedly pick the first non-empty piece, flood-fill, and "empty" the found group, maybe?