How to add buoyancy in Newton 3? #345
-
|
I am implementing water in our engine and I have no idea how to add this in Newton 3. I have some old code for it, but there are parts commented out, and the applied force is zero. It looks like the command NewtonConvexCollisionCalculateBuoyancyAcceleration() was present in Newton.cpp at one time, but was removed?: How can I add buoyancy to an object? void PhysicsNode::AddBuoyancyForce(const dFloat nx, const dFloat ny, const dFloat nz, const dFloat d, const dFloat fluidDensity, const dFloat fluidViscosity)
{
dFloat Ixx;
dFloat Iyy;
dFloat Izz;
dFloat mass;
const float m_waterToSolidVolumeRatio = 0.5;
dVector m_plane = dVector(nx, ny, nz, d);
NewtonBodyGetMass(newtonbody, &mass, &Ixx, &Iyy, &Izz);
if (mass > 0.0f)
{
dMatrix matrix;
dVector cog(0.0f);
dVector accelPerUnitMass(0.0f);
dVector torquePerUnitMass(0.0f);
const dVector gravity(0.0f, world->gravity.y, 0.0f, 0.0f);
NewtonBodyGetMatrix(newtonbody, &matrix[0][0]);
NewtonBodyGetCentreOfMass(newtonbody, &cog[0]);
Mat4 mat = Mat4((float*)&matrix[0][0]);
Mat4 identity;
Vec3 v = TransformPoint(cog[0], cog[1], cog[2], mat, identity);
cog[0] = v.x; cog[1] = v.y; cog[2] = v.z;
//cog = matrix.TransformVector(cog);
NewtonCollision* const collision = NewtonBodyGetCollision(newtonbody);
dFloat shapeVolume = NewtonConvexCollisionCalculateVolume(collision);
if (shapeVolume > 0.0f)
{
dFloat fluidDentity = 1.0f / (m_waterToSolidVolumeRatio * shapeVolume);
dFloat viscosity = 0.990f;
//dFloat NewtonConvexCollisionCalculateBuoyancyVolume(const NewtonCollision* const convexCollision, const dFloat* const matrix, const dFloat* const fluidPlane, dFloat* const centerOfBuoyancy)
//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));
dVector omega(0.0f);
NewtonBodyGetOmega(newtonbody, &omega[0]);
omega = omega.Scale(viscosity);
NewtonBodySetOmega(newtonbody, &omega[0]);
NewtonBodyGetVelocity(newtonbody, &omega[0]);
omega = omega.Scale(viscosity);
NewtonBodySetVelocity(newtonbody, &omega[0]);
NewtonBodyAddForce(newtonbody, &force[0]);
NewtonBodyAddTorque(newtonbody, &torque[0]);
}
}
}` |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
what do you mean is commented out? it is in file |
Beta Was this translation helpful? Give feedback.
-
|
Okay, there is an example here of how to do this: Thank you for your answer. |
Beta Was this translation helpful? Give feedback.
what do you mean is commented out? it is in file
..\newton-3.14\applications\demosSandbox\sdkDemos\demos\ArchimedesBuoyancy.cpp