C
C#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.
C
canton743d 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
J
Jimmacle43d ago
i'm confused what the variables are, why does it sound like InputX is both the input and the output?
C
canton743d 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)
J
Jimmacle43d ago
a less abstract explanation of the problem trying to be solved might help (it would help me at least)
FF
Fabian F.43d 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
C
canton743d ago
I think my first comment answers your question still?
J
Jimmacle43d 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
FF
Fabian F.43d 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.
J
Jimmacle43d 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
FF
Fabian F.43d ago
Alright, I'll try that out and I'll get back to you!
J
Jimmacle43d 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
FF
Fabian F.43d ago
I might be confused by how was newValue caluclated in the first place?
J
Jimmacle43d ago
it's not calculated, that's your input
FF
Fabian F.43d ago
and currentValue might then be?
J
Jimmacle43d ago
the output going to your other code
FF
Fabian F.43d ago
Alright!
J
Jimmacle43d 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
FF
Fabian F.43d 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!
C
canton743d 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
More Posts
IValueResolver won't work with AutoMapperHi guys , hope all of you doing well, I'm recently working on a project where I need to convert a spHelp with concurrency blocking issueI have a small app that runs CPU bound on multiple threads (amount of processors) On one PC it workFrontend not workingI dont really keen on frontend but I think have some knowledge about it would be nice, I'm learning JsonSerializerOptionsTo be continuedC# Dictionary questionHi, if i have a dictionary in C# defined like so: `Dictionary<string, List<int>> test;` and I do `Li✅ Avalonia not showing rows in datagridThere are no rows in my datagrid in Avalonia, the headers are visible. I've checked if my list is emInfinite loop```c# /// ------------------------------------------------------------ /// <summary> /// Creates a sHow to connect my c# backend with the frontend?Hello, I created a ASP NET Core Web Api project with the command dotnet new webapi -minimal. In my ✅ Using `Func<ReadOnlySpan<char>,bool>` but not allowed due to Pointer TypeI am building a parser, and performance is of concern. So instead of allocating infinite strings wheconcurrency and atomicity Asp.net core web api and efcorelet's say there's an endpoint called AssingUser() ,this endpoint assigns one donation to this currenBuilding a generic repository for mongodbhello folks I was thinking build a generic repository structure for mongodb. but someday if i want Reading large xml file from archive by using XmlReader in Parallel modeHello 👋. I am looking for how can I read data from archive xml file in Parallel mode. I have archiResizing the Console window - Console ApplicationHi there, I'm trying to resize the console window in a Console Application. I have tried multiple ✅ Creating a datetime with a timezoneHow do I create a `DateTime` with `new DateTime(2010, 05, 12);` and give it a specific `TimeZoneInfoCustom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -3D Unity RaycastingHi guys, currently working on a puzzle game involving lasers and mirrors, however, have hit a brick