C
C#10mo ago
Fabian F.

clamping how fast a input (as a float) can change.

Let’s say my Input X float changes in a amount of time from -1 to 1 (-1, -0.5, 0, 0.5, 1) so as an example the speed is 12 (Which I calculate by subtracting the current input from last input and dividing that by Time which will result in the Speed of change). Now the float changes quicker, so from -1 to 1 (-1, 0, 1) so now the speed is 24, right? But I want a way that if it the input speed from InputX exceeds the example speed of 12, that the InputX speed still changes at the speed of 12. I tried for days but I have no idea what to do anymore.
19 Replies
canton7
canton710mo ago
Seems easy, unless I'm missing something? Given the previous value of X and the time, calculate what the new value if X would be if it was travelling at speed 12. If the actual new value of X is larger than this, limit it to that value you calculated
Jimmacle
Jimmacle10mo ago
i'm confused what the variables are, why does it sound like InputX is both the input and the output?
canton7
canton710mo ago
(I think it is. In my reading, it's an input to the system, and a rate limit is being applied to that system input)
Jimmacle
Jimmacle10mo ago
a less abstract explanation of the problem trying to be solved might help (it would help me at least)
Fabian F.
Fabian F.OP10mo ago
Sorry for the late response, my internet fumbled a bit. What I am trying to do is that if my Input which can change between -1 and 1 for X and Y on a gamepad, exceeds a set speed, it should still change at that set speed independent of how fast I change the input. I need this to make the player move in a circular motion, if the input variables X and Y are able to change too quickly, the player is able to spin like a helicopter, this is more so a polish feature than a gameplay feature, it would look nice with the animation system. I hope I am not being too abstract again! It's basically like my slowly turning the analog stick to move in a circle
canton7
canton710mo ago
I think my first comment answers your question still?
Jimmacle
Jimmacle10mo ago
yeah, you'd keep extra state for your "current" value and when the input changes, compare it and limit to a maximum change if needed
Fabian F.
Fabian F.OP10mo ago
Oh chucks, I accidentally skipped that message and jumped immedietly to jiMMACLE's message. Though I am quite unsure of how I would do said calculation, that is more so my issue than anything else to be honest.
Jimmacle
Jimmacle10mo ago
rough pseudocode
if newValue > currentValue + 12
currentValue += 12
else if newValue < currentValue - 12
currentValue -= 12
else
currentValue = newValue
if newValue > currentValue + 12
currentValue += 12
else if newValue < currentValue - 12
currentValue -= 12
else
currentValue = newValue
the actual threshold you'd compare to depends on the amount of time between changes
Fabian F.
Fabian F.OP10mo ago
Alright, I'll try that out and I'll get back to you!
Jimmacle
Jimmacle10mo ago
you're basically just clamping the change between a minimum and maximum value var limitedChange = Math.Min(12, Math.Max(newValue - currentValue, -12)) would be another way to calculate it
Fabian F.
Fabian F.OP10mo ago
I might be confused by how was newValue caluclated in the first place?
Jimmacle
Jimmacle10mo ago
it's not calculated, that's your input
Fabian F.
Fabian F.OP10mo ago
and currentValue might then be?
Jimmacle
Jimmacle10mo ago
the output going to your other code
Fabian F.
Fabian F.OP10mo ago
Alright!
Jimmacle
Jimmacle10mo ago
it accumulates the changes applied by limiting the new input value then for example, if your current value starts at 0 and you fed it a constant input of 144 it would take 12 iterations for the current value to match the raw input which sounds like the smoothing you're looking for
Fabian F.
Fabian F.OP10mo ago
Oh boy, this really is tough to understand but I suppose that I am starting to get it. I'll see what I can do and I will hop back in here if I have any more questions, for now thank you both!
canton7
canton710mo ago
I mean, you said you're calculating the speed from the change in value and the time. Calculating what the change in value would be if the speed is 12, given the same change in time, is just the same equation, rearranged slightly So if you have x1 (previous input), x2 (new input) and t (time), then speed = (x2 - x1) / t. Rearrange for x2 and you get x2 = (speed * t) + x1 So if x2 is larger than (12 * t) + x1, then the speed is faster than 12 Make sense?
var change = x2 - x1;
if (change > 12 * t)
{
change = 12 * t;
}
var clampedX2 = x1 + change;
var change = x2 - x1;
if (change > 12 * t)
{
change = 12 * t;
}
var clampedX2 = x1 + change;
(fix to handle x2 being less than x1 of course)
Want results from more Discord servers?
Add your server