Hi
I am trying to create simplistic model for boats floating in water in Unity using PhysX but i am not understanding how to get it work properly.
I use a single rigid body with a centre of mass as the red dot. Its below the ship's mesh as i read having a lower centre of mass made it more stable. Though i cannot get it to be stable anyway.
Each green sphere is a buoyancy force calculation which has this code, and it applies the force upwards at the spheres position:
_volumeUnderPlane = VolumeBelowPlane();
if (_volumeUnderPlane > 0)
{
// P = 1027 (approx density of water at sea level)
var force = -_volumeUnderPlane * Physics.gravity * P;
_parentRB.AddForceAtPosition(force, transform.position,ForceMode.Force);
}
Where Volume below the plane is calculated as:
private float VolumeBelowPlane()
{
//water level is always 0 for now
//math based on https://en.m.wikipedia.org/wiki/Spherical_cap
var dist = -transform.position.y + _radius;
dist = Mathf.Clamp(dist, 0, 2 * _radius);
var dist2 = dist * dist;
var frac = Mathf.PI * dist2 / 3f;
var volume = 3 * _radius * frac - frac * dist;
return volume;
}
But the result is totally unstable, this is how it looks with a lowish mass:
https://i.imgur.com/LLoVzP5.gif
With a high mass it looks like this:
https://i.imgur.com/WNrO33z.gif
I am trying to replicate how assassins creed does it, they used spheres to approximate the buoyancy without it being too computationally heavy, yet they didn't go into any details on how they did the math for it but they did post this image of it:
https://www.fxguide.com/wp-content/uploads/2012/12/immerse_spheres.jpg
Am i doing any thing wrong with my math here? I don't know why its so unstable for me.