How to do water?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

How to do water?

Postby pHySiQuE » Sat Jan 24, 2015 7:54 pm

Now that the buoyancy force function is gone, how do we make a water plane? I have this function but it seems to add random force to the object and doesn't look anything like buoyancy forces:
Code: Select all
   void NewtonDynamicsBody::AddBuoyancyForce(const float nx, const float ny, const float nz, const float d, const float fluidDensity, const float fluidViscosity)
   {
      if (collision)
      {
         dFloat shapeVolume = NewtonConvexCollisionCalculateVolume(collision);
         if (shapeVolume > 0.0f)
         {
            dFloat shapeOrigin[3] = { 0.0, 0.0, 0.0 };
            dFloat matrix[16];
            dFloat gravityVector[3] = { world->gravity.x, world->gravity.y, world->gravity.z };
            dFloat accel[3];
            dFloat alpha[3];
            dFloat fluidPlane[4] = { nx, ny, nz, d };
            
            NewtonBodyGetMatrix(body,matrix);
            NewtonBodyGetCentreOfMass(body,shapeOrigin);

            float waterToSolidVolumeRatio=fluidDensity;
            
            dFloat dens = 1.0f / (waterToSolidVolumeRatio * shapeVolume);

            NewtonConvexCollisionCalculateBuoyancyAcceleration(collision, matrix, shapeOrigin, gravityVector, fluidPlane, fluidDensity, fluidViscosity, accel, alpha);
            if (accel[0] != 0.0 || accel[1] != 0.0 || accel[2] != 0.0 || alpha[0] != 0.0 || alpha[1] != 0.0 || alpha[2] != 0.0)
            {
               NewtonBodyAddForce(body, accel);
               NewtonBodyAddTorque(body, alpha);
            }
         }
      }
   }
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: How to do water?

Postby Julio Jerez » Sat Jan 24, 2015 8:25 pm

the function is this
Code: Select all
void NewtonConvexCollisionCalculateBuoyancyAcceleration (const NewtonCollision* const convexCollision, const dFloat* const matrix, const dFloat* const shapeOrigin, const dFloat* const gravityVector, const dFloat* const fluidPlane, dFloat fluidDensity, dFloat fluidViscosity, dFloat* const accel, dFloat* const alpha)

the demo is ArchimedesBuoyancy.cpp
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to do water?

Postby pHySiQuE » Sun Jan 25, 2015 4:03 pm

This is what my behavior looks like (video freezes until about halfway through, just watch the whole thing):


Is this right?
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: How to do water?

Postby Julio Jerez » Mon Jan 26, 2015 1:43 pm

I first try making a box with a more regular aspect ratio.
see if you can get I stable first and them start change the dimensions gradually.

essentially the alchemies buoyancy force is a function of the volume under the plane
a shape with a very skew volume lead to a very stiff volume rate of change.

you can adjust it with damping, to a certain limit, beyond that the only thong that can make stable is a higher frequency update so the volume become a more smooth function.

If you can run the SDK demo, try to place one of the boxes with the same shape size, make I can see how to adjust it to be more stable.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to do water?

Postby pHySiQuE » Mon Jan 26, 2015 2:16 pm

I didn't really notice any effect with fluid viscosity. Shouldn't the viscosity of the water cause a huge amount of damping? Right now it just looks like it's been dropped a vertical wind tunnel:
Image
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm

Re: How to do water?

Postby Julio Jerez » Tue Jan 27, 2015 10:18 am

after I changed the function to operate on shapes rather than on a rigid body, the viscosity is not longer used. That was delegate to the end application

For realistic calculation the effect for the fluid viscosity on the body, the velocity must be known, and realistic viscose effects is a trivial calculation because viscosity act like a non linear thing on objects, It is like fly lift forces calculation, you can do a trivial calculation ot you can do a surface integration. You can however fake it very easy, in you function you can do something like this.
Code: Select all
            dMatrix matrix;
            dVector cog;
            dVector accelPerUnitMass;
            dVector torquePerUnitMass;
            const dVector gravity (0.0f, DEMO_GRAVITY, 0.0f, 0.0f);

            NewtonBodyGetMatrix (visitor, &matrix[0][0]);
            NewtonBodyGetCentreOfMass(visitor, &cog[0]);
            cog = matrix.TransformVector (cog);
            NewtonCollision* const collision = NewtonBodyGetCollision(visitor);

            
            dFloat shapeVolume = NewtonConvexCollisionCalculateVolume (collision);
            dFloat fluidDentity = 1.0f / (m_waterToSolidVolumeRatio * shapeVolume);
            dFloat viscosity = 0.995f;

            NewtonConvexCollisionCalculateBuoyancyAcceleration (collision, &matrix[0][0], &cog[0], &gravity[0], &m_plane[0], fluidDentity, viscosity, &accelPerUnitMass[0], &torquePerUnitMass[0]);

            dVector force (accelPerUnitMass.Scale (mass));
            dVector torque (torquePerUnitMass.Scale (mass));

// here this is show it was done before in 1.5 and 2.xx
            dVector omega;
            NewtonBodyGetOmega(visitor, &omega[0]);
            omega = omega.Scale (viscosity);
            NewtonBodySetOmega(visitor, &omega[0]);

            NewtonBodyAddForce (visitor, &force[0]);
            NewtonBodyAddTorque (visitor, &torque[0]);


I added that to the demo now.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to do water?

Postby pHySiQuE » Wed Feb 11, 2015 2:47 am

The damping made the behavior much better.
pHySiQuE
 
Posts: 608
Joined: Fri Sep 02, 2011 9:54 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron