Search code examples
c#listunity-game-enginedistinct-values

C#/Unity - How to get only Distinct values (with threshold) and a specific count?


I have the following Problem:

I have a list Vector3 positions of an object I tracked in Unity (e.g. an animated sphere flying some track). I am adding the position of it every frame to the List. Which leads to a sum of e.g. 500 values. When it's animation stopped i want to "clean" the recorded track and only keep the most distinct values (in the right order). But the List.Count has to be EXACTLY 100. So it has to check for "equal" values with a threshold.

What i've done so far: I am recording the position of "trackableObject" as Vector3 in every frame and cleaning it directly (only keep values that are further away than 'cleaningDiffTolerance' and/or have a greater angle difference then 'cleaningAngleMaxDiff'). This way i am getting only values for significant changes in direction/distance and get more points i curves/corners.

What i want: Do the cleaning not every frame but do it after i stopped recording. Then I want to only keep the most distinct values in correct order and exactly 100 values.


Solution

  • It depends how precise your result needs to be (and how you define 'precise'). The first question would be:

    Must the 100 values be exact position values from the first list, or is it ok if it is near.

    Position values won't change much every frame. An easy way tou solve the problem would be to average every step:

    1. Compute how many values must be grouped together: n = totalValues/100
    2. Take the first n values and store the average in your final list
    3. Do the same for the next n values, and so on

    Alternatively, if you need to have exact values, replace step 2 by "take first value of the groupe" for example.

    This approche will be precise enough if the move is smooth. The problem with this is that if you have a sudden position change (like an angle instead of a smooth turn), you will likely not get the exact position at which the angle occure. The only solution to identify these is to do some more advance analysis. You can search on google for "High Pass Filter" for instance.

    I would recommend trying the simple approach first and see if it is fine for your needs