Unity 3D mathf.clamp to multiple zones - c#

I'm building a knob that can only be turned to some fixed zones..
Now I'm using math clamp for one of these zones:
float clampedAngle = Mathf.Clamp(angle, -250f, 0f);
I want it to work for multiple zones, like this:
clampedAngle = Mathf.Clamp(angle, -250f, -230f);
clampedAngle = Mathf.Clamp(angle, -100f, -45f);
clampedAngle = Mathf.Clamp(angle, -30f, 0f);
Unfortunately the code above does not work, as it will clamp to the last value. How can I clamp a value to multiple valid zones?

The tricky part of this problem is determining which range to clamp to. One approach is to find the nearest min/max bound to the value, then clamp according to the corresponding range.
Assuming that your ranges are represented as an array of value pairs (two-value arrays), we can join them together and find the nearest min/max value by adapting the approach in this answer. Then, it's fairly easy to work backwards to determine which range the min/max value is from, and clamping accordingly:
// Clamps given value to nearest of given min/max pairs
private float ClampToNearestRange(float value, float[][] ranges)
{
// First, let's flatten the values into a single list
List<float> flattenedRanges = ranges.SelectMany(item => item).ToList();
// Now, we'll find the closest value in the list, and then get its index
float nearestValue = flattenedRanges.Aggregate((x,y) => Mathf.Abs(x-value) < Mathf.Abs(y-value) ? x : y);
int valueIndex = flattenedRanges.IndexOf(nearestValue);
// With the value index, we can deduce the corresponding range index
int rangeIndex = (valueIndex % 2 == 0) ? valueIndex / 2 : (valueIndex - 1) / 2;
// Finally, we'll clamp according to the range we selected
return Mathf.Clamp(value, ranges[rangeIndex][0], ranges[rangeIndex][1]);
}
You would then use the method like so:
// First, declaring your ranges somewhere in your class
float[] range1 = new float[]{0, 60};
float[] range2 = new float[]{80, 100};
float[] range3 = new float[]{150, 200};
float[][] ranges;
Start ()
{
ranges = new float[][]{range1, range2, range3};
float clampedAngle1 = ClampToNearestRange(120, ranges); // returns 100
float clampedAngle2 = ClampToNearestRange(126, ranges); // returns 150
float clampedAngle3 = ClampToNearestRange(170, ranges); // returns 170
}
Note: This does use LINQ, meaning if you need to do this often you might want to expand the logic into more Unity-friendly loops. Won't be as succinct, but it could affect your game performance.
Hope this helps! Let me know if you have any questions.

Related

Random number generator but avoid 0

I am generating a random number between a range but I want the number to not be 0. It can be 0.1, 0.2...etc but not 0. How do I do this?
public float selectedValue;
void Start()
{
selectedValue = Random.Range(-0.5f, 0.5f);
}
Keep finding random values until its value is not zero
float RandomNumExceptZero (float min, float max){
float randomNum = 0.0f;
do {
randomNum = Random.Range (min, max);
} while (randomNum == 0.0f );
return randomNum ;
}
Building on the suggestion of #Psi you could do this:
public float selectedValue;
void Start()
{
selectedValue = Random.Range(float.MinValue, 0.5f)*(Random.value > 0.5f?1:-1);
}
Random.Range() takes in 2 arguments in which the second argument is exclusive. You can use it for your advantage by excluding the value 0. The logic used is to find a random value between -0.5f and 0 (exclusive). Use another randomizer to get either a positive value or a negative value
public float selectedValue;
void Start()
{
selectedValue = Random.Range(-0.5f, 0);
int sign = Random.Range(0, 2);
// the value sign can be either 0 or 1
// if the sign is positive, invert the sign of selectedValue
if(sign) selectedValue = -selectedValue;
}
I just want to point out that there are 2,113,929,216 (*) float values in the interval [-0.5, 0.5) which gives a ≈ 0.000000047305 % chance that exactly 0.0f will be generated.
(*) found by brute force with C++ std::next_after but both implementation should follow IEEE 754 so I don't expect to be language differences in this regard, unless Unity somehow doesn't use subnormal numbers.
Well, just make if{} in Update() to pick another random number with same function if it is 0.0f. No way it will get 0.0f two times in a row

