<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Infinity Code Forum — Drag with two fingers]]></title>
	<link rel="self" href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=1310&amp;type=atom" />
	<updated>2019-08-01T20:49:13Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.infinity-code.com/viewtopic.php?id=1310</id>
		<entry>
			<title type="html"><![CDATA[Re: Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5393#p5393" />
			<content type="html"><![CDATA[<p>Something like that:<br /></p><div class="codebox"><pre><code>using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    public class DragAndZoomInertia : MonoBehaviour
    {
        /// &lt;summary&gt;
        /// Deceleration rate (0 - 1).
        /// &lt;/summary&gt;
        public float friction = 0.9f;

        private bool isInteract;
        private List&lt;double&gt; speedX;
        private List&lt;double&gt; speedY;
        private List&lt;float&gt; speedZ;
        private double rsX;
        private double rsY;
        private float rsZ;
        private double ptx;
        private double pty;
        private float pz;
        private const int maxSamples = 5;

        private OnlineMaps map;
        private OnlineMapsControlBase control;

        private void FixedUpdate()
        {
            if (isInteract &amp;&amp; control.GetTouchCount() == 0) isInteract = false;

            // If there is interaction with the map.
            if (isInteract)
            {
                // Calculates speeds.
                double tx, ty;
                map.GetTilePosition(out tx, out ty, 20);

                double cSpeedX = tx - ptx;
                double cSpeedY = ty - pty;
                float cSpeedZ = map.floatZoom - pz;

                int halfMax = 1 &lt;&lt; 19;
                int max = 1 &lt;&lt; 20;
                if (cSpeedX &gt; halfMax) cSpeedX -= max;
                else if (cSpeedX &lt; -halfMax) cSpeedX += max;

                while (speedX.Count &gt;= maxSamples) speedX.RemoveAt(0);
                while (speedY.Count &gt;= maxSamples) speedY.RemoveAt(0);
                while (speedZ.Count &gt;= maxSamples) speedZ.RemoveAt(0);

                speedX.Add(cSpeedX);
                speedY.Add(cSpeedY);
                speedZ.Add(cSpeedZ);

                ptx = tx;
                pty = ty;
                pz = map.floatZoom;
            }
            // If no interaction with the map.
            else if (rsX * rsX + rsY * rsY &gt; 0.001 || rsZ &gt; 0.001)
            {
                // Continue to move the map with the current speed.
                double tx, ty;
                map.GetTilePosition(out tx, out ty, 20);

                tx += rsX;
                ty += rsY;

                int max = 1 &lt;&lt; 20;
                if (tx &gt;= max) tx -= max;
                else if (tx &lt; 0) tx += max;

                map.SetTilePosition(tx, ty, 20);
                //control.ZoomOnPoint(rsZ, lastScreenPoint);
                map.floatZoom += rsZ;

                // Reduces the current speed.
                rsX *= friction;
                rsY *= friction;
                rsZ *= friction;
            }
        }

        /// &lt;summary&gt;
        /// This method is called when you press on the map.
        /// &lt;/summary&gt;
        private void OnMapPress()
        {
            // Get tile coordinates of map
            map.GetTilePosition(out ptx, out pty, 20);
            pz = map.floatZoom;

            // Is marked, that is the interaction with the map.
            isInteract = true;
        }

        /// &lt;summary&gt;
        /// This method is called when you release on the map.
        /// &lt;/summary&gt;
        private void OnMapRelease()
        {
            if (control.GetTouchCount() != 0) return;

            // Is marked, that ended the interaction with the map.
            isInteract = false;

            // Calculates the average speed.
            rsX = speedX.Count &gt; 0 ? speedX.Average() : 0;
            rsY = speedY.Count &gt; 0 ? speedY.Average() : 0;
            rsZ = speedZ.Count &gt; 0 ? speedZ.Average() : 0;

            speedX.Clear();
            speedY.Clear();
            speedZ.Clear();
        }


        private void Start()
        {
            map = OnlineMaps.instance; ;
            control = OnlineMapsControlBase.instance;

            // Subscribe to map events
            control.OnMapPress += OnMapPress;
            control.OnMapRelease += OnMapRelease;

            // Initialize arrays of speed
            speedX = new List&lt;double&gt;(maxSamples);
            speedY = new List&lt;double&gt;(maxSamples);
            speedZ = new List&lt;float&gt;(maxSamples);
        }
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2019-08-01T20:49:13Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5393#p5393</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5378#p5378" />
			<content type="html"><![CDATA[<p>&quot;smooth damping&quot; - do you mean inertia?<br />Yes, of course it is possible to implement.<br />It will take a couple of days (due to the release of Unity 2019.2).</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2019-07-30T17:42:35Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5378#p5378</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5373#p5373" />
			<content type="html"><![CDATA[<p>Works like a charm now!<br />Thank you very much! <img src="https://forum.infinity-code.com/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><p>Now for me there is just one more thing that reduces the usability of the touch controls: Google maps has a smooth damping when zooming. This makes it much easier and faster to zoom in/out very quickly (and also adds a natural feel to it). Do you think it&#039;s also possible to implement this in Online Maps? (Should I have opened a new topic for this?)</p>]]></content>
			<author>
				<name><![CDATA[MartinTinu]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=915</uri>
			</author>
			<updated>2019-07-30T09:30:41Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5373#p5373</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5371#p5371" />
			<content type="html"><![CDATA[<p>Updated script in post #2 to not produce a million versions of the same script.<br />lastInputPosition contains the position of a mouse or touch, and not geographical coordinates.</p>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2019-07-29T16:46:26Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5371#p5371</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5370#p5370" />
			<content type="html"><![CDATA[<p>Cool, this almost works. <img src="https://forum.infinity-code.com/img/smilies/big_smile.png" width="15" height="15" alt="big_smile" /></p><p>But it jumps when starting the zoom/drag. My guess is that it takes the last inputPosition and centers the pinch to it, which leads to the jump. Any idea how to solve this?</p><p>Thank you!</p><br /><p>EDIT: Ooops, the following code doesn&#039;t work. <br /><em>Additionally I added the following lines. Otherwise when starting not at {0,0}, it jumps to the african atlantic.</em><br /></p><div class="codebox"><pre><code>if(lastInputPosition == Vector2.zero)
            lastInputPosition = OnlineMaps.instance.position;</code></pre></div>]]></content>
			<author>
				<name><![CDATA[MartinTinu]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=915</uri>
			</author>
			<updated>2019-07-29T16:32:10Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5370#p5370</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5369#p5369" />
			<content type="html"><![CDATA[<p>Hello.</p><p>Something like:<br /></p><div class="codebox"><pre><code>using System;
using System.Reflection;
using UnityEngine;

public class DragOnSmoothZoom : MonoBehaviour
{
    private Vector2 lastInputPosition;
    private Vector2 inputPosition;
    private OnlineMapsControlBase control;
    private MethodInfo updatePosition;
    private FieldInfo lastInputField;
    private bool sendWrongTouchCount;

    private int OnGetTouchCount()
    {
        if (sendWrongTouchCount) return 1;

        if (Input.touchSupported)
        {
            if (Input.touchCount &gt; 0) return Input.touchCount;
        }
        return Input.GetMouseButton(0) ? 1 : 0;
    }

    private void OnSmoothZoomBegin()
    {
        lastInputPosition = control.GetInputPosition();
        control.OnUpdateBefore += OnUpdateBefore;

        lastInputField.SetValue(control, control.GetInputPosition());
        control.UpdateLastPosition();
    }

    private void OnSmoothZoomFinish()
    {
        control.OnUpdateBefore -= OnUpdateBefore;

        lastInputField.SetValue(control, control.GetInputPosition());
        control.UpdateLastPosition();
    }

    private void OnUpdateBefore()
    {
        if (OnGetTouchCount() &lt;= 1) return;

        inputPosition = control.GetInputPosition();
        if (lastInputPosition == inputPosition) return;

        lastInputField.SetValue(control, lastInputPosition);
        sendWrongTouchCount = true;
        updatePosition.Invoke(control, null);
        sendWrongTouchCount = false;

        lastInputPosition = inputPosition;
    }

    private void Start()
    {
        control = OnlineMapsControlBase.instance;
        control.OnSmoothZoomBegin += OnSmoothZoomBegin;
        control.OnSmoothZoomFinish += OnSmoothZoomFinish;
        control.OnGetTouchCount += OnGetTouchCount;

        Type controlType = typeof(OnlineMapsControlBase);
        updatePosition = controlType.GetMethod(&quot;UpdatePosition&quot;, BindingFlags.Instance | BindingFlags.NonPublic);
        lastInputField = controlType.GetField(&quot;lastInputPosition&quot;, BindingFlags.Instance | BindingFlags.NonPublic);
    }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Alex Vertax]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=2</uri>
			</author>
			<updated>2019-07-29T11:26:43Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5369#p5369</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Drag with two fingers]]></title>
			<link rel="alternate" href="https://forum.infinity-code.com/viewtopic.php?pid=5368#p5368" />
			<content type="html"><![CDATA[<p>Hello there!</p><p>I want to drag the map while zooming with two fingers, like Google maps does it. Is there an option for it?<br />If not, I can&#039;t find where you handle the touch input (when not connected to Fingers or EasyTouch), to add this option manually. In which script is the touch input handled? </p><p>Best,<br />Martin</p>]]></content>
			<author>
				<name><![CDATA[MartinTinu]]></name>
				<uri>https://forum.infinity-code.com/profile.php?id=915</uri>
			</author>
			<updated>2019-07-29T08:52:33Z</updated>
			<id>https://forum.infinity-code.com/viewtopic.php?pid=5368#p5368</id>
		</entry>
</feed>
