Buoyancy Problem

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Buoyancy Problem

Postby Andy Price » Thu Aug 14, 2008 9:45 am

Hi,

I've done my best at understanding the buoyancy system but there's something I'm missing. When a body enters the buoyancy region is never seems to come to rest and leaps out of the water and sinks down in a never ending cycle - it does eventually come to rest but, this takes maybe 5 minutes and even then doesn't appear totally stable.

The density I pass to NewtonBodyAddBuoyancyForce is calculated via Density=((Viscosity * Mass) / Volume), where Viscosity is 1.5 (taken from the Newton SDK buoyancy example), Viscosity is perhaps a misnomer here so you could call it 'DensityScale' instead I suppose and the mass and volume are returned by the respective Newton functions within the Buoyancy callback. The plane equation is passed to the plane callback and is correct when stepped through in the debugger and aligns correctly to the graphics body and physics object, (currently (0,1,0,40) - where 40 equals surface height in units from world origin).

The mass of the box is 350 and it is 10x10x10, gravity is 9.8 so the box has a downward, (0,-1,0) force of 3430 (give or take floating point error) applied in the force and torque callback.

The code for the Buoyancy callback is currently:
Code: Select all
   const NewtonBody* pBody=Body0;
   const NewtonBody* pTrigger=Body1;
   if(NewtonCollisionIsTriggerVolume(NewtonBodyGetCollision(Body0)))
   {
      pBody=Body1;
      pTrigger=Body0;
   }
   WaterVolumeInfo WaterInfo=*(WaterVolumeInfo*)NewtonBodyGetUserData(pTrigger);
   dFloat fMass;
   dFloat fDummy;
   NewtonBodyGetMassMatrix(pBody,&fMass,&fDummy,&fDummy,&fDummy);
   dFloat fVolume=NewtonConvexCollisionCalculateVolume(NewtonBodyGetCollision(pBody));
   dFloat fDensity=((WaterInfo.fViscosity * fMass) / fVolume);
   D3DXVECTOR3 Gravity=Physics_GetGravity();
   NewtonBodyAddBuoyancyForce(pBody,fDensity,0.8f,0.8f,&Gravity.x,GetPhysicsBuoyancyPlane,(void*)WaterInfo.Plane);
   return(1);


This is a video of what currently happens Buoyancy Problem, 3044kb Indeo Video 5.1 avi.

Please excuse the poor video quality. I've checked things like, making sure the water graphics line up with the physics representation etc, the debug display is on (although you can barely tell in the video). I think that the density calculated is too great and is overcoming gravity and throwing the body out but, the body still behaves strangely even if that is the case. I've tried reproducing this error using the SDK but it works when I create a box of the same size and mass and doesn't display this problem.

Does anyone recognise this symptom or could possibly suggest steps I could take to sort it?

Just to double check, my understanding is that;
liquidMass = liquidDensity * bodyVolume and,
BodyMass = liquidMass = liquidDensity * BodyVolume
so,
liquidDensity = BodyMass / BodyVolume;

Is that correct or am I doing it all wrong?

Ideally I'd like to be able to configure the WaterInfo.fViscosity so one value makes objects sink more and a different (higher/lower) value makes objects float more.

Thanks for any help.
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: Buoyancy Problem

Postby Aphex » Thu Aug 14, 2008 5:20 pm

I found buoyancy very hard to tune to my requirements (bodies of largely differing masses). The Newton wiki details a calibration process you need to do to get sensible results.
I also found applying quite severe linear and angular damping to objects in the water helped the most.
Aphex
 
Posts: 144
Joined: Fri Jun 18, 2004 6:08 am
Location: UK

Re: Buoyancy Problem

Postby Andy Price » Fri Aug 15, 2008 4:18 am

Thanks for the reply. I don't think the problem I have is from calibration, ie - my code doesn't work for 'any' body, it isn't just bodies with a larger mass / volume ratio. I haven't got this working for anything that falls in water yet and as I said in my earlier post, the SDK demo works when I use the same size cube and same mass so I know it's something I'm doing wrong.

