Buoyancy script

Just Archimedes’ law:

https://vimeo.com/18468122

Requires a collider and rigidbody components.

P.S.
I’ve updated the script:

  1. The old one didn’t take into account object rotation and consequently produced wrong results sometimes.
  2. The new one draws nice gizmos

P.P.S.
Version 2.1

  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.
  2. Now it works with concave shapes too. Thanks to Antitheory for the idea.

P.P.P.S.
License: WTFPL version 2.

466976–16416–$Buoyancy.cs (8.43 KB)

9 Likes

Looks great,i can see me floating away on a raft :smile:.

Dave

1 Like

nice presentation… will give it a try. Thank You for sharing.

looks very cool, will have to give it a try later.
thanks you =)

man that water shader rocks!!! maybe a package for that to??? :smile: thanks!

Nicely done. I noted the “links” to the ocean shader that were commented out. Does it work with the community ocean shader?

1 Like

Nice work. Thanks for sharing

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.

2 Likes

Thanks Antitheory. I’d love to see your enhanced script as well.

Looks really awesome, thanks for sharing!

3dDude,
It’s a community ocean shader:

http://www.unifycommunity.com/wiki/index.php?title=Ocean_Shader_Project
http://forum.unity3d.com/threads/16540-Wanted-Ocean-shader

Yes, it does.

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.

Here it is:

467101–16380–$Ocean.cs (35.1 KB)

1 Like

Nice idea!

EDIT

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 have updated my buoyancy script, because the old one produces wrong results if an object is rotated. Check the first post.

Does anyone remember how floating boxes should look?

Is this 45 degree angle ok?

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.

In real life i sapose you would get both :face_with_spiral_eyes:.

dave

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).

Very interesting… I just read up on ship-design, center of buoyancy, “metacenters” and what not. Pretty interesting stuff…

Been thinking about this to,i think random is the word :face_with_spiral_eyes: (:face_with_spiral_eyes: but then again i usualy am :smile:)

Dave