Calculate objects position in water affected by buoyancy

Started by
25 comments, last by FishFace 11 years, 11 months ago
Hello there,

im currently working on a framework to create my own games and wanted to add buoyancy physics to it.
I understand the fundamentals, but still have problems to implement it the right way into my framework.
I calculated the resulting force on the object by subtracing the gravity force from the buoyancy and worked out the resultgin acceleration.
I know that the density of the water gets bigger as the object rises in the water, but i dont know when it gets lower...but these are only some problems...anyways ill post my code here(written in C#) and hope somebody will be able to help me creating realistic buoyancy physics. In the moment the object just accelerates up and never returns down -,-...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DextroEngine;
using DextroEngine.Physics;
using Tao.OpenGl;
namespace Shooter
{
class buoyancy_test:IGameObject
{
Renderer _renderer = new Renderer();
Sprite s = new Sprite();
Gravitation g = new Gravitation(9.81f);
double pK = 0.75f;//object density
double pFl = 1;//water density
double watervolume = 0;//amount of object in water
double YIncrement = 0;//amount to increase/decrease objects posítion
double buoyancy = 0;
double bodyforce = 0;
double _totaltimePassed;
float PercentInWater = 9f;
bool moveup = false;
bool movedown = false;
public buoyancy_test()
{
s.SetWidth(10);
s.SetHeight(10);
s.rigidbody.Density = pFl;
s.rigidbody.SetVolume(s.GetWidth(), s.GetHeight(),10);
CalculateMass();
Get_pFl();
}
public double CalculateMass()
{
bodyforce = pK* g.gravitation * s.rigidbody.GetVolume();
s.rigidbody.Mass = bodyforce / g.gravitation;
return s.rigidbody.Mass;
}
public double InWaterVolume()
{
watervolume = s.GetWidth() * s.GetHeight() * PercentInWater;
return watervolume;
}
public double Get_pFl()
{
double v_inWater = InWaterVolume();
pFl = (s.rigidbody.Volume * pK)/v_inWater;
return pFl;
}
public void UpdateMovement(double time)
{
_totaltimePassed += time;
buoyancy = pFl * g.gravitation * s.rigidbody.GetVolume();
double result_force = buoyancy - bodyforce;
double a = result_force / s.rigidbody.Mass;
if ((pFl > pK)&&(pFl < 1))
{
moveup = true;
movedown = false;
}
if ((pFl >1))
{
moveup = false;
movedown = true;
}
if (moveup)
{
YIncrement += (a * 0.5 * (_totaltimePassed * _totaltimePassed));
PercentInWater -= 0.1f;
if (PercentInWater > 7.5f)
{
Get_pFl();
}
}
if (movedown)
{
YIncrement -= (a * 0.5 * (_totaltimePassed * _totaltimePassed));
PercentInWater += 0.1f;
if (PercentInWater < 9.0f)
{
Get_pFl();
}
}
}

public void Update(double elapsedTime)
{
UpdateMovement(elapsedTime);
s.SetPosition(0, 0+YIncrement);
}
public void Render()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
_renderer.DrawSprite(s);
}

}
}
Advertisement

I know that the density of the water gets bigger as the object rises in the water, but i dont know when it gets lower


Density of water does not change (or rather it is not neccessary to include that in a simple calculation). Maybe you just worded that wrong?
Bouyancy force is volume of displaced liquid times liquid density times gravitational force.
So the hard part is to find the amount of displaced liquid. In my buoyancy script I simply use the bounding box and check against the water surface to find the relative amount of displaced water.
But isnt it so, that the object in water floats if its density is equal to the water's density and it sinks if it is bigger and it rises if it is lower? so the density of the water somehow must change...actually finding the amount of displaced liquid shouldnt be too hard, because it is exactly the same as the amount of volume of the object submerged in water isnt it, so i just needed to calculate that volume?

But isnt it so, that the object in water floats if its density is equal to the water's density and it sinks if it is bigger and it rises if it is lower?

Yes, if the densities are equal the gravitation pulling down and the buoyancy pushing up cancel each other so the object floats.


so the density of the water somehow must change

I still don't understand what you mean by that? Why must the density change? To move the object around?
you said that the density of water doesnt change, but how can the object sink or rise, if the waters density stays the same?
From wikipedia i got this:
"A body often rises till it breaks through the waters surface. In this case : V_submerged * p_fluid = V_body * p_body.
Because if the density of the object is different from the density of water it will receive a force that will cause an acceleration.
This force will be constant as long as all the parameters stay the same (density, volume, gravity). Actually density of water is bigger if you go deeper.
But according to wikipedia it increases by only 5% at 10km depth which is negligible for any simple game physics simulation that I can imagine.
So, actually the denities of both water and object don't change? im confused...why am i then reading that

1) if p_fluid < p_body : Object sinks
2) if p_fluid = p_body: Object floats
3) if p_fluid > p_body: Object rises...?

Apart from that, can you help me calculating the correct motion of the object in water influenced by bouyency?
Could you rephrase your problem? Actually, I'm afraid I don't quite get what it is that you want to know.
first i want to know that how is it that

1) if p_fluid < p_body : Object sinks
2) if p_fluid = p_body: Object floats
3) if p_fluid > p_body: Object rises,

if both water and object density dont change?
I'm sorry I still don't know what you mean...

If you want to learn the fundamental principles of how buoyancy works you should check out wikipedia or a physics book.
If you just want to implement it in your simulation everything that needs to be done has already been said.

This topic is closed to new replies.

Advertisement