Useful buoyancy snippet.

scratchyrice

28-03-2009 17:03:34

This snippet comes directly from my game engine, But im sure the bright minds on here can easily figure it out. This code is used for buoyancy, Of dynamic waves like the hydrax system. I will soon put up a video demonstrating the effect.


//Get the current wave height
_waveHeight = _myPhysicsSystem->getSceneManager()->environment->getOceanSystem()->getHeight(_currentActorPosition->x, _currentActorPosition->z);

//Calculate the distance from the wave
_distanceFromWave = _currentActorPosition->y - _waveHeight;

//Check to see if the actor is below the current wave level
if (_distanceFromWave < 0)
{
//Calculate a buoyancy force.
_forcey = ((-_myPhysicsSystem->getGravity().y) * _currentActor->_getPhysXActor()->getMass())
+ (-_currentActor->_getPhysXActor()->getLinearVelocity().y * _currentActor->_getPhysXActor()->getMass())
+ ((_distanceFromWave) * _currentActor->_getPhysXActor()->getMass());

//Dampen the force, If needed. This is done so when a wave level drops, The boat does not seemingly plumet to its death.
if (-_distanceFromWave <= _currentActorHeight / 2)
{
_dampingPercentage = (_distanceFromWave / (_currentActorHeight / 2));
_dampingPercentage = _forcey * (-_dampingPercentage);
_forcey = _dampingPercentage;
}
_currentActor->_getPhysXActor()->addForce(Ogre::Vector3(0, _forcey, 0) , NxForceMode::NX_FORCE, true);
}


I hope someone finds this snippet useful

Cheers

Scratchy

nargil

28-03-2009 17:38:22

Could you record a movie and put on youtube ? I'd like to see it in action, but don't want to implement ;)

scratchyrice

28-03-2009 17:48:13

Indeed. I am currently in the process of polishing my engine, Ready for a new wip video. It will be out later on today, If not, definatly tommorow.

Cheers

Scratchy

betajaen

28-03-2009 18:19:15

Nice! I'm glad my code helped.

scratchyrice

28-03-2009 18:21:59

Nice! I'm glad my code helped.
Indeed it did! Thanks again for the help btw.

Cheers

Scratchy

scratchyrice

28-03-2009 20:54:30

Here you go, A video demonstrating the buoyancy physics in my engine.
http://www.youtube.com/watch?v=Z8qu10OEsng&feature=channel_page

Cheers

Scratchy

betajaen

28-03-2009 21:44:50

Really nice, and with sound too!

Are you going to introduce density and volume calculations, so you can have partially submerged bodies or even sinking ones, or just leave them all floating at the top?

scratchyrice

28-03-2009 22:03:55

Thanks XD. I am currently in the process of intergrating density calculations, But im just getting my head around the math atm.

Cheers

Scratchy

nargil

28-03-2009 22:05:55

awesome.

betajaen

28-03-2009 22:17:45

Thanks XD. I am currently in the process of intergrating density calculations, But im just getting my head around the math atm.

I find HyperPhysics a fantastic resource to look through;

float fBouyant = 9.81f * actor->getMass() * volume->getDensity() / actorDensity

I would recommend you cache the volume of the body (and the fluid)somewhere instead of calculating it each frame. It being a rigid body it's hardly going to change it's mass or shape. ;)

scratchyrice

28-03-2009 22:51:05

I would recommend you cache the volume of the body (and the fluid)somewhere instead of calculating it each frame. It being a rigid body it's hardly going to change it's mass or shape.
Thanks for the advice. I shall do this tommorow, Im going off to sleep now.

Cheers

Scratchy

boyamer

09-06-2009 16:16:32

Can you provide an example? I personally don't know how to apply this?

xius

10-12-2009 01:51:16

Very nice result there, I must admit!
I'm currently working on implementing buoyancy, actually. HyperPhysics is a great source, but there is something different I'd like to ask: when I have an aquarium full of fluid (the PhysX's one), how should I determine whether the actor is *in* it? (this kind of things is my weak spot :/) Or is there some other way to implement buoyancy when working with not so big volume?

Good luck with evolution of your project, scratchyrice!