C# - Better ways to sort a list of colors

So before someone calls this post a duplicate, let me explain. I'm trying to find more optimized sorting algorithms to organize a list of colors based on a list of hex values. I'm currently sorting colors based on hue. This is fine, but when there are a bunch of colors, it seems to generate allot of noise.
I'm currently trying to find C# ways of sorting a giant list of random hex values. I have referenced this stackoverflow question and used some knowledgement from this site and created my code bellow.
RichTextBox1:
#ee82ee // Violet
#008000 // Green
#ffa500 // Orange
#0000ff // Blue
#ff0000 // Red
#ffff00 // Yellow
#4b0082 // Indigo
// Create a List from each color within the richtextbox
List<Color> tiledata = new List<Color> ();
foreach (string line in richTextBox1.Lines)
{
// Get Each Line From Richtoxbox And Convert The Hex
tiledata.Add(System.Drawing.ColorTranslator.FromHtml(line));
}
// Sort colors based on HUE
// https://stackoverflow.com/a/62203405/8667430
var hexColorsSorted = tiledata.OrderBy(color => color.GetHue()).ThenBy(o => o.R * 3 + o.G * 2 + o.B * 1);
// Expand Each Item Of The List
foreach (var color in hexColorsSorted)
{
// Output the data
Console.WriteLine(ColorConverterExtensions.ToHexString(color));
}
From above, this will obviously sort the colors in the correct rainbow order. However once you introduce more so similar colors, things start getting very messy from the image above. Currently the sorting algorithm is only sorting based on HUE, then by RGB. Is there another way or more I can do to this to clean things up a bit? My only request is to allow it to work with List<Tuple<Color>> such as an example as bellow.
.OrderBy(color => color.Item3.GetHue()).ThenBy(o => o.Item3.R * 3 + o.Item3.G * 2 + o.Item3.B * 1)
Bellow is a sorting algorithm this method seems to try and follow but ultimately fails. It would be nice if things looked similar to the following step sorting algorithms.
or
Upd: I made an adjustment to match the hue orientation more like the example.
I have recreated the style form your example, you just need to sort the array using this comparer:
class ColorRampComparer : IComparer<Color>
{
public int Compare(Color a, Color b)
{
var c1 = Step(a);
var c2 = Step(b);
return ((IComparable)c1).CompareTo(c2);
}
private Tuple<int, int, int> Step(Color color, int repetitions = 8)
{
int lum = (int)Math.Sqrt(.241 * color.R + .691 * color.G + .068 * color.B);
float hue = 1 - Rotate(color.GetHue(), 90) / 360;
float lightness = color.GetBrightness();
int h2 = (int)(hue * repetitions);
int v2 = (int)(lightness * repetitions);
// To achieve second style uncomment this condition
//if ((h2 % 2) == 0)
// v2 = repetitions - v2;
//else
// lum = repetitions - lum;
return Tuple.Create(h2, lum, v2);
}
private float Rotate(float angle, float degrees)
{
angle = (angle + degrees) % 360;
if (angle < 0) angle += 360;
return angle;
}
}
Results are the next:
And if you uncomment the fragment in Step function, then it will look like this:
To sort an array or enumerable use Sort method:
colors.Sort(new ColorRampComparer());

Distance between two grid cells, with no diagonal

