Bouyancy?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Bouyancy?

Postby Leadwerks » Wed Nov 05, 2008 11:27 pm

I am trying to add water physics.

Here I call the NewtonBodyAddBuoyancyForce function in the add body force & torque callback:
Code: Select all
NewtonBodyAddBuoyancyForce( newtonBody,0.1,0.1,0.1,[TWorld.current.gravity.x,TWorld.current.gravity.y,TWorld.current.gravity.z],NewtonGetBuoyancyPlane,Null)


And here is my NewtonGetBuoyancyPlane callback:
Code: Select all
   Function NewtonGetBuoyancyPlane:Int(collisionID:Byte Ptr,context:Byte Ptr,globalSpaceMatrix:Float Ptr,globalSpacePlane:Float Ptr) "c"
      globalSpacePlane[0]=0.0
      globalSpacePlane[1]=1.0
      globalSpacePlane[2]=0.0
      globalSpacePlane[3]=-15.6
      Return 1
   EndFunction


This only slows down bodies when they roll down a beach into the ocean. They are harder to roll, but they won't float. How can I increase the force so they float?
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Bouyancy?

Postby Leadwerks » Thu Nov 06, 2008 1:29 am

Ah, fluid density, it's all coming back now. :D
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Bouyancy?

Postby Leadwerks » Fri Nov 07, 2008 8:39 pm

So there is this callback:

NewtonGetBuoyancyPlane:Int(collisionID:Byte Ptr,context:Byte Ptr,globalSpaceMatrix:Float Ptr,globalSpacePlane:Float Ptr)

But my body matrix is not the same as the body's center of mass. What are the collisionID and context parameters? How should I get pass the body to the callback function so I can find the wave height at the body's center?

I am doing 3D waves, so the body center is important for finding the height:
http://www.youtube.com/watch?v=HrUEnT277WQ
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Bouyancy?

Postby Julio Jerez » Sat Nov 08, 2008 12:08 pm

for teh buoyancy demo, you can see that teh funtion pass a trigger volume as a paramete in teh cal lback

Code: Select all
static int ApplyBuoyancyAABBLevel (const NewtonMaterial* material, const NewtonBody* body0, const NewtonBody* body1, int threadIndex)
{
   const NewtonBody* body;
   const NewtonBody* trigger;
   RenderPrimitive* primitive;

   _ASSERTE (NewtonBodyGetUserData(body0));
   _ASSERTE (NewtonBodyGetUserData(body1));

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

   _ASSERTE (NewtonCollisionIsTriggerVolume (NewtonBodyGetCollision(trigger)));
   _ASSERTE (!NewtonCollisionIsTriggerVolume (NewtonBodyGetCollision(body)));

   dVector gravity (0.0f, -10.0f, 0.0f, 0.0f);

   primitive = (RenderPrimitive*) NewtonBodyGetUserData(body);

//   dFloat density;
//   dFloat mass;
//   dFloat dommy;
//   dFloat volume;
//   NewtonBodyGetMassMatrix(body, &mass, &dommy, &dommy, &dommy);
//   volume = NewtonConvexCollisionCalculateVolume(NewtonBodyGetCollision(body));
//   density = 1.5f * mass / volume;

// leadwerek check here
 // check here trigger is a trigger volume
   NewtonBodyAddBuoyancyForce (body, primitive->m_density, 0.8f, 0.8f, &gravity[0], PhysicsBouyancyPlane, (void*) trigger);
   return 1;
}




then in the callback I use a ray cast a vertical on the trigger volume using the center o fteh object which is opass to teh call back in the matrix parameter

Code: Select all
static int PhysicsBouyancyPlane (const int collisionID, void *context, const dFloat* matrix, dFloat* globalSpacePlane)
{
   const NewtonBody* trigger;
   const dMatrix& globalSpaceMatrix =  *(dMatrix*)matrix;

   trigger = (const NewtonBody*) context;

   // find the fluid surface by casting a ray on the trigger volume in the direction of teh gravity

   dVector p0 (globalSpaceMatrix.m_posit);
   dVector p1 (globalSpaceMatrix.m_posit);

   p0.m_y += 100.0f;
   p1.m_y -= 100.0f;

   // get the matrix of the trigger volume
   dMatrix volumeMatrix;
   NewtonBodyGetMatrix(trigger, &volumeMatrix[0][0]);

   // transform the ray to the local space of the trigger volume
   dVector q0 (volumeMatrix.UntransformVector(p0));
   dVector q1 (volumeMatrix.UntransformVector(p1));

   // cast the ray of the collision shape of the trigger volume
   int attribute;
   dFloat param;
   dVector normal;
   param = NewtonCollisionRayCast (NewtonBodyGetCollision(trigger), &q0[0], &q1[0], &normal[0], &attribute);

   _ASSERTE (param >= 0.0f);
   _ASSERTE (param <= 1.0f);

   // now rotate the ray normal  and the intersection point to global space
   normal = volumeMatrix.RotateVector(normal);
   dVector point (p0 + (p1 - p0).Scale(param));

   // return the plane equation
   globalSpacePlane[0] = normal.m_x;
   globalSpacePlane[1] = normal.m_y;
   globalSpacePlane[2] = normal.m_z;
   globalSpacePlane[3] = -(normal % point);
   return 1;   
}


what I wou do in your case in to pass teh pointer to teh water object ih nth conxtext, the shot the ray oin teh water surface.
to alliviate the problem of hitter vecaus you ciode will use a chngae susrafe to calcula teh plane, you can use a trick of sampel a few point aroun the surace of the water and average them
for example you can use the ray at the center at 100% plus a ray in te front, back, left an dright at 25% each then avarage themn together to get the surface normal.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Bouyancy?

Postby Leadwerks » Mon Nov 10, 2008 6:54 pm

