Hello,
Im trying to shatter up windows in a building ive created. The first part of the code works out the center of each window on the building and creates a simple polyCube to represent the glass/window.
I then shatter up the window, create particles at the window shard positions and then instance the shards to the particles.
This is nice n neat as it by passes the nasty rigid body systems which are full in interpenetration errors and the like.
The issue I have is that when the shards are created, they inherit the original cubes pivot point. Using the center pivot command, the shards appear to center their pivots closer to their own body of mass.
When querying this position information per shard then emiting a particle at these positions per shard, the particles still adopt the original cubes pivot point, not the shards position.
The secondary issue this creates is that when i come to instance the shards to the particles, the shards will instance with the original cubes pivot point also. This means that each shard object is controlled from the same point making the simulation incorrect.
I need to be able to first, move the pivot to the center of each shard, then, create a particle at that point, and then finally, instance the object without the shards moving out of shape (tesselation).
Hope that makes sense
Any help would be most appreciated.
Code:
Shatter code based on the 'simple shatter code' in the mel scripts section at highend3d.
CODE
//loop through each window and shatter accordingly.
//$former_windows is a list of the window objects.
for ($o=0; $o<size($former_windows); $o++)
{
select $former_windows[$o];
DeleteHistory;
float $bb[] = `polyEvaluate -boundingBox ($former\_windows[$o])`;
for ($i = 0; $i < 7; $i++)
{
float $px = rand(($bb[0]0.95),($bb[1]0.95));
float $py = rand(($bb[2]0.95),($bb[3]0.95));
float $pz = rand(($bb[4]0.95),($bb[5]0.95));
float $rx = rand(0,180);
float $ry = rand(0,180);
float $rz = rand(0,180);
polyCut -pc ($px)($py)($pz) -ro ($rx)($ry)($rz) -ef 1 -eo 0 0 0 ($former_windows[$o]);
polyCloseBorder ($former_windows[$o]);
}
select -r ($former_windows[$o]);
polySeparate ($former_windows[$o]);
//visual progress output
float $percent = ($o/(float)(size($former_windows))*100);
string $perc = (int)$percent + "%" + "\n";
print $perc;
}
simple center pivot loop:
CODE
//center all pivots
//$shards[] = list of all shards that make up the window object
for ($each in $shards)
{xform -centerPivots $each;}
simple rename loop:
CODE
//rename all shards for association purposes.
string $shards[] = ls -transforms "polySurface*"
;
string $token[];
for ($each in $shards)
{
tokenize $each "polySurface" $token;
string $name = "windowShard_" + $token[0];
rename $each $name;
}
particle instance code:
CODE
//create particle node
particle -name windowDyn;
//list all window shards
string $shards[] = ls -transforms "windowShard\_*"
;
//emit a particle at the center of each shard
for ($each in $shards)
{
//get shard position
float $shardPos[] = xform -q -ws -translation $each
;
//get shard volume
float $bb[] = xform -q -ws -bb $each
;
float $x = abs($bb[3] - $bb[0]);
float $y = abs($bb[4] - $bb[1]);
float $z = abs($bb[5] - $bb[2]);
vector $v = <>;
//get the magnitude of the vector - use for particle mass
float $mag = mag($v);
//emit particle at center of the shard
emit -o windowDyn
-pos $shardPos[0] $shardPos[1] $shardPos[2]
-at mass
-fv $mag;
}
//save the initial state
saveInitialState windowDyn;