We’ve been using buoyancy effectors quite a bit for our water, and it was working pretty well. recently we ran into a situation where we need to replace them, due to needing to support different kinds of collider shapes that are not supported by the built-in buoyancy effector.
We’ve managed to perfectly replicate the buoyancy part of the component, but we’re struggling with the linear drag.
I thought that the linear drag on the BuoyancyEffector was just extra drag applied to the object. If that was the case, an object with drag A entering a BuoyancyEffector with drag B would experience the same slowdown as an object with drag A+B. That’s not the case. We have also tried to write custom logic that adds forces based on the drag to the object, but we haven’t been able to match the results.
It does seem like the linear drag on the effector might be wrong? Or at least not a proper drag simulation. If I set the density to 0, and only have linear drag, I can see that different objects are affected differently. That’s not correct - I’m pretty sure the drag is supposed to work exactly like air friction, and not be affected by mass at all?
Any insight into how the drag value on BuoyancyEffector works? I tried to look at the Box2D source, but it seems like the effector stuff is all Unity.
I threw a little capsule bobber in the drink this morning and it almost feels like maybe Unity is adjusting the drag depending on how submerged the object is, which is an interesting proxy for hydrodynamic skin drag vs defacto zero drag in air… is that perhaps what you are seeing?
It’s quite compelling motion, especially noticeable if you throw a long-ish object in and let it fall over, so they are clearly considering the shape of the area of the collider submerged and its centroid and consequent drag.
You’re for sure right that it’s the amount that’s submerged that’s taken into account! The mass is also for sure used - objects with the same shape at the same level of submersion with different mass behave differently, even with 0 buoyancy.
I’d love to know the exact maths used for this, but I can live with approximating just fine.
The BE2D has a density property… and I’m pretty sure they compute a density for your floating objects based on their areal displacement and mass.
Try it yourself: a 1x1 cube with mass of 1 dropped into density 1 fluid from a height will enter the water and come to a stop from the drag, then it will not sink or rise.
If you double the density of the fluid and the mass of the cube, it will penetrate the water farther, but then come to a rest.
As far as the math for the damping, pretty sure it is just something like:
It does indeed calculate the submerged portions of the colliders with respect to the plane the buoyancy-effector defines to calculate the area and with the density can then calculate the submerged mass and its center etc.
The drag formula isn’t so easy to post but it takes the submerged area as a scale of how much drag to use. Obviously only a crude approximation but it seems to serve its purpose well here. It’s calculated as just another force.
We calculate lots of forces and sum them all up. The final force is applied to the center of the submerged region although we apply an explicit term for Torque for the drag having removed the off center-of-mass portion. We do this because we scale its effects by the current angular velocity and rotational inertia and also have to do some sensitive work with sleeping. If you note, objects can sleep when buoyant. Applying forces all the time used to mean they were always awake; now they can sleep as expected when doing nothing.