# How to apply Buoyant force to a Box2d body?

I have been following a formula to calculate the buoyant force needed to apply to a dynamic body to be able to simulate water.

Fb = ρgV

1. Fb - is the buoyant force
2. ρ - density of the liquid
3. g - gravity
4. V - volume of the object

My box2d body will always be submerged to the sensor so I don't have to detect what portion of the body is in the water, basically the entire body is always in the water and I will always apply the force to the center of the body.

body.applyForceTocenter(0, Fb, true);


If I apply the force on upon collision to the censor then it will just fall cause there is not enough force, and if I type this to the render method then the body will fly cause there is too much force. So how do I implement the force? and how to compute for the drag? so that it won't bounce.

So how do I implement the force?

Considering that your body is always underwater, there's an easy way out of this one: just use gravity :) As iforce2d points out in his tutorial on custom gravity, you can set the gravity scale of any object:

//Box2D v2.2.1 onwards
body->SetGravityScale(0);//cancel gravity (use -1 to reverse gravity, etc)


So if you can control the bouyant force by tweaking the value you pass to setGravityScale. As long as it's less than 0 your object will be bouyant.

As usual if you want your body to experience drag then use setLinearDamping.

If you want to control the bounce then set the restitution field to some value between 0 and 1 when you create the fixture, or use the appropriate method to change it when the fixture has been created.