Game Development Reference
In-Depth Information
{
Vector3 DeformableMD5::volumetricForce( int mesh, int vertex)
int i;
ModelMesh
m=&meshes[mesh];
ModelVertex mv = &m −> vertices[vertex];
Ve r t ex
finalVertices[vertex];
real totalWeight = 0.0f;
Vector3 totalForce = vecCreate(0.0f,0.0f,0.0f);
v=&m
−>
/ calculate the contribution of one joint /
for (i=mv −> start; i < mv −> start + mv −> count; i++) {
ModelWeight w=&m −> weights[i];
/ from weight, retrieve joint and its parent /
ModelJoint
−>
joint];
ModelJoint jp = &skeleton[j −> parent];
j = &skeleton[w
/ calculate the unit vector in the direction of the bone /
Vector3 axis = j
−>
pos
&jp
−>
pos;
vecNormalize(axis);
/ calculate the force contribution as before /
Vector3 diffPos = v −> pos j −> pos;
Vector3 diffPos0 = v −> restPos j −> pos;
real projection = vecDot(axis, &diffPos);
real projection0 = vecDot(axis, &diffPos0);
Vector3 projVector = vecScaledVector(axis, projection);
Vector3 projVector0 = vecScaledVector(axis, projection0);
Vector3 distanceVector = diffPos projVector;
Vector3 distanceVector0 = diffPos0
projVector0;
real distanceVectorAbs = vecLength(distanceVector);
real distanceVectorAbs0 = vecLength(distanceVector0);
if (distanceVectorAbs == 0) return vecCreate(0,0,0);
real preFactor = (distanceVectorAbs0 /
distanceVectorAbs 1.0f) /(timeStep timeStep);
Vector3 result = distanceVector;
vecScale(&result, preFactor w −> bias);
totalWeight += w −> bias;
}
/ sum over all contributions /
vecScale(&totalForce, 1.0f/totalWeight);
}
Listing 14.3. The bone-distance preservation algorithm for a model definition using SSD.

Custom Search