I cant really explain this, but for example: rpm is 1436, it would get the value for 1000 rpm, and if its 1654 for example, it would get the value for 2000 rpm
local timingCurve = { -- Delay in milliseconds, delays the combustion after TDC. Example: [rpm] = <ms>
[0] = 3,
[1000] = 3,
[2000] = 6,
[3000] = 9,
[4000] = 12,
[5000] = 15,
[6000] = 18,
[7000] = 21,
[8000] = 24,
[9000] = 27
}
local function getRPMFromRadsPerSec(x: number)
return (x * 60) / (2 * math.pi)
end
RunService.Heartbeat:Connect(function(dt)
local rpm = getRPMFromRadsPerSec(flywheel.AssemblyAngularVelocity.X)
if cylinders[1].Head.Position.Y >= firingHeight then
end
end)
RunService.Heartbeat:Connect(function(dt)
local rpm = getRPMFromRadsPerSec(flywheel.AssemblyAngularVelocity.X)
if cylinders[1].Head.Position.Y >= firingHeight then
rpm = math.round(rpm)
print(timingCurve[rpm])
end
end)
If all the keys inside your table remain exact multiples of 1000 you can use rounding:
local curve = timingCurve[math.round(rpm/1000)*1000]
else if the keys are random but your table remains small you can brute-force the result using linear search:
local function rpmToCurve(rpm)
local closest = nil
for key, value in pairs(timingCurve) do
local distance = math.abs(rpm-key)
if not closest or distance < math.abs(rpm-closest) then
closest = key
end
end
return timingCurve[closest]
end
local curve = rpmToCurve(rpm)
else if you plan to use a much larger table you may want to use a modified version of the binary search algorithm that doesn’t look for an exact match.