Engine working

This commit is contained in:
max
2026-05-07 21:48:37 +02:00
parent 92d84eacfe
commit b3230844b7
14 changed files with 441 additions and 486 deletions

View File

@@ -14,7 +14,30 @@ namespace FluidSim.Tests
protected const double AmbientPressure = 101325.0;
protected const double AmbientTemperature = 300.0;
// ---------- Color helper ----------
// ---------- Color from pressure (volumes) ----------
protected Color PressureColor(double pressurePa)
{
double bar = pressurePa / 1e5; // convert to bar for easier mapping
byte r, g, b;
if (bar < 1.0) // vacuum → blue to green
{
double factor = Math.Clamp(bar, 0.0, 1.0);
r = 0;
g = (byte)(255 * factor);
b = (byte)(255 * (1.0 - factor));
}
else // above ambient → green to red
{
double factor = Math.Min((bar - 1.0) / 9.0, 1.0); // 1→10 bar maps to 0→1
r = (byte)(255 * factor);
g = (byte)(255 * (1.0 - factor));
b = 0;
}
return new Color(r, g, b);
}
// ---------- Color from temperature (pipes) ----------
protected Color TemperatureColor(double temperature)
{
double t = Math.Clamp(temperature, 0.0, 2000.0);
@@ -42,7 +65,7 @@ namespace FluidSim.Tests
{
var rect = new RectangleShape(new Vector2f(width, height))
{
FillColor = TemperatureColor(volume.Temperature),
FillColor = PressureColor(volume.Pressure), // ← pressurebased
Position = new Vector2f(centerX - width / 2f, topY)
};
target.Draw(rect);
@@ -60,7 +83,7 @@ namespace FluidSim.Tests
protected void DrawCylinder(RenderWindow target, Cylinder cylinder,
float centerX, float topY, float width, float maxHeight)
{
double fraction = cylinder.PistonFraction; // 0 = TDC, 1 = BDC
double fraction = cylinder.PistonFraction;
float currentHeight = (float)(maxHeight * fraction);
// Walls
@@ -69,10 +92,10 @@ namespace FluidSim.Tests
wall.Position = new Vector2f(centerX - width / 2f, topY);
target.Draw(wall);
// Gas
// Gas colored by pressure now
float gasTop = topY;
var gasRect = new RectangleShape(new Vector2f(width, currentHeight));
gasRect.FillColor = TemperatureColor(cylinder.Temperature);
gasRect.FillColor = PressureColor(cylinder.Pressure); // ← pressurebased
gasRect.Position = new Vector2f(centerX - width / 2f, gasTop);
target.Draw(gasRect);
@@ -95,7 +118,7 @@ namespace FluidSim.Tests
target.Draw(exhaustValve);
}
// ---------- Draw a pipe ----------
// ---------- Draw a pipe (unchanged) ----------
protected void DrawPipe(RenderWindow target, Pipe1D pipe, float pipeCenterY, float pipeStartX, float pipeEndX)
{
int n = pipe.CellCount;
@@ -141,7 +164,7 @@ namespace FluidSim.Tests
{
float x = centers[i];
float r = radii[i];
Color col = TemperatureColor(temperatures[i]);
Color col = TemperatureColor(temperatures[i]); // pipes still use temperature
stripVertices[idx++] = new Vertex(new Vector2f(x, pipeCenterY - r), col);
stripVertices[idx++] = new Vertex(new Vector2f(x, pipeCenterY + r), col);