The wiki page is a copy and paste from a forum post and seems to basically say; get it working for 1 body and then adjust all others to fit within the scale of the one you got working. It seems there isn't a 'solution' and it'll need tweaking on a per body basis.

I tried your suggestion about adding damping just in case and this stops the oscilation of flying into the air, then beneath the surface etc and it comes to a rest within 20 seconds now but, it comes to rest above the water - not touching the surface but literally levitating above it. You can't see on the video very well but, when the body reaches the height of it's rise, it suddenly stops, it doesn't slowly decrease in speed. It looks as if it's hit something it's so sudden - I think this is because all the uplift is cut out as it leaves the water volume.

What could be causing this behaviour?
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: Buoyancy Problem

Postby Aphex » Tue Aug 19, 2008 8:31 am

Sounds like a deactivation/sleep problem. Have you set the body to non-freezing?

(P.S. In my project I implemented my own buoyancy using Newton forces in the end - I needed more control.)
Aphex
 
Posts: 144
Joined: Fri Jun 18, 2004 6:08 am
Location: UK

Re: Buoyancy Problem

Postby Andy Price » Tue Aug 19, 2008 10:45 am

As far as I can tell it remains active but I will double check and edit this post with the answer later tonight. If your bouyancy system is worthwhile and I still can't get the "correct" way to work, would you perhaps be interested in talking me through some of what you've done?
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: Buoyancy Problem

Postby Julio Jerez » Tue Aug 19, 2008 11:21 am

The problem with the bouyancy function is that it is very unstable.
It is one of those things that are mathematically correct but numerically unstable.
The reason is that say you have a body that is long and flat, the body will be flat of the water, if you applui a very small forec on any corner.
there soudl will be a very small intesection angle between the water line and the part of the body the is under water.
however that small intesection lead to very big error when claculation the intersection of the volume under water.
The bouyancy is Ok fo small bodies that are with a more less aspect ratio of 1, for big bodies and better model is need it.

I am considering going back to the definition bouyancy at the fundametal level and calculate the bouyycancy force as a sumation of columns under the water line.
These method shoud be more presise for larger bodies and also can handle water waves.
Aphex wrote:(P.S. In my project I implemented my own buoyancy using Newton forces in the end - I needed more control.)

coudl you say hwo you when about, maybe I can put a demo so teh peopel can benefic for it?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Buoyancy Problem

Postby Aphex » Tue Aug 19, 2008 5:43 pm

Yeah pretty simple in the end. I distribute a number of 'fluid samples' inside each object's mesh, and apply a force to each via Newton, plus angular & linear damping to the body. The force applied for an 'area' sample = upVector * WaterDensity * coeff. For a 'volume' sample it's multiplied proportional to its depth.
It's described in detail here: (Scroll down to 'Realistic Computational Fluid Dynamics') http://www.brightland.com/physics/
Note that I needed to turn off most of Newton's default damping to get good results (I use the same system for aircraft, and this caused me a few tuning problems!)
The buoyancy forces can be 'seen in action' here: Image
Aphex
 
Posts: 144
Joined: Fri Jun 18, 2004 6:08 am
Location: UK

Re: Buoyancy Problem

Postby Julio Jerez » Tue Aug 19, 2008 6:42 pm

basically you are doing a course aproximation of what I was thinking,
Populating the shape with simple bouyancy volumes that are small but for witch the for calculation is stable, and fast.
Good idea.

Is that image using newton 2.0 in doubles presition? 8)
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Buoyancy Problem

Postby Aphex » Wed Aug 20, 2008 8:33 am

Heh, that's an old image, but I do have Newton2 double integrated & working since last night :twisted:
I had to turn off the custom vehicle joint though, as it was crashing on creation. I suspect I need to rebuild the jointLib dll myself, though I get link errors using the archive vcproj (unresolved dVector & dMatrix externals).
Aphex
 
Posts: 144
Joined: Fri Jun 18, 2004 6:08 am
Location: UK

Re: Buoyancy Problem

