Game Development Reference
In-Depth Information
real liquidDensity;
public:
/** Creates a new buoyancy force with the given parameters. */
ParticleBuoyancy(real maxDepth, real volume, real waterHeight,
real liquidDensity = 1000.0f);
/** Applies the buoyancy force to the given particle. */
virtual void updateForce(Particle *particle, real duration);
};
Excerpt from src/pfgen.cpp
void ParticleBuoyancy::updateForce(Particle* particle, real duration)
{
// Calculate the submersion depth.
real depth = particle->getPosition().y;
// Check if we're out of the water.
if (depth >= waterHeight + maxDepth) return;
Vector3 force(0,0,0);
// Check if we're at maximum depth.
if (depth <= waterHeight - maxDepth)
{
force.y = liquidDensity * volume;
particle->addForce(force);
return;
}
// Otherwise we are partly submerged.
force.y = liquidDensity * volume *
(depth - maxDepth - waterHeight) / 2 * maxDepth;
particle->addForce(force);
}
In this code I have assumed that the buoyancy is acting in the up direction. I have
used only the y component of the object's position to calculate the length of the spring
for Hook's law. The generator takes four parameters: the maximum depth parameter,
as discussed earlier; the volume of the object; the height of the surface of the water;
and the density of the liquid in which it is floating. If no density parameter is given,
then water, with a density of 1000 kg/m 3 , is assumed (ocean water has a density of
around 1020 to 1030 kg/m 3
up to 1250 kg/m 3
for the Dead Sea).
Search Nedrilad ::




Custom Search