<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Map and marker shaking]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=1459&amp;type=atom" />
	<updated>2020-03-10T08:59:40Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=1459</id>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6058#p6058" />
			<content type="html"><![CDATA[<p>Try replacing in the example:<br /></p><div class="codebox"><pre><code>OnlineMaps.instance.OnUpdateBefore += OnUpdateBefore;</code></pre></div><p>to<br /></p><div class="codebox"><pre><code>OnlineMaps.instance.OnLateUpdateBefore += OnUpdateBefore;</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2020-03-10T08:59:40Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6058#p6058</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6057#p6057" />
			<content type="html"><![CDATA[<p>Here is video example: <a href="https://drive.google.com/open?id=1ibQs93eNFUeYW-s5tJ0CQ4h_odU1_zsa">https://drive.google.com/open?id=1ibQs9 … h_odU1_zsa</a></p>]]></content>
			<author>
				<name><![CDATA[jostikmr]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1231</uri>
			</author>
			<updated>2020-03-10T01:04:17Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6057#p6057</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6056#p6056" />
			<content type="html"><![CDATA[<p>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: <br />- I use marker that created by Location Service class<br />- &quot;Use compass for marker&quot; field enabled<br />- &quot;Lerp compass value&quot; field enabled<br />- value of &quot;Compass threshold&quot; = 0.3<br />For rotating my map with marker I use this example: <a href="http://infinity-code.com/atlas/online-maps/pages/RotateMapInsteadMarkerExample.html">http://infinity-code.com/atlas/online-m … ample.html</a><br />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?</p>]]></content>
			<author>
				<name><![CDATA[jostikmr]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1231</uri>
			</author>
			<updated>2020-03-10T01:02:51Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6056#p6056</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6027#p6027" />
			<content type="html"><![CDATA[<p>Shaking is unstable values from the sensor.</p><p>Why do you think Google Maps handles a change in value even by 1 degree?<br />Can you confirm this, or is it just your thoughts?</p><p>There are several ways to remove shaking:<br />1. The threshold for changing the value.<br />2. Lerp or tween values.<br />3. Collect the last few values (4, 8, 10, 12, etc.), and based on them find the average value. <br />There are many algorithms for calculating the average value, for example: sum / quantity.</p><p>Online Maps Location Service uses options 1 and 2.</p><p>The value 0.02 was selected by the test way.</p><p>How to improve this - try to combine all three ways.<br />Something like:<br /></p><div class="codebox"><pre><code>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&lt;float&gt; headings;
    private bool compassInitialized = false;

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

        headings = new List&lt;float&gt;();
        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 &gt; 180) heading += 360;
        if (prevHeading - heading &lt; -180) heading -= 360;

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

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

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

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

        OnlineMaps.instance.Redraw();
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2020-02-27T11:11:09Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6027#p6027</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6025#p6025" />
			<content type="html"><![CDATA[<p>Thanks for advice, I decided to use marker that created by Online Maps Location Service. But rotation of marker and&nbsp; 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&#039;s work much better if set t value in</p><p>value = Mathf.Lerp(value, trueHeading, 0.02f); (OnlineMapsLocationServiceBase.cs, line 614)</p><p>to higher, like 0.08f. Now marker moves without &quot;delay&quot; effect, but still little shaking. Can you explain how to improve it and why you set value of t to 0.02f?</p>]]></content>
			<author>
				<name><![CDATA[jostikmr]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1231</uri>
			</author>
			<updated>2020-02-26T21:32:34Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6025#p6025</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6022#p6022" />
			<content type="html"><![CDATA[<p>Hello.</p><p>lerpCompassValue only works for the marker created by Online Maps Location Service.<br />The true compass value is passed to OnCompassChanged, and you need to smooth this value yourself.</p><p>Example:<br /></p><div class="codebox"><pre><code>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 &gt; 0.5) offset -= 1;
        else if (offset &lt; -0.5) offset += 1;

        if (!isFirst &amp;&amp; Mathf.Abs(offset) &lt; 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) &lt; float.Epsilon) return;

        if (Mathf.Abs(offset) &lt; 0.003f) markerRotation = compassValue;
        else
        {
            if (offset &gt; 0.5) compassValue -= 1;
            else if (offset &lt; -0.5) compassValue += 1;

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

        marker.rotation = markerRotation;
        OnlineMaps.instance.Redraw();
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2020-02-25T18:58:38Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6022#p6022</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6021#p6021" />
			<content type="html"><![CDATA[<p>Can you make functionality of rotating any of created marker by setting &quot;rotate with compas&quot; on it? Just like it implemented in OnlineLocationService script</p>]]></content>
			<author>
				<name><![CDATA[jostikmr]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1231</uri>
			</author>
			<updated>2020-02-25T16:51:28Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6021#p6021</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Map and marker shaking]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=6020#p6020" />
			<content type="html"><![CDATA[<p>Hi<br />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 &quot;Create Marker&quot; and asign marker rotation to map rotation in separate method and subscribe it to mapsLocationService.OnCompassChanged but that&#039;s not what I need<br />So how I can implement simultaneous rotation of marker (which be instatiated separatly) and map without shaking?<br />Here is exapmle: <a href="https://drive.google.com/open?id=1JOCgzn2FfMUyB9Tb3MSlupPVSDVXd_cm">https://drive.google.com/open?id=1JOCgz … PVSDVXd_cm</a></p>]]></content>
			<author>
				<name><![CDATA[jostikmr]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=1231</uri>
			</author>
			<updated>2020-02-25T00:59:10Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=6020#p6020</id>
		</entry>
</feed>
