diff --git a/.idea/.idea.PixelEngine/.idea/.name b/.idea/.idea.PixelEngine/.idea/.name new file mode 100644 index 0000000..708b0d6 --- /dev/null +++ b/.idea/.idea.PixelEngine/.idea/.name @@ -0,0 +1 @@ +PixelEngine \ No newline at end of file diff --git a/.idea/.idea.PixelEngine/.idea/indexLayout.xml b/.idea/.idea.PixelEngine/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.PixelEngine/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.PixelEngine/.idea/projectSettingsUpdater.xml b/.idea/.idea.PixelEngine/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/.idea/.idea.PixelEngine/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.PixelEngine/.idea/vcs.xml b/.idea/.idea.PixelEngine/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.PixelEngine/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PixelEngine/BlackHole.cs b/PixelEngine/BlackHole.cs deleted file mode 100644 index d32d096..0000000 --- a/PixelEngine/BlackHole.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Numerics; - -namespace PixelEngine; - -public class BlackHole : Hole -{ - public BlackHole(Vector2 position, int radius, float mass) : base(position, mass) - { - this.radius = radius; - } - - public override void Spawn(Fluid fluid) - { - fluid.reduceDensity((int)position.X, (int)position.Y, mass, radius); - } - - public override Vector2 position { get; set; } - public int radius { get; set; } - public override float mass { get; set; } -} \ No newline at end of file diff --git a/PixelEngine/Fluid.cs b/PixelEngine/Fluid.cs index b8adff8..ee4828b 100644 --- a/PixelEngine/Fluid.cs +++ b/PixelEngine/Fluid.cs @@ -1,4 +1,5 @@ -using Raylib_CsLo; +using System.Numerics; +using Raylib_CsLo; using static Raylib_CsLo.RayMath; namespace PixelEngine; @@ -130,23 +131,16 @@ public class Fluid } } } - - - - - /*var amtX = Raylib.GetMouseX(); - var amtY = Raylib.GetMouseY(); - - addVelocity(amtX / scale, amtY / scale, amtX * 2, amtY * 2);*/ } + public void addVelocity(int x, int y, float amountX, float amountY) { int index = IX(x, y); this.Vx[index] += amountX; this.Vy[index] += amountY; } - - + + void diffuse (int b, float[] x, float[] x0, float diff, float dt) { float a = dt * diff * (size - 2) * (size - 2); lin_solve(b, x, x0, a, 1 + 4 * a); diff --git a/PixelEngine/Game.cs b/PixelEngine/Game.cs new file mode 100644 index 0000000..d93908a --- /dev/null +++ b/PixelEngine/Game.cs @@ -0,0 +1,91 @@ +using System.Numerics; +using static Raylib_CsLo.Raylib; +using static Raylib_CsLo.RayGui; +using Raylib_CsLo; + +namespace PixelEngine; + +public class Game +{ + private const int Size = 64; + private const int Scale = 10; + private const int Iter = 10; + private const float Dt = 0.2f; + private const float Diffusion = 0f; + private const float Viscosity = 0.0000001f; + + private const string WindowTitle = "Fluid Simulation"; + private const int TargetFps = 60; + + private Fluid _fluid = null!; + + public Vector2 PMouse; + + public void Start() + { + _fluid = new Fluid(Dt, Diffusion, Viscosity, Size, Iter, Scale); + + InitWindow(Size * Scale, Size * Scale, WindowTitle); + SetTargetFPS(TargetFps); + } + + public void HandleInput() + { + + + // Add Density + if (IsMouseButtonDown(MouseButton.MOUSE_BUTTON_LEFT)) + { + _fluid.addDensity(GetMouseX() / Scale, GetMouseY() / Scale, 100); + } + + + // Reduce Density + else if (IsMouseButtonDown(MouseButton.MOUSE_BUTTON_RIGHT)) + { + _fluid.reduceDensity(GetMouseX() / Scale, GetMouseY() / Scale, 100,5); + + + //TODO: Suction for Density Reduction + + } + + //Mouse Velocity + if (Math.Abs(GetMouseX() - PMouse.X) > 0.1 || Math.Abs(GetMouseY() - PMouse.Y) > 0.1) + { + var amtX = GetMouseX() - PMouse.X; + var amtY = GetMouseY() - PMouse.Y; + + _fluid.addVelocity(GetMouseX() / Scale, GetMouseY() / Scale, amtX, amtY); + + PMouse = GetMousePosition(); + } + + + + + } + + public void UpdateSim() + { + _fluid.step(); + } + + public void UpdateDraw() + { + BeginDrawing(); + ClearBackground(BLACK); + + //Debug + DrawText("FPS: " + GetFPS(), 10, 10, 10, WHITE); + + + //Create a GUI Slider for Viscosity + var rect = new Rectangle(10, 20, 100, 20); + GuiSlider(rect, "", "", _fluid.visc, 0.0000001f, 0.01f); + + _fluid.renderD(); + + EndDrawing(); + } +} \ No newline at end of file diff --git a/PixelEngine/Hole.cs b/PixelEngine/Hole.cs deleted file mode 100644 index bb6cf6b..0000000 --- a/PixelEngine/Hole.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Numerics; - -namespace PixelEngine; - -public abstract class Hole -{ - public Hole(Vector2 position, float mass) - { - this.position = position; - this.mass = mass; - } - public abstract void Spawn(Fluid fluid); - public abstract Vector2 position { get; set; } - public abstract float mass { get; set; } -} \ No newline at end of file diff --git a/PixelEngine/Program.cs b/PixelEngine/Program.cs index 48e034e..0bb0fdb 100644 --- a/PixelEngine/Program.cs +++ b/PixelEngine/Program.cs @@ -1,4 +1,5 @@ -using System.Drawing; +using System.Diagnostics.SymbolStore; +using System.Drawing; using System.Numerics; using System.Security.Cryptography.X509Certificates; using static Raylib_CsLo.Raylib; @@ -8,125 +9,15 @@ using System.Drawing; using PixelEngine; using Raylib_CsLo; +Game game = new Game(); -/*var c = new CircleAndClock(); -c.Run();*/ -var N = 64; -var Scale = 10; -var Iter = 10; -float t = 0; -int whiteholes = 4; -int blackholes = 1; - -Fluid fluid; - -fluid = new Fluid(0.2f, 0, 0.0000001f, N, Iter, Scale); -InitWindow(N * Scale, N * Scale, "Raylib_CsLo"); -SetTargetFPS(60); -Random rd = new Random(); - -var pMouseX = GetMouseX(); -var pMouseY = GetMouseY(); - -var whiteholesA = new WhiteHole[whiteholes]; -var blackholesA = new BlackHole[blackholes]; - -for (int i = 0; i < whiteholes; i++) -{ - whiteholesA[i] = new WhiteHole( - new Vector2(rd.Next(3, N - 3), rd.Next(3, N - 3)), - new Vector2(rd.Next(50, 100), rd.Next(50, 100)), - rd.Next(0, 100), - rd.Next(0, 100) - ); -} - -for (int i = 0; i < blackholes; i++) -{ - blackholesA[i] = new BlackHole( - new Vector2(rd.Next(3, N - 3), rd.Next(3, N - 3)), - rd.Next(0, 3), - rd.Next(0,200) - ); -} - +game.PMouse = GetMousePosition(); +game.Start(); while (!WindowShouldClose()) { - BeginDrawing(); - ClearBackground(BLACK); - DrawFPS(10,10); - - /*int cx = (int)0.5*N/Scale; - int cy = (int)0.5*N/Scale; - for (int i = -1; i <= 1; i++) { - for (int j = -1; j <= 1; j++) { - fluid.addDensity(cx+i, cy+j, rd.Next(50, 150)); - } - }*/ - /*for (int i = 0; i < 2; i++) { - float angle = noise(t) * TWO_PI * 2; - PVector v = PVector.fromAngle(angle); - v.mult(0.2); - t += 0.01; - fluid.addVelocity(cx, cy, v.x, v.y ); - }*/ - - - /*fluid.step(); - fluid.renderD();*/ - - - if (IsMouseButtonDown(MouseButton.MOUSE_BUTTON_LEFT)) - fluid.addDensity((int)GetMouseX() / Scale, (int)GetMouseY() / Scale, 100); - else if (IsMouseButtonDown(MouseButton.MOUSE_BUTTON_RIGHT)) - { - fluid.reduceDensity((int)GetMouseX() / Scale, (int)GetMouseY() / Scale, 100, 5); - - - //TODO: Suction for Density Reduction - /*var amtX = GetMouseX() - pMouseX; - var amtY = GetMouseY() - pMouseY; - - fluid.addVelocity((int)GetMouseX() / Scale, (int)GetMouseY() / Scale, amtX, amtY);*/ - } - - - - - foreach (var hole in blackholesA) - { - hole.Spawn(fluid); - } - - foreach (var hole in whiteholesA) - { - hole.Spawn(fluid); - } - - - /*fluid.addDensity(N - 3 , 3, 100); - fluid.addVelocity(N - 3 , 3, -10f, 0);*/ - - - - - /*if (GetMouseX() != pMouseX || GetMouseY() != pMouseY) - { - - var amtX = GetMouseX() - pMouseX; - var amtY = GetMouseY() - pMouseY; - - fluid.addVelocity((int)GetMouseX() / Scale, (int)GetMouseY() / Scale, amtX, amtY); - }*/ - - pMouseX = GetMouseX(); - pMouseY = GetMouseY(); - - fluid.step(); - fluid.renderD(); - - - EndDrawing(); -} + game.HandleInput(); + game.UpdateSim(); + game.UpdateDraw(); +} \ No newline at end of file diff --git a/PixelEngine/WhiteHole.cs b/PixelEngine/WhiteHole.cs deleted file mode 100644 index 84089a2..0000000 --- a/PixelEngine/WhiteHole.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Numerics; - -namespace PixelEngine; - -public class WhiteHole : Hole -{ - public override void Spawn(Fluid fluid) - { - fluid.addDensity((int) position.X, (int) position.Y, mass); - fluid.addVelocity((int) position.X, (int) position.Y, direction.X * speed, direction.Y * speed); - } - - public override Vector2 position { get; set; } - public Vector2 direction { get; set; } - public override float mass { get; set; } - public float speed { get; set; } - - public WhiteHole(Vector2 position, Vector2 direction, float mass, float speed) : base(position, mass) - { - this.direction = direction; - this.speed = speed; - } -} \ No newline at end of file