## Question: Simulating water

Eternl Knight
Posts: 58
Joined: Sun Jan 22, 2006 4:31 am

### Re: Question: Simulating water

Yeah, I am looking at it already. Hence the discussion about trying to integrate the idea of buoyancy & volumetric terrains.

Will try to get a prisms version working in the next couple of days (sorry, but my baby boy is sick so time is limited to the 2.5hrs train trip I do to/from work)

--EK
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

### Re: Question: Simulating water

Today I implemented the height field based fluid simulation in my engine. Below I Will give some
details of my implementation.

collision detection:
- special collision shape for a pool of water
- only support convex vs. pool for now
- for each beam of water in the pool:
if convex shape bbox overlap water beam bbox:
using center of water beam as the x and z coordinate and the top/bottom of the convex shape's bbox for the y coordinate:
y_min = shoot a ray from below the object up
y_max = shoot a ray from above the object down

A contact point between a convex shape and the pool consists of:
beam coordinate: int i,j;
contact points: scalar x,y_min,y_max,z

I keep up to 16 contact beam points for each convex / pool encounter

physics:
used equations found in slides
when displacing water from a beam I put a quarter of the displaced water in beams to the "east,west,south and north."
-> this can violate the height requirement of adjacent beams so I loop.

My physics loop looks like:
[step rb velocity]
step fluid velocity
10 iterations:
handle collision between beams of water and convex shapes
[handle rb contacts/joints]
[step rb position]
step fluid position

John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

### Re: Question: Simulating water

My implementation of buoyancy has changed almost completely so I thought I would post update notes.

Rigid Bodies:
I added volume to my mass information class.
it now includes the following:
inverse inertia tensor (local)
mass
volume

This gives me access to the density of the rigid body.

Collision Detection:
an "encounter" between a rigid body and a fluid consists of the following data:
submerged volume
AABB of submerged volume
the 2D bounding box of overlapping fluid columns
age of encounter

The submerged volume is the sum of the volume of displaced fluid across
all columns. I shoot a ray from above the object down to find the top and
a ray from below the object up to find the bottom. I clip the top and bottom
against the fluid column top and bottom.

The 2D bounding box of overlapping fluid columns and age come into play later on.

Physics Solver:
Fluid:
Use the wave equations from the paper to animate

Fluid -> Rigid Body:
fluid_density = 2.0 * body_density; (A nice hack -- keeps the body half submerged)
F = (submerged volume * fluid_density) * -gravity
apply impulse (F * dt) to rigid body
apply drag to object [See Erin Catto's buoyancy code]

Rigid Body -> Fluid: I fake it.
if encounter.age == 1:
create ripple around submerged volume.

I uploaded a video of it here. Sorry about the video quality.
Last edited by John McCutchan on Mon Oct 22, 2007 7:33 pm, edited 1 time in total.
Erwin Coumans
Site Admin
Posts: 4232
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

### Re: Question: Simulating water

jmc wrote: I uploaded a video of it here. Sorry about the video quality.
Thanks a lot. Could you upload the movie to YouTube? It makes it easier to watch on some platforms.
Erwin
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

### Re: Question: Simulating water

Link now points to youtube video
Eternl Knight
Posts: 58
Joined: Sun Jan 22, 2006 4:31 am

### Re: Question: Simulating water

Looks pretty damn good. Is this code Bullet or other engine specific?

--EK
Enrico
Posts: 42
Joined: Thu May 17, 2007 9:34 am
Location: Stuttgart, Germany
Contact:

### Re: Question: Simulating water

jmc wrote:I uploaded a video of it here. Sorry about the video quality.
Wow, this looks fantastic!

Any chance to port this to Bullet?
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

### Re: Question: Simulating water

The video is of my own engine (Cactus.) I plan on implementing a demo for bullet in the near future.
Kafu
Posts: 8
Joined: Sat Jun 21, 2008 1:51 pm

### Re: Question: Simulating water

Are there any progresses about the buoyancy demo?
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

### Re: Question: Simulating water

Hey Daniele,

Thanks for asking about the buoyancy work. I have been busy working on other projects but it is still on my TODO list. If you are interested in working on an implementation I can possibly assist by answering questions.

Thanks,

John
Kafu
Posts: 8
Joined: Sat Jun 21, 2008 1:51 pm

### Re: Question: Simulating water

Thanks John,

actually I'm working on a simple implementation on an ocean surface.

To keep things easy I'm using a simplified particles approach, not sophisticated but - for now - fast and quite acceptable. Actually I'm trying to implement stress tensors (what in "Game Physics Engine Development" by Millington, and his Cyclone physics engine, are called "aerodynamic tensors") to simulate drag coefficients.

I would be interested in know how do you use the inertia tensor in your buoyancy solver: as it should give informations about distribution of the mass, I think it can be used to "weight" the particles.

P.S. Erin solution seems the same "local/elemental pressure gradient" used in RoR: http://rigsofrods.blogspot.com/2007/01/boats.html. It's right?
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

### Re: Question: Simulating water

Hey Daniele,

My buoyancy solver didn't make sue of the inertia tensor although that sounds like a good idea. My solver worked similarly to Erin's: Integrate the volume of the shape under the surface of the water and apply a buoyant impulse to the shape. The main difference between mine and Erin's solver is that I used a GJK based ray cast and height field bar elements to integrate the volume under the surface where Erin built a mesh by intersecting the meshes of the surface of the water and the shape. He then performed center of mass and volume calculations on this new mesh.

John