Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change quad map camera panning to snap cursor location better #1684

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 58 additions & 40 deletions sdkproject/Assets/Mapbox/Examples/Scripts/QuadTreeCameraMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@

public class QuadTreeCameraMovement : MonoBehaviour
{
[SerializeField]
[Range(1, 20)]
public float _panSpeed = 1.0f;
[SerializeField] [Range(1, 20)] public float _panSpeed = 1.0f;

[SerializeField]
float _zoomSpeed = 0.25f;
[SerializeField] float _zoomSpeed = 0.25f;

[SerializeField]
public Camera _referenceCamera;
[SerializeField] public Camera _referenceCamera;

[SerializeField]
AbstractMap _mapManager;
[SerializeField] AbstractMap _mapManager;

[SerializeField]
bool _useDegreeMethod;
[SerializeField] bool _useDegreeMethod;

private Vector3 _origin;
private Vector3 _mousePosition;
Expand All @@ -38,12 +32,13 @@ void Awake()
if (null == _referenceCamera)
{
_referenceCamera = GetComponent<Camera>();
if (null == _referenceCamera) { Debug.LogErrorFormat("{0}: reference camera not set", this.GetType().Name); }
if (null == _referenceCamera)
{
Debug.LogErrorFormat("{0}: reference camera not set", this.GetType().Name);
}
}
_mapManager.OnInitialized += () =>
{
_isInitialized = true;
};

_mapManager.OnInitialized += () => { _isInitialized = true; };
}

public void Update()
Expand All @@ -62,7 +57,10 @@ public void Update()

private void LateUpdate()
{
if (!_isInitialized) { return; }
if (!_isInitialized)
{
return;
}

if (!_dragStartedOnUI)
{
Expand Down Expand Up @@ -103,27 +101,27 @@ void HandleTouch()
switch (Input.touchCount)
{
case 1:
{
PanMapUsingTouchOrMouse();
}
{
PanMapUsingTouchOrMouse();
}
break;
case 2:
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);

// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

// Find the difference in the distances between each frame.
zoomFactor = 0.01f * (touchDeltaMag - prevTouchDeltaMag);
}
// Find the difference in the distances between each frame.
zoomFactor = 0.01f * (touchDeltaMag - prevTouchDeltaMag);
}
ZoomMapUsingTouchOrMouse(zoomFactor);
break;
default:
Expand All @@ -136,7 +134,19 @@ void ZoomMapUsingTouchOrMouse(float zoomFactor)
var zoom = Mathf.Max(0.0f, Mathf.Min(_mapManager.Zoom + zoomFactor * _zoomSpeed, 21.0f));
if (Math.Abs(zoom - _mapManager.Zoom) > 0.0f)
{

var mousePosScreen = Input.mousePosition;
mousePosScreen.z = _referenceCamera.transform.localPosition.y;
_mousePosition = _referenceCamera.ScreenToWorldPoint(mousePosScreen);
var geo = _mapManager.WorldToGeoPosition(_mousePosition);
var pos1 = Conversions.LatLonToMeters(geo);

_mapManager.UpdateMap(_mapManager.CenterLatitudeLongitude, zoom);
geo = _mapManager.WorldToGeoPosition(_mousePosition);

var pos2 = Conversions.LatLonToMeters(geo);
var delta = pos2 - pos1;
_mapManager.UpdateMap(Conversions.MetersToLatLon(_mapManager.CenterMercator - new Vector2d(delta.x, delta.y)));
}
}

Expand All @@ -148,7 +158,7 @@ void PanMapUsingKeyBoard(float xMove, float zMove)
// Divide it by the tile width in pixels ( 256 in our case)
// to get degrees represented by each pixel.
// Keyboard offset is in pixels, therefore multiply the factor with the offset to move the center.
float factor = _panSpeed * (Conversions.GetTileScaleInDegrees((float)_mapManager.CenterLatitudeLongitude.x, _mapManager.AbsoluteZoom));
float factor = _panSpeed * (Conversions.GetTileScaleInDegrees((float) _mapManager.CenterLatitudeLongitude.x, _mapManager.AbsoluteZoom));

var latitudeLongitude = new Vector2d(_mapManager.CenterLatitudeLongitude.x + zMove * factor * 2.0f, _mapManager.CenterLatitudeLongitude.y + xMove * factor * 4.0f);

Expand Down Expand Up @@ -213,13 +223,14 @@ void UseMeterConversion()
{
if (null != _mapManager)
{
float factor = _panSpeed * Conversions.GetTileScaleInMeters((float)0, _mapManager.AbsoluteZoom) / _mapManager.UnityTileSize;
var latlongDelta = Conversions.MetersToLatLon(new Vector2d(offset.x * factor, offset.z * factor));
var newLatLong = _mapManager.CenterLatitudeLongitude + latlongDelta;

var offset2D = new Vector2d(offset.x, offset.z);
var gameobjectScalingMultiplier = _mapManager.transform.localScale.x * (Mathf.Pow(2, (_mapManager.InitialZoom - _mapManager.AbsoluteZoom)));
var newLatLong = Conversions.MetersToLatLon(
Conversions.LatLonToMeters(_mapManager.CenterLatitudeLongitude) + (offset2D / _mapManager.WorldRelativeScale) / gameobjectScalingMultiplier);
_mapManager.UpdateMap(newLatLong, _mapManager.Zoom);
}
}

_origin = _mousePosition;
}
else
Expand All @@ -228,6 +239,7 @@ void UseMeterConversion()
{
return;
}

_mousePositionPrevious = _mousePosition;
_origin = _mousePosition;
}
Expand Down Expand Up @@ -271,12 +283,13 @@ void UseDegreeConversion()
// Divide it by the tile width in pixels ( 256 in our case)
// to get degrees represented by each pixel.
// Mouse offset is in pixels, therefore multiply the factor with the offset to move the center.
float factor = _panSpeed * Conversions.GetTileScaleInDegrees((float)_mapManager.CenterLatitudeLongitude.x, _mapManager.AbsoluteZoom) / _mapManager.UnityTileSize;
float factor = _panSpeed * Conversions.GetTileScaleInDegrees((float) _mapManager.CenterLatitudeLongitude.x, _mapManager.AbsoluteZoom) / _mapManager.UnityTileSize;

var latitudeLongitude = new Vector2d(_mapManager.CenterLatitudeLongitude.x + offset.z * factor, _mapManager.CenterLatitudeLongitude.y + offset.x * factor);
_mapManager.UpdateMap(latitudeLongitude, _mapManager.Zoom);
}
}

_origin = _mousePosition;
}
else
Expand All @@ -285,6 +298,7 @@ void UseDegreeConversion()
{
return;
}

_mousePositionPrevious = _mousePosition;
_origin = _mousePosition;
}
Expand All @@ -294,7 +308,11 @@ void UseDegreeConversion()
private Vector3 getGroundPlaneHitPoint(Ray ray)
{
float distance;
if (!_groundPlane.Raycast(ray, out distance)) { return Vector3.zero; }
if (!_groundPlane.Raycast(ray, out distance))
{
return Vector3.zero;
}

return ray.GetPoint(distance);
}
}
Expand Down