Jump to content
  • Advertisement
Sign in to follow this  
Hairies

Calculate objects position in water affected by buoyancy

This topic is 2347 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}

}
}
Edited by Hairies

Share this post


Link to post
Share on other sites
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. Edited by Madhed

Share this post


Link to post
Share on other sites
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? Edited by Hairies

Share this post


Link to post
Share on other sites

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? Edited by Madhed

Share this post


Link to post
Share on other sites
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. Edited by Hairies

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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? Edited by Hairies

Share this post


Link to post
Share on other sites
Could you rephrase your problem? Actually, I'm afraid I don't quite get what it is that you want to know.

Share this post


Link to post
Share on other sites
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? Edited by Hairies

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!