Clamping reverses min and max

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    A not reversed math.clamp()?
  2. What is the issue?
    Basically I’m making a code that calculates engine RPM (Revolutions/Minute) and I clamped it, but the min and max seems to be flipped?
  3. What solutions have you tried so far?
    Tried flipping the min and max but that just returns an error.
    local function getEngineRPM(currentSpeedinRPS : number)
	local FULL_CIRCLE = math.pi * 2
	local MINUTE = 60
	local RPM = FULL_CIRCLE/(currentSpeedinRPS * MINUTE)
	
	return RPM * 1000
end
local function getSPStoRPS(currentSpeedinSPS : number)
	local speedInMeter = currentSpeedinSPS * 28/100
	local radiusInMeter = wheelRadius * 28/100
	local calculations = speedInMeter/radiusInMeter 
	-- Same concept as ω = v/r
	
	return calculations
end
        
local rpm = getEngineRPM(getSPStoRPS(engineSpeed))
script.RPM.Value = math.clamp(rpm, 700, 7000)

This is the formula for RPM:
RPM = (Linear Speed / (π × Diameter)) × 60

Try doing:

local function getSPStoRPM(currentSpeedinSPS : number) -- Removed conversion to meter as it would just cancel each other out.
	return currentSpeedinSPS/(math.pi*2*wheelRadius) * 60
end
        
local rpm = getSPStoRPS(engineSpeed)
script.RPM.Value = math.clamp(rpm, 700, 7000)

OKay ebwdobuqwdowqd 18j1398gbd1nd981hdn129d1d1gdh129n1

Thanks because now 700 is constant which I really need (ofc not)

Put a print in here to tell you what the number actually is. It might point out if you messed up the calculation by a decimal place or 2, or if the number is half of what you expect, indicating you need to go back and change your formula.

print(rpm)  -- if the number isn't what you figured it is then go back to your calculations.
script.RPM.Value = math.clamp(rpm, 700, 7000)

Well that’s not really nice of you. -_-

Yeah probably just wait for anyone else. I am out.

1 Like

I’m pretty sure that the formula @awry_y gave is correct. You just seem to have an unrealistic min. I don’t know what exactly your wheel radius is but if I calculated correctly, then with a wheel radius of 1, the speed in studs per second would need to be more than 73 in order to reach 700 RPM. If wheel radius is greater than 1, then an even bigger speed would be needed for 700 RPM.

My RPM was too high :joy: that’s why the clamping reverses but now I use this

local function getSpin(currentSpeedinSPS : number, radius : number)
	local speed = currentSpeedinSPS
	local radius = radius
	
	local calculations = speed/radius
	local circumference = (math.pi * 2) * radius
	
	local wheelSpeed = (currentSpeedinSPS/60)/circumference
	
	return wheelSpeed
end
local wheelSpin = getSpin(getEngineSpeed(carPrim, delta), wheelRadius)			
local engineRPM = ((wheelSpin * RatioIncludedGear) * 2000) + 700
engineRPM = math.clamp(engineRPM, 700, 7000)

You’re partially correct! I printed and it showed an insane number.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.