I've been working on a small project for some days, everything was working fine until I changed my "map" implementation to be the same as in the game (Dofus) I'm based on (it's a little helper for the community).
Basically, I've a grid layout rotated at 45° (see image below), contructed from top left to bottom right. Every cell as an xIndex and zIndex to represent where it is (xIndex ; zIndex) on the image, and I just want to get the distance between two cells, without traveling diagonally.
As I tried to explain on the picture:
GetDistanceBetweenTiles(A, B) should be 3
GetDistanceBetweenTiles(A, C) should be 5
GetDistanceBetweenTiles(B, C) should be 2
I found the "Manhattan distance" which looks like it is what I want, but it's not giving me the values above.
Here is the code:
private int GetDistanceBetweenTiles(MovableObject a, MovableObject b)
{
//int dist = Mathf.Abs(a.xIndex - b.xIndex) + Mathf.Abs(a.zIndex - b.zIndex);
int minX = a.xIndex < b.xIndex ? a.xIndex : b.xIndex;
int maxX = a.xIndex > b.xIndex ? a.xIndex : b.xIndex;
int minZ = a.zIndex < b.zIndex ? a.zIndex : b.zIndex;
int maxZ = a.zIndex > b.zIndex ? a.zIndex : b.zIndex;
int distX = (maxX - minX);
int distZ = (maxZ - minZ);
int dist = Mathf.Abs(maxX - minX) + Mathf.Abs(maxZ - minZ);
print($"Distance between {a.name} and {b.name} is {dist}");
return dist;
}
Any help would be gladly appreciated.
If it can help, here is the project working with the first map implementation I did (but not translated yet).
Let make new coordinates in inclined rows with simple formulae:
row = z/2 - x ("/" for **integer division**)
col = z - row
Now we can just calculate Manhattan distance as
abs(row2 - row1) + abs(col2 - col1)
For your example
x z r c
4, 2 => -3, 5
1, 4 => 1, 4
distance = (1-(-3)) + (5-4) = 4 + 1 = 5
To explain: your grid rotated by 45 degrees:
0 1 2 3 4 5 6 7 8 \column
40|41 row -4
30|31|42|43 row -3
20|21|32|33|44|45 row -2
10|11|22|23|34|35|46|47 row -1
00|01|12|13|24|15|36|37|48 row 0
02|03|14|15|26|27|38 row 1
04|05|16|17|28 row 2
06|07|18 row 3
The "No-Maths" solution
I maybe have a workaround solution for you. I'm kind of a lazy person and very bad in maths ... so I usually let Unity do the maths for me in situations like yours ;)
For that you would need one dedicated GameObject that is rotated in the way that it represents the grid "rotation" so 0,45,0.
Then - since your tiles move always in steps of exactly 1 just in the rotated coordinate system - you could inetad of using an index based distance rather directly compare the absolute positions using Transform.InverseTransformPoint in order to get the positions relative to that rotated object.
InverseTransformPoint retuns as said the given world position in the local space of the used transform so that if the object was originally placed at e.g. x=1, z=1 in our rotated local space it will have the position z=1.1414..., x=0.
I simply attached this component to my rotated object .. actually I totate in Awake just to be sure ;)
public class PositionsManager : MonoBehaviour
{
// I know .. singleton pattern .. buuu
// but that's the fastest way to prototype ;)
public static PositionsManager Singleton;
private void Awake()
{
// just for making sure this object is at world origin
transform.position = Vector3.zero;
// rotate the object liek you need it
// possible that in your case you rather wanted -45°
transform.eulerAngles = new Vector3(0, 45, 0);
// since InverseTransformPoint is affacted by scale
// just make sure this object has the default scale
transform.localScale = Vector3.one;
// set the singleton so we can easily access this reference
Singleton = this;
}
public Vector2Int GetDistance(Transform from, Transform to)
{
var localPosFrom = transform.InverseTransformPoint(from.position);
var localPosTo = transform.InverseTransformPoint(to.position);
// Now you can simply get the actual position distance and return
// them as vector2 so you can even still see the components
// seperately
var difference = localPosTo - localPosFrom;
// since you are using X-Z not X-Y you have to convert the vector "manually"
return new Vector2Int(Mathf.RoundToInt(difference.x), Mathf.RoundToInt(difference.z));
}
public int GetAbsoluteDistance(Transform from, Trasnform to)
{
var difference = GetDistance(from, to);
return Mathf.Abs(difference.x) + Mathf.Abs(difference.y);
}
}
Now when you need to get the absolute distance you could simply do
var difference = PositionsManager.Singleton.GetDistance(objectA.transform, objectB.transform);
var absoluteDistance = PositionsManager.Singleton.GetAbsoluteDistance(objectA.transform, objectB.transform);
Little Demo (used a chess board drawer since I had that ^^)
The maths solution
It just came to me while writing the upper explenation:
You already know your steps between the tiles: It is allways Mathf.Sqrt(2)!
So again you could simply use the absolute positions in your world and compare them like
private float Sqrt2;
private void Awake()
{
Sqrt2 = Mathf.Sqrt(2);
}
...
// devide the actual difference by Sqrt(2)
var difference = (objectA.position - objectB.position) / Mathf.Sqrt(2);
// again set the Vector2 manually since we use Z not Y
// This step is optional if you anyway aren't interrested in the Vector2
// distance .. jsut added it for completeness
// You might need the rounding part though
var fixedDifference = new Vector2Int(Mathf.RoundToInt(difference.x), Mathf.RoundToInt(difference.z));
// get the absolute difference
var absoluteDistance = Mathf.Abs(fixedDifference.x) + Mathf.Abs(fixedDifference.y);
...
still completely without having to deal with the indexes at all.

