Loading...
677
11 months ago

Simple water physics that I implemented in my old project.

It calculates water resistance, buoyancy and applies water current force to objects.

#Unity #Water #Physics #Simulation #Drag #Buoyancy #WaterCurrent #WaterFlow


The buoyancy is calculated based on the bounding box of the object ("Submergence" function).

		
			protected virtual void OnTriggerStay(Collider other)
{
    other.SendMessage("OnWaterStay", C, SendMessageOptions.DontRequireReceiver);
    R = other.GetComponent<Rigidbody>();
    if(R!=null)
    {
        //Flow
        R.velocity += globalFlow * Time.fixedDeltaTime / R.mass;
        //Drag
        V = R.velocity.magnitude;
        R.velocity = R.velocity.normalized * Mathf.Clamp(R.velocity.magnitude - ((density * V * V) / 2 * Time.fixedDeltaTime * R.drag) / R.mass, 0, float.PositiveInfinity);
        //Angular Drag
        V = R.angularVelocity.magnitude;
        R.angularVelocity = R.angularVelocity.normalized * Mathf.Clamp(R.angularVelocity.magnitude - ((density * V * V) / 2 * Time.fixedDeltaTime * R.angularDrag) / R.mass, 0, float.PositiveInfinity);
        //Buoyancy
        R.velocity -= (Physics.gravity * density * Submergence(C.bounds, other.bounds) * Time.fixedDeltaTime) / R.mass;
    }
}

public static float Submergence1D(float C,float R,float c,float r)
{
    if(R<r)
    {
        var tmp = R;
        R = r;
        r = tmp;
        tmp = C;
        C = c;
        c = tmp;
    }
    return Mathf.Clamp(2 * R - (Mathf.Abs((C + R) - (c + r)) + Mathf.Abs((C - R) - (c - r))), 0, 2 * r);
}

public static float Submergence(Bounds W,Bounds O)
{
    return Submergence1D(W.center.x, W.extents.x, O.center.x, O.extents.x) * Submergence1D(W.center.y, W.extents.y, O.center.y, O.extents.y) * Submergence1D(W.center.z, W.extents.z, O.center.z, O.extents.z);
}
		
	


0 comments

Loading...

Next up

Comparison of view transforms in Blender using Christmas candles as an example

#Blender #3D #3DArt #ViewTransform #Christmas #Candles

Lord Vader decides to delete his Minecraft world

Made in Blender.

#StarWarsArt #FanArtFriday #ArtWeeklies #Minecraft #Blender #Animation #3D #3DArt #DeathStar #Space

Diglett as chocolate cake

Made in Blender.

#MyPokeCake #Pokemon #Diglett #Cake #Blender #3D #3DArt #Modeling

Happy #WIPWednesday!

Are you working on a game? A song? Something else? Tell us in the comments!

Praise the Cat!

Blobcats worshiping their creator in an ancient temple.

Made in Blender for Art Weeklies - Monument theme.

#Blender #Animation #3D #3DArt #Modeling #ArtWeeklies #Cat #Blobcat #KniteBlargh #Golden #Monument #Temple #Pyramid #Ancient

🕹️ Enter The Highrise Game Jam Before It Ends On May 5! 🕹️

Learn the rules here: https://gamejolt.com/c/gamedev/highrisejam

Learn how to make a world on Highrise (which you MUST do to enter the jam) here: https://gamejolt.com/p/the-highrise-game-jam-is-underway-watch-t…

The jam has cash prizes! 💸💸💸

Snow globe with a city in retro style

Made in Blender for Art Weeklies - Item and City themes.

#Blender #Animation #3D #3DArt #Modeling #ArtWeeklies #City #Item #SnowGlobe #Retro #LowPoly #PerVertexLighting #PS1Graphics #Old3DGraphics #Early3DGraphics

My Pokémon Baking Book arrived today

#MyPokemonBakingBook #InsightEditions #Giveaway

Highrise Game Jam Reminder: In order to enter to win the Jam, your game must be created in Unity and submitted using the Highrise Studio Hub.

Learn all the deets about the jam at https://gamejolt.com/c/gamedev/highrisejam

To this day, I don't know how they did it

#PhotoDay