Postby Andy Price » Wed Aug 20, 2008 10:56 am

I tried the body freeze/sleep state, it still happens. I can't see any obvious reason why I'm having the problem I've shown.

Julio, are you saying that Newton 2 will have a new/upgraded buoyancy feature? If so it's probably not worth me doing my own custom implementation and I'd be better off waiting.

I don't have boats, (the one I do have is treated as a moving platform with a matrix that 'rolls' like a boat) and I don't need anything amazingly fancy, I just wanted heavy objects to sink and light objects to float without too much hassle. I also can't rely on tweaking things on a per object basis. It's an rpg game and the most complicated it ever gets with water is the character swimming and diving.

Aphex - the dVector and dMatrix classes are included in the SDk as part of the math library and can be cast to other types, such as the D3DXVECTOR3 in DirectX.
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: Buoyancy Problem

Postby Julio Jerez » Wed Aug 20, 2008 12:57 pm

can you send me a test demo to see what it is, I may be a bug.
when it is teh time I will added to the SDK, do to the Engine.

now that the engine has the newtonMesh the bounacy can be done much easier.
but the will be after I finish the car and the animation.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Buoyancy Problem

Postby Andy Price » Wed Aug 20, 2008 1:04 pm

I will see if I can get a demo to send to you, (just the usual email?) but, most of my attempts to reproduce the problem fail, making it difficult to diagnose. That's why I was asking if anyone had ever had a similar problem.
Andy Price
 
Posts: 26
Joined: Mon Sep 18, 2006 11:45 pm

Re: Buoyancy Problem

Postby Nearga » Tue Sep 02, 2008 5:13 pm

Code: Select all
void ApplyBuoyancy (const NewtonMaterial* material, const NewtonBody* body0, const NewtonBody* body1, int threadIndex)
{
   const NewtonBody* body;
   const NewtonBody* trigger;

   // get the body with the trigger volume
//   if (NewtonCollisionIsTriggerVolume (NewtonBodyGetCollision(body0))) {
//      body = body1;
//      trigger = body0;
//      } else {
//      body = body0;
//      trigger = body1;
//   }
   
   NewtonDestroyBody(nworld,body);

   //   NewtonBodyAddBuoyancyForce (body, 0, 0.8, 0.8, vectorf(0,-10,0), PhysicsBouyancyPlane, (void*) trigger);
   return 1;
}

action newt_water()
{
   set(my,TRANSLUCENT);
   my.alpha=25;
   wait(2);

   defaultMaterialID = NewtonMaterialGetDefaultGroupID (nworld);
   bouyancyForceMaterialID = NewtonMaterialCreateGroupID (nworld);
   NewtonMaterialSetCollisionCallback (nworld, bouyancyForceMaterialID, defaultMaterialID, NULL, ApplyBuoyancy, NULL);
   
   NewtonBody* body = newton_addentity(me, 0, NEWTON_BOX, onforceandtorque);
   NewtonBodySetMaterialGroupID(body,bouyancyForceMaterialID);
   
   NewtonCollision* collision = NewtonBodyGetCollision(body);
   NewtonCollisionSetAsTriggerVolume(collision, 1);
}


this one crashes, but if i commented
Code: Select all
//NewtonMaterialSetCollisionCallback (nworld, bouyancyForceMaterialID, defaultMaterialID, NULL, ApplyBuoyancy, NULL);

everythins works fine... what it could be?
Nearga
 
Posts: 21
Joined: Thu Jul 31, 2008 11:47 am

Re: Buoyancy Problem

Postby Leadwerks » Sun Apr 26, 2009 4:31 pm

I would be interested in a buoyancy model that handles waves, since my water is 3D. It would pretty neat for small boats.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Buoyancy Problem

Postby Aphex » Mon Apr 27, 2009 4:03 am

The system I described above handles waves well. Note: I have to damp floating objects Y velocity to provide more realistic floating behaviour.
Aphex
 
Posts: 144
Joined: Fri Jun 18, 2004 6:08 am
Location: UK


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 13 guests

cron