So the context parameter is just an extra parameter for custom user data?
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Bouyancy?

Postby Julio Jerez » Tue Nov 11, 2008 1:03 am

yes.
you can encude a pointe to you water object there.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Bouyancy?

Postby Leadwerks » Thu Nov 13, 2008 12:29 am

Thanks, it works.
User avatar
Leadwerks
 
Posts: 569
Joined: Fri Oct 27, 2006 2:54 pm

Re: Bouyancy?

Postby VeT » Sat Feb 28, 2009 6:49 pm

okay... so i'm trying
Code: Select all
void onBuoforceandtorque(NewtonBody* body)
{
   float mass, ixx, iyy, izz;
   NewtonBodyGetMassMatrix(body, &mass, &ixx, &iyy, &izz);
   NewtonBodySetForce(body, vectorf(0, 0, -9.8 * mass));
   
   float grav[3];
   grav[0]=0.0;
   grav[1]=0.0;
   grav[2]=100.0;
   
   NewtonBodyAddBuoyancyForce (body, floatv(10), floatv(0.8), floatv(0.8), &grav[0], NULL, NULL);
}

action box_newt()
{
   wait(3);
   newton_addentity(me, 75, NEWTON_BOX, onBuoforceandtorque);
}


but seems to be that buoyancy force dont added
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: Bouyancy?

Postby VeT » Sat Feb 28, 2009 7:08 pm

i tried that in entity's callback and in material's calback, no results... i missed something, but what?....
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: Bouyancy?

Postby VeT » Sun Mar 01, 2009 6:37 pm

Code: Select all
int ApplyBuoyancyAABBLevel (NewtonMaterial* mat, NewtonBody* body0, NewtonBody* body1, int threadIndex)
{
   NewtonBody* body;
   NewtonBody* trigger;
   
   if (NewtonCollisionIsTriggerVolume (NewtonBodyGetCollision(body0))) {
      body = body1;
      trigger = body0;
   } else {
      body = body0;
      trigger = body1;
   }
   
   float grav[4]= {0,-10,0,0};
   
   NewtonBodyAddBuoyancyForce (body, 0.1, 0.8, 0.8, &grav[0], NULL, (void*) trigger); // PhysicsBouyancyPlane
   return 1;
}

action box_23_newt()
{
   wait(3);
   newton_addentity(me, 75, NEWTON_BOX, onforceandtorque);
}


action newt_water()
{
   wait(3);
   set(my,TRANSLUCENT);   
   
   static int waterID = NewtonMaterialCreateGroupID (nworld);   
            NewtonMaterialSetCollisionCallback(nworld, waterID, defaultID, NULL, ApplyBuoyancyAABBLevel, NULL );


   NewtonBody * temp_body = newton_addentity(me, 0, NEWTON_BOX, onBuoforceandtorque);
   NewtonBodySetMaterialGroupID(temp_body, waterID);
   
   NewtonCollision* collision = NewtonBodyGetCollision(temp_body);
   NewtonCollisionSetAsTriggerVolume(collision, 1);
}



Julio, what i do wrong? i wrap every string from your original code to lite-c, where problem could be?
Last edited by VeT on Mon Mar 02, 2009 5:53 am, edited 1 time in total.
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: Bouyancy?

Postby Julio Jerez » Mon Mar 02, 2009 1:38 am

have you checked the Bouyancy demo in the SDK?
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Bouyancy?

Postby VeT » Mon Mar 02, 2009 5:44 am

of course, i wrap every string to lite-c, exept "PhysicsBouyancyPlane", but as said in wiki, if NULL, then
When the parameter buoyancyPlane is set to NULL, the body is considered to completely immersed in the fluid.
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: Bouyancy?

Postby Julio Jerez » Mon Mar 02, 2009 8:32 am

you can see i fteh Buoyancy force was added by calling GetForce after the call to apply bouyancy force.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Bouyancy?

Postby VeT » Mon Mar 02, 2009 9:17 am

good idea, i'd try
1st prize: Lite-C and Newton 2.17 by Vasilenko Vitaliy
LiteC+Newton2 download: http://hosted.filefront.com/NeArGa
LiteC+Newton2 discussion: http://tinyurl.com/6l7y9v
VeT
 
Posts: 84
Joined: Thu Jul 31, 2008 11:31 am

Re: Bouyancy?

Postby coimbra79 » Wed Jun 30, 2010 10:46 am

hi boiz!!
I'm using this discussion insteand create a new with same content
as u i'm adding performant NGD buoyancy into my 3dEngine (coiAxis*), but result = 0 :oops:

what is bat into my vb6 code?
here are a semplification

__________________________________________________________________
dim collision as long
dim NewBody as long
'creating a new body
collision=NewtonCreateBox(newtonWorld, 1, 1, 1, 0)
NewBody=NewtonCreateBody(newtonWorld, collision)
NewtonReleaseCollision newtonWorld, collision
NewtonBodySetMassMatrix NewBody,Massa,Inerzia(0),Inerzia(1),Inerzia(2)
NewtonBodySetForceAndTorqueCallback NewBody, AddressOf ForceAndTorqueCallback

'gravity vector
Gravity4.(1) = 0
Gravity4.(1) = -10
Gravity4.(1) = 0
Gravity4.(1) = 0

'plave equation
PianoEQ4.(0) = 0
PianoEQ4.(1) = 1
PianoEQ4.(2) = 0
PianoEQ4.(3) = -2

NewtonBodyAddBuoyancyForce NewBody, 0.8, 0.8, 0.8, Gravity4(0), PianoEQ4(0), vbNull
__________________________________________________________________

it happen nothing! :x
User avatar
coimbra79
 
Posts: 42
Joined: Sun Jun 27, 2010 1:39 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron