Пример #1
0
        public override void Apply(Heightmap targetHeightmap, Bitmap targetBitmap, int x, int y, double size, double speed, double deltaTime)
        {
            double radius = size;

            //Make a bounding box around the circle
            int startX = (int)((double)x - size);
            int endX   = (int)((double)x + size);

            int startY = (int)((double)y - size);
            int endY   = (int)((double)y + size);

            //Make sure the corners are in bounds
            startX = Utils.CapBounds(startX, 0, targetHeightmap.width);
            endX   = Utils.CapBounds(endX, 0, targetHeightmap.width);

            startY = Utils.CapBounds(startY, 0, targetHeightmap.height);
            endY   = Utils.CapBounds(endY, 0, targetHeightmap.height);

            //Loop through the bounding box, skipping any pixels that fall outside the circle

            int centerX = x;
            int centerY = y;    //Save x and y, since we're using those variable names in the loop

            for (x = startX; x < endX; x++)
            {
                for (y = startY; y < endY; y++)
                {
                    //Skip this pixel if it's not within the radius
                    double squaredDist = (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY);
                    if (squaredDist > radius * radius)
                    {
                        continue;
                    }

                    //Make it so the speed decreases as it gets further from the center
                    double distPercent = squaredDist / (radius * radius);
                    double scaledSpeed = Utils.Lerp(speed, 0, distPercent);

                    //Change the height at this position
                    double val = targetHeightmap.GetValue(x, y);
                    val += scaledSpeed * deltaTime;

                    //Ensure the value stays within the limits
                    if (Math.Abs(val) > Heightmap.MAX_HEIGHT)
                    {
                        val = Math.Sign(val) * Heightmap.MAX_HEIGHT;
                    }

                    //Apply the height change
                    targetHeightmap.SetValue(x, y, val);
                    targetBitmap.SetPixel(x, y, Heightmap.ValueToColor(val));
                }
            } //End of double for loop
        }     //End of function
Пример #2
0
        public override void Apply(Heightmap targetHeightmap, Bitmap targetBitmap, int x, int y, double size, double speed, double deltaTime)
        {
            //Make a square around the coordinates
            int startX = (int)((double)x - size / 2);
            int endX   = (int)((double)x + size / 2);

            int startY = (int)((double)y - size / 2);
            int endY   = (int)((double)y + size / 2);

            //Make sure the corners of the square are in bounds
            startX = Utils.CapBounds(startX, 0, targetHeightmap.width);
            endX   = Utils.CapBounds(endX, 0, targetHeightmap.width);

            startY = Utils.CapBounds(startY, 0, targetHeightmap.height);
            endY   = Utils.CapBounds(endY, 0, targetHeightmap.height);

            //Iterate through the square, applying the logic to it.
            for (x = startX; x < endX; x++)
            {
                for (y = startY; y < endY; y++)
                {
                    //Change the height at this position
                    double val = targetHeightmap.GetValue(x, y);
                    val += speed * deltaTime;

                    //Ensure the value stays within the limits
                    if (Math.Abs(val) > Heightmap.MAX_HEIGHT)
                    {
                        val = Math.Sign(val) * Heightmap.MAX_HEIGHT;
                    }

                    //Apply the height change
                    targetHeightmap.SetValue(x, y, val);
                    targetBitmap.SetPixel(x, y, Heightmap.ValueToColor(val));
                }
            }
        }