コード例 #1
0
        public void Execute([ReadOnly] ref TriangleData triangleData)
        {
            var currentGlobals = new TriangleGlobals(L2W, triangleData);

            // TODO: Add water mesh height calculation for wave physics.
            // Assuming water is flat and calm
            var h1 = new VertexData(currentGlobals.G1, currentGlobals.G1.y, 0);
            var h2 = new VertexData(currentGlobals.G2, currentGlobals.G2.y, 1);
            var h3 = new VertexData(currentGlobals.G3, currentGlobals.G3.y, 2);

            // All above water, ignore.
            if (h1.Height > 0 && h2.Height > 0 && h3.Height > 0)
            {
                return;
            }

            // All below water.
            if (h1.Height < 0 && h2.Height < 0 && h3.Height < 0)
            {
                UnderwaterTriangles.Enqueue(currentGlobals);
                return;
            }

            // Sorting points in descending order
            if (h1.Height < h3.Height)
            {
                Swap(ref h1, ref h3);
            }

            if (h1.Height < h2.Height)
            {
                Swap(ref h1, ref h2);
            }

            if (h2.Height < h3.Height)
            {
                Swap(ref h2, ref h3);
            }

            // Mathematics for the following two originated from:
            // https://gamasutra.com/view/news/237528/Water_interaction_model_for_boats_in_video_games.php
            // And overall code structure and commenting adapted from:
            // https://www.habrador.com/tutorials/unity-boat-tutorial/3-buoyancy/

            if (h2.Height < 0)
            {
                AddTrianglesOneAboveWater(h1, h2, h3);
            }
            else
            {
                AddTrianglesTwoAboveWater(h1, h2, h3);
            }
        }
コード例 #2
0
        private static float3 BuoyancyForce(float rho, TriangleGlobals triangle)
        {
            //Buoyancy is a hydrostatic force - it's there even if the water isn't flowing or if the boat stays still

            // F_buoyancy = rho * g * V
            // rho - density of the mediaum you are in
            // g - gravity
            // V - volume of fluid directly above the curved surface

            // V = z * S * n
            // z - distance to surface
            // S - surface area
            // n - normal to the surface

            var buoyancyForce = rho * Physics.gravity.y * triangle.DistanceToSurface()
                                * triangle.SurfaceArea() * triangle.Normal();

            //The vertical component of the hydrostatic forces don't cancel out but the horizontal do
            buoyancyForce.x = 0f;
            buoyancyForce.z = 0f;

            return(buoyancyForce);
        }