Detecting split in array

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
rdlaitila
Prole
Posts: 8
Joined: Tue Nov 01, 2011 5:26 pm

Detecting split in array

Post 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!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Detecting split in array

Post by bartbes »

Repeatedly pick the first non-empty piece, flood-fill, and "empty" the found group, maybe?
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests