Game Development Reference
In-Depth Information
// Get the origin and half size of the block, in old-body
// local coordinates.
cyclone::Vector3 halfSize = (max - min) * 0.5f;
cyclone::Vector3 newPos = halfSize + min;
// Convert the origin to world coordinates.
newPos = tempBody.getPointInWorldSpace(newPos);
// Set the body's properties (we assume the block has a body
// already that we're going to overwrite).
blocks[i].body->setPosition(newPos);
blocks[i].body->setVelocity(tempBody.getVelocity());
blocks[i].body->setOrientation(tempBody.getOrientation());
blocks[i].body->setRotation(tempBody.getRotation());
blocks[i].body->setLinearDamping(tempBody.getLinearDamping());
blocks[i].body->setAngularDamping(tempBody.getAngularDamping());
blocks[i].offset = cyclone::Matrix4();
blocks[i].exists = true;
// Finally calculate the mass and inertia tensor of the new block.
blocks[i].calculateMassProperties(invDensity);
}
}
This assumes that the collision will occur on the YZ plane of the block (which it
must in our demo). More complete code would have similar algorithms for the other
possible collision planes.
Because the resulting pieces are roughly rectangular, they are treated like rectan-
gular blocks for calculating their inertia tensors. This is done simply as
Excerpt from src/demos/fracture/fracture.cpp
/**
* Calculates and sets the mass and inertia tensor of this block,
* assuming it has the given constant density.
*/
void calculateMassProperties(cyclone::real invDensity)
{
// Check for infinite mass.
if (invDensity <= 0)
{
// Just set zeros for both mass and inertia tensor.
body->setInverseMass(0);
body->setInverseInertiaTensor(cyclone::Matrix3());