The old one didn’t take into account object rotation and consequently produced wrong results sometimes.
The new one draws nice gizmos
P.P.S.
Version 2.1
I decided to not calculate density from volume, because volume “calculation” itself is quite inaccurate. It’s easer to specify the correct density manually.
Now it works with concave shapes too. Thanks to Antitheory for the idea.
The Ocean script/shader by Nikko has a buoyancy script to that works similar to this, although that one is only two-dimensional (ie: it doesn’t chop the object along the y-axis).
I just made my own little version of the same script in order to make it work semi-realistically with irregular objects. The problem with the approach taken here is that it simply gets the bounds (a rectangular prism) of the collider and chops that up into cubes… this means that a cone, for example, would have just as much buoyancy at it’s tip as at it’s base (obviously not true).
To solve this problem I use basically the same approach as this script, but each point is checked to see if it’s truly “inside” the collider. I accomplished this by firing 6 rays originating outside the mesh toward the point. each ray corresponds to a direction along the x,y,z axes. If any of the rays don’t hit the collider it mean’s the point does not reside inside the mesh. There can be some false-positives, but only for very concave shapes.
The script is designed to work with the Ocean script but it could be easily modified to work with any water plane. If there is interest I’ll post the script here.
Actually, the ocean package comes with its own buoyancy script. I looked at it, got the idea, and wrote this one from scratch. Then, in order to avoid the defective javascript<>c# interoperability, I translated the whole ocean script to c# for my personal use.
I removed my version of the script because it had some errors in it. I have fixed them now and will clean-up the code a bit so you guys can check it out. I tested it out with composite objects too. I made a pretty sweet catamaran… I will post it all soon in a new thread.
I get the same effect when using cubes… I don’t know whether that is what it would actually look like or not. Considering that all the cubes are doing that then there is either a real-life physics reason or a programming-error physics reason. It’s just a guess, but it may have something to do with fluid dynamics…
In most cases though objects like a crate don’t have their center of gravity right in the middle anyways… usually it’s closer to the bottom because the stuff inside is heavier nearer the bottom (or you are packing your crates wrong!). If you imbalance your cubes they might end up floating more level…
EDIT:
Now that I think about it some more I may have the reason why. It’s because the only way to calculate the buoyancy affect on an individual chunk of the object is to the proportion of it which is underwater… however when the chunk is rotated the proportion is not linearly related to the chunk’s center point’s position relative to the water-level.
A floating object is considered “at rest” when the center of buoyancy is directly above the center of gravity. I don’t recall what an ideal cube’s resting point is, but I’d guess it’s pretty much what you’re seeing which is similar to an ideal sphere which has no resting point (the center of gravity and center of buoyancy are in the same location).