Analyzing the object position using C# generic list

I am computing the distance of an object.
The X and Y position values first stored in two different Lists X and W.
Then I use another List for storing the distance covered by this object. Also, I refresh the lists if their count reaches 10, in order to avoid memory burden.
On the basis of distance value, I have to analyze, if the object is in the static position the distance should not increases. And on the text box display, the computed distance values appears to be static.
Actually, I am using sensors to compute the distance. And due to sensor error even if the object is in the static state the distance value varies. The sensor error threshold is about to be 15cm.
I have developed the logic, However, I receive error:
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
My code is as follows:
void distance()
{
List<double> d = new List<double>();
double sum = 0, sum1 = 0;
for (int i = 1; i < X.Count; i++)
{
//distance computation
if ((d[i] - d[i -1]) > 0.15)
{
sum1 = d.Sum();
sum = sum1 + dis1;
Dis = Math.Round(sum, 3);
}
}
// refresh the Lists when X, W and d List reach the count of 10
}
}
You do it totally wrong. Come up with a method computing a distance for a two given points. That's gonna be a func of signature double -> double -> double or, if you prefer C#, double ComputeDistance(double startPoint, double endPoint).
Then the only thing to do is to apply such a fuction to each pair of points you got. The easiest and most compact way to accomplish that is by means of Linq. It could be done in a regular foreach as well.
Take a note that it would be a way clearer if you will eventually merge your separated lists into a single list. Tuple<double, double> seems to be the best choice including performance.

Unity3D C# Negative Lerp?

I am wanting to lerp the variable that controls my animations speed so it transitions fluidly in the blend tree.
void Idle()
{
_agent.SetDestination(transform.position);
//Something like the following line
_speed = Mathf.Lerp(0.0f, _speed, -0.01f);
_animationController._anim.SetFloat("Speed", _speed);
}
I'v read that you can't lerp negatives, so how do I do this?
I think you're using Lerp bad.
As written in Unity Documentation (http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html), you should pass 3 floats, the minimum as first argument, the maximum as second and the time as third.
This third should be beetwen 0 and 1.
Aside from that, you can always make the number negative after lerping it.
Some examples :
In case you want from -10 to 10, lerp from 0 to 20 and then substract 10.
float whatever = Mathf.Lerp(0f, 20f, t);
whatever -= 10f;
In case you want from 0 to -50, lerp from 0 to 50 and then make it negative.
float whatever = Mathf.Lerp(0f, 50f, t);
whatever = -whatever;
The t value in a lerp function between a and b typically needs to be in the 0-1 range. When t is 0, the function returns a and when t is 1, the function returns b. All the values of t in between will return a value between a and b as a linear function. This is a simplified one-dimensional lerp:
return a + (b - a) * t;
In this context, negative values of t don't make sense. For example, if your speed variable is 4.0f, the result of lerp(0, 4, -0.01f) would be:
0 + (4 - 0) * -0.01
which returns -0.04.
The simplest way to solve this issue is to simply flip a and b instead of trying to use a negative t.
_speed = Mathf.Lerp(_speed, 0.0f, 0.01f);
Another suggestion would be to use Mathf.SmoothStep and store the original speed instead of applying the function on the constantly changing _speed to get a really smooth transition.
Well, you cannot actually.
A psuedo-codish implementation of a Lerp function would look like this:
float Lerp(float begin, float end, float t)
{
t = Mathf.Clamp(t, 0, 1);
return begin * (1 - t) + end * t;
}
Therefore, any attempt to give a t value outside of [0, 1], ie. an extrapolate attempt, would be clamped to the valid range.

Categories

Resources