Smoothing motion...spike filter equivalent?

Trying to smooth out ‘jitter’ or random ‘shivering’ motion of tracked virtual objects in an AR project.
Is there something equivalent to a ‘remove spikes’ filter in Unity or C# in general?

I’ve tried the Mathf.SmoothDamp method but it doesn’t do exactly what I am trying to achieve

The logic would be more along the lines of simply
if change in motion is too abrupt, please ignore

One way is a simple low-pass filter: instead of feeding the input value directly through, average it with a previous value, send the average through, and then update the previous with that average.

In pseudocode:

x = inputValue
average = (x + previous) / 2
previous = average
output = average

This will make things slightly less-responsive, but might be all you need to get rid of jitters.

You can do this with individual values like rotations along axes, or else with entire vectors. It’s best to experiment with what works best.

You can also make the mixing function asymetric, like so

average = (x * 5 + previous) / 6

That will make it more responsive to new input by using more of the new value, less of the previous.

average = (x + 3 * previous) / 4

And that would make it less responsive, even more low-pass filtered.

2 Likes

Thanks. Tried creating a buffer array, and then averaging that array. Your simple low pass filter might be all I need

Another option is to abuse lerp.

newPosition = Vector3.Lerp (oldPosition, newPosition, 0.2f);

Mess around with the magic number. It tends to ‘pull’ the object faster the further away it is from the setpoint.

2 Likes

and if you do be sure to multiply that “0.2f” by Time.deltaTime so thats its not Frame-dependent, which should reduce even more jitter.

EDIT: Mathf.SmoothDamp() is another great function thats commonly used for many camera motion scripts, and can be used to great effect here as well.

1 Like

Thanks. Position is relatively straightforward. Rotation using Quaternions is a bit more of a head scratcher.
And I assume when using Time.deltaTime it should always be called from Update?

Both the filter and the lerp method will still work here.

On the contrary, if you’re using Update then use Time.deltaTime. FixedUpdate would use Time.fixedDeltaTime. and if you want movement during Update even when Time.timescale is 0 (i.e. paused) then use Time.unscaledDeltaTime.

The real takeaway is to simply use the Time class to avoid jitter due to variable framerate. exactly which type of update you use doesn’t really matter so much.

It won’t solve sudden jumps from major frame drops, but it will smooth movement overall on stable FPS.

Thanks. Great info. In my case I don’t think jitter is connected directly to framerate variation. But Time.deltaTime still makes a lot of sense. Jitter in my case is almost entirely a result of the limitations of the Vuforia tracking engine.

Facing same issue can you please provide updated code

The code hasn’t changed. It’s a low pass filter. What have you tried, what are you having difficulty with?