Topic: Map and marker shaking

Hi
I need to implement 2D UI online map with marker which would rotate together with compass so marker always should face forward. But now I have odd shaking of my map and marker. Also I can prevent it by creating marker in OnlineMapsLocationService component while setting "Create Marker" and asign marker rotation to map rotation in separate method and subscribe it to mapsLocationService.OnCompassChanged but that's not what I need
So how I can implement simultaneous rotation of marker (which be instatiated separatly) and map without shaking?
Here is exapmle: https://drive.google.com/open?id=1JOCgz … PVSDVXd_cm

Re: Map and marker shaking

Can you make functionality of rotating any of created marker by setting "rotate with compas" on it? Just like it implemented in OnlineLocationService script

Re: Map and marker shaking

Hello.

lerpCompassValue only works for the marker created by Online Maps Location Service.
The true compass value is passed to OnCompassChanged, and you need to smooth this value yourself.

Example:

using UnityEngine;

public class LerpMarker:MonoBehaviour
{
    public float compassThreshold = 0.022f;

    private float compassValue;
    private OnlineMapsMarker marker;
    private bool isFirst = true;

    private void OnCompassChanged(float value)
    {
        float offset = compassValue - value;

        if (offset > 0.5) offset -= 1;
        else if (offset < -0.5) offset += 1;

        if (!isFirst && Mathf.Abs(offset) < compassThreshold) return;

        isFirst = false;
        compassValue = value;
    }

    private void Start()
    {
        marker = OnlineMapsMarkerManager.CreateItem(OnlineMaps.instance.position);
        marker.align = OnlineMapsAlign.Center;

        OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;
    }

    private void Update()
    {
        float markerRotation = marker.rotation;
        float offset = compassValue - markerRotation;

        if (Mathf.Abs(offset) < float.Epsilon) return;

        if (Mathf.Abs(offset) < 0.003f) markerRotation = compassValue;
        else
        {
            if (offset > 0.5) compassValue -= 1;
            else if (offset < -0.5) compassValue += 1;

            markerRotation = Mathf.Lerp(markerRotation, compassValue, 0.02f);
        }

        marker.rotation = markerRotation;
        OnlineMaps.instance.Redraw();
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

4 (edited by jostikmr 2020-02-26 21:47:11)

Re: Map and marker shaking

Thanks for advice, I decided to use marker that created by Online Maps Location Service. But rotation of marker and  camera are smooth too mutch, like with delay. I want to implement rotation of marker like in google maps, where marker rotating when you change compass value even on 1 degree, but without shaking and jerking. I figure out that it's work much better if set t value in

value = Mathf.Lerp(value, trueHeading, 0.02f); (OnlineMapsLocationServiceBase.cs, line 614)

to higher, like 0.08f. Now marker moves without "delay" effect, but still little shaking. Can you explain how to improve it and why you set value of t to 0.02f?

Re: Map and marker shaking

Shaking is unstable values from the sensor.

Why do you think Google Maps handles a change in value even by 1 degree?
Can you confirm this, or is it just your thoughts?

There are several ways to remove shaking:
1. The threshold for changing the value.
2. Lerp or tween values.
3. Collect the last few values (4, 8, 10, 12, etc.), and based on them find the average value.
There are many algorithms for calculating the average value, for example: sum / quantity.

Online Maps Location Service uses options 1 and 2.

The value 0.02 was selected by the test way.

How to improve this - try to combine all three ways.
Something like:

using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class LerpMarker2 : MonoBehaviour
{
    public float headingThreshold = 0.5f;
    public int maxValues = 4;
    public float lerpValue = 0.08f;

    private float targetHeading;
    private OnlineMapsMarker marker;
    private List<float> headings;
    private bool compassInitialized = false;

    private void Start()
    {
        marker = OnlineMapsMarkerManager.CreateItem(OnlineMaps.instance.position);
        marker.align = OnlineMapsAlign.Center;

        headings = new List<float>();
        OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;
    }

    private void OnCompassChanged(float value)
    {
        compassInitialized = true;
        OnlineMapsLocationService.instance.OnCompassChanged -= OnCompassChanged;
        targetHeading = value * 360;
        marker.rotation = value;
        headings.Add(targetHeading);
    }

    private void Update()
    {
        if (!compassInitialized) return;

        if (headings.Count == maxValues) headings.RemoveAt(0);

        float heading = OnlineMapsLocationService.instance.trueHeading;
        float prevHeading = headings[headings.Count - 1];
        if (prevHeading - heading > 180) heading += 360;
        if (prevHeading - heading < -180) heading -= 360;

        headings.Add(heading);
        heading = headings.Average();

        if (Mathf.Abs(heading - targetHeading) > headingThreshold) targetHeading = heading;

        float targetRotation = targetHeading / 360;
        float rotationOffset = Mathf.Abs(marker.rotation - targetRotation);
        if (rotationOffset < float.Epsilon) return;

        if (rotationOffset < 0.0028) // 1 deg / 360
        {
            marker.rotation = targetRotation;
        }
        else marker.rotation = Mathf.Lerp(marker.rotation, targetRotation, lerpValue);

        OnlineMaps.instance.Redraw();
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Map and marker shaking

Thanks for your response, I keep experimenting with marker rotation and now I noticed strange behaviour of rotation marker with compass. For now I use next settings:
- I use marker that created by Location Service class
- "Use compass for marker" field enabled
- "Lerp compass value" field enabled
- value of "Compass threshold" = 0.3
For rotating my map with marker I use this example: http://infinity-code.com/atlas/online-m … ample.html
but for some reason my map starts to jitter when I try to change my compass direction to opposite (especially if I do it really fast). I tried to figure out why this happening but for now I came to only one conclusion which lies of marker lerping. I noticed that map jittering when marker still rotating (Lerp is not ended) and at this moment I try to change rotation to opposite side. Maybe this is the reason?

7 (edited by jostikmr 2020-03-10 01:04:27)

Re: Map and marker shaking

Here is video example: https://drive.google.com/open?id=1ibQs9 … h_odU1_zsa

Re: Map and marker shaking

Try replacing in the example:

OnlineMaps.instance.OnUpdateBefore += OnUpdateBefore;

to

OnlineMaps.instance.OnLateUpdateBefore += OnUpdateBefore;
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.