250cc mx engine, and dyno

This commit is contained in:
max
2026-06-09 20:20:56 +02:00
parent aba9b76530
commit ac2eab6f83
5 changed files with 385 additions and 82 deletions

View File

@@ -2,6 +2,8 @@
using SFML.System;
using FluidSim.Core;
using FluidSim.Components;
using System;
using System.Collections.Generic;
namespace FluidSim.Tests
{
@@ -10,11 +12,200 @@ namespace FluidSim.Tests
protected const float AmbientPressure = 101325f;
protected const float AmbientTemperature = 300f;
public float Throttle { get; set; }
public float Load { get; set; }
public Font? Font { get; set; }
public abstract void Initialize(int sampleRate);
public abstract float Process();
public abstract void Draw(RenderWindow target);
// ---- Dyno curve graph ----
private const float RpmBinSize = 50f;
private readonly List<(float powerKw, float torqueNm)> _dynoBins = new();
private int _lastDynoBin = -1;
public void ResetDynoCurve()
{
_dynoBins.Clear();
_lastDynoBin = -1;
}
protected void UpdateDynoCurve(float rpm, float powerKw, float torqueNm)
{
if (rpm <= 0) return;
int bin = (int)(rpm / RpmBinSize);
while (_dynoBins.Count <= bin)
_dynoBins.Add((0f, 0f));
if (_lastDynoBin >= 0 && bin > _lastDynoBin + 1)
{
float lastPower = _dynoBins[_lastDynoBin].powerKw > 0 ? _dynoBins[_lastDynoBin].powerKw : 0f;
float lastTorque = _dynoBins[_lastDynoBin].torqueNm > 0 ? _dynoBins[_lastDynoBin].torqueNm : 0f;
for (int b = _lastDynoBin + 1; b < bin; b++)
{
float t = (b - _lastDynoBin) / (float)(bin - _lastDynoBin);
float interpPower = lastPower + (powerKw - lastPower) * t;
float interpTorque = lastTorque + (torqueNm - lastTorque) * t;
if (interpPower > _dynoBins[b].powerKw || _dynoBins[b].powerKw <= 0)
_dynoBins[b] = (interpPower, _dynoBins[b].torqueNm);
if (interpTorque > _dynoBins[b].torqueNm || _dynoBins[b].torqueNm <= 0)
_dynoBins[b] = (_dynoBins[b].powerKw, interpTorque);
}
}
var current = _dynoBins[bin];
if (powerKw > current.powerKw || current.powerKw <= 0)
current.powerKw = powerKw;
if (torqueNm > current.torqueNm || current.torqueNm <= 0)
current.torqueNm = torqueNm;
_dynoBins[bin] = current;
_lastDynoBin = bin;
}
protected void DrawDynoCurve(RenderWindow target,
float graphX, float graphY, float graphWidth, float graphHeight,
float currentRpm, float currentPowerKw)
{
if (_dynoBins.Count == 0) return;
float maxPowerKw = 0.01f, maxTorqueNm = 0.01f, maxRpm = 1000f;
for (int b = 0; b < _dynoBins.Count; b++)
{
var bin = _dynoBins[b];
if (bin.powerKw > 0 || bin.torqueNm > 0)
{
float rpmBin = b * RpmBinSize + RpmBinSize / 2f;
if (bin.powerKw > maxPowerKw) maxPowerKw = bin.powerKw;
if (bin.torqueNm > maxTorqueNm) maxTorqueNm = bin.torqueNm;
if (rpmBin > maxRpm) maxRpm = rpmBin;
}
}
maxPowerKw *= 1.1f;
maxTorqueNm *= 1.1f;
maxRpm = MathF.Max(maxRpm * 1.05f, 1000f);
var bg = new RectangleShape(new Vector2f(graphWidth, graphHeight))
{
FillColor = new Color(20, 20, 20, 200),
Position = new Vector2f(graphX, graphY)
};
target.Draw(bg);
const float leftMargin = 50f, rightMargin = 50f, topMargin = 20f, bottomMargin = 35f;
float plotX = graphX + leftMargin;
float plotY = graphY + topMargin;
float plotW = graphWidth - leftMargin - rightMargin;
float plotH = graphHeight - topMargin - bottomMargin;
float xMin = 0f, xMax = maxRpm;
float yLeftMin = 0f, yLeftMax = maxPowerKw;
float yRightMin = 0f, yRightMax = maxTorqueNm;
var powerColor = new Color(0xFF, 0x1B, 0x1B);
var torqueColor = new Color(0x09, 0x09, 0xFF);
var gridColor = new Color(50, 50, 50);
for (int i = 0; i <= 9; i++)
{
float t = i / 9f;
float x = plotX + t * plotW;
var vLine = new VertexArray(PrimitiveType.Lines, 2);
vLine[0] = new Vertex(new Vector2f(x, plotY), gridColor);
vLine[1] = new Vertex(new Vector2f(x, plotY + plotH), gridColor);
target.Draw(vLine);
}
for (int i = 0; i <= 5; i++)
{
float t = i / 5f;
float y = plotY + (1 - t) * plotH;
var hLine = new VertexArray(PrimitiveType.Lines, 2);
hLine[0] = new Vertex(new Vector2f(plotX, y), gridColor);
hLine[1] = new Vertex(new Vector2f(plotX + plotW, y), gridColor);
target.Draw(hLine);
}
DrawLabel(target, "RPM", new Vector2f(graphX + graphWidth / 2 - 12, graphY + graphHeight - 15), Color.White, 12);
DrawLabel(target, "kW", new Vector2f(graphX + 5, graphY + 2), Color.White, 11);
DrawLabel(target, "Nm", new Vector2f(graphX + graphWidth - 25, graphY + 2), Color.White, 11);
for (int i = 0; i <= 5; i++)
{
float leftValue = yLeftMin + (yLeftMax - yLeftMin) * i / 5f;
float rightValue = yRightMin + (yRightMax - yRightMin) * i / 5f;
float y = plotY + (1 - i / 5f) * plotH;
DrawLabel(target, $"{leftValue:F1}", new Vector2f(graphX + 2, y - 6), Color.White, 9);
DrawLabel(target, $"{rightValue:F1}", new Vector2f(graphX + graphWidth - 40, y - 6), Color.White, 9);
}
for (int i = 0; i <= 9; i++)
{
float value = xMin + (xMax - xMin) * i / 9f;
float x = plotX + i / 9f * plotW;
DrawLabel(target, $"{value / 1000f:F1}k", new Vector2f(x - 15, graphY + graphHeight - bottomMargin + 5), Color.White, 9);
}
var powerLine = new VertexArray(PrimitiveType.LineStrip);
bool firstPower = true;
for (int b = 0; b < _dynoBins.Count; b++)
{
float rpmBin = b * RpmBinSize + RpmBinSize / 2f;
if (rpmBin > xMax) break;
var bin = _dynoBins[b];
if (bin.powerKw > 0)
{
float sx = plotX + (rpmBin - xMin) / (xMax - xMin) * plotW;
float sy = plotY + (1 - (bin.powerKw - yLeftMin) / (yLeftMax - yLeftMin)) * plotH;
if (firstPower) { powerLine.Clear(); firstPower = false; }
powerLine.Append(new Vertex(new Vector2f(sx, sy), powerColor));
}
else if (!firstPower)
{
target.Draw(powerLine);
powerLine.Clear();
firstPower = true;
}
}
if (!firstPower) target.Draw(powerLine);
var torqueLine = new VertexArray(PrimitiveType.LineStrip);
bool firstTorque = true;
for (int b = 0; b < _dynoBins.Count; b++)
{
float rpmBin = b * RpmBinSize + RpmBinSize / 2f;
if (rpmBin > xMax) break;
var bin = _dynoBins[b];
if (bin.torqueNm > 0)
{
float sx = plotX + (rpmBin - xMin) / (xMax - xMin) * plotW;
float sy = plotY + (1 - (bin.torqueNm - yRightMin) / (yRightMax - yRightMin)) * plotH;
if (firstTorque) { torqueLine.Clear(); firstTorque = false; }
torqueLine.Append(new Vertex(new Vector2f(sx, sy), torqueColor));
}
else if (!firstTorque)
{
target.Draw(torqueLine);
torqueLine.Clear();
firstTorque = true;
}
}
if (!firstTorque) target.Draw(torqueLine);
if (currentRpm > 0 && currentRpm <= xMax && currentPowerKw > 0)
{
float sx = plotX + (currentRpm - xMin) / (xMax - xMin) * plotW;
float sy = plotY + (1 - (currentPowerKw - yLeftMin) / (yLeftMax - yLeftMin)) * plotH;
var dot = new CircleShape(2.5f)
{
FillColor = Color.White,
Position = new Vector2f(sx - 2.5f, sy - 2.5f)
};
target.Draw(dot);
}
}
// ---- Drawing helpers ----
protected Color PressureColor(float pressurePa)
{
float bar = pressurePa / 1e5f;
@@ -157,5 +348,18 @@ namespace FluidSim.Tests
}
target.Draw(va);
}
protected void DrawLabel(RenderWindow target, string text, Vector2f position, Color fillColor, uint characterSize = 14)
{
if (Font == null) return;
var txt = new Text(Font)
{
DisplayedString = text,
Position = position,
FillColor = fillColor,
CharacterSize = characterSize
};
target.Draw(txt);
}
}
}