Topic: Marker rotation

Hello!
I'm developing application that requires to show user at current position by marker and marker should be able to rotate with compass. Now marker rotates by short way (for example if I rotate phone from 0 degrees to 179, in this case marker would rotate in left side, but if I rotate phone more than 180 degrees (in same direction) marker would rotate in right side. On photo I showed how I want to rotate my marker (by green dotted line) and how it rotating now (by red line))
So how I can implement it?

https://imgur.com/a/ZiCVYno

Re: Marker rotation

Hello.

Something like:

public void RotateMarkerDegree(float degreeDelta)
{
  float delta = degreeDelta / 360;
  if (delta > 0.5) delta -= 1;
  marker.rotation += delta;
}
Kind Regards,
Infinity Code Team.

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

3 (edited by lanserot 2020-03-16 00:23:54)

Re: Marker rotation

I need to assign it to

OnlineMapsLocationService.instance.OnCompassChanged

?
or I need to use it on update while rotating compass?
Also need to mention that I use 3d marker which rotates in update and which created manually, not by location service class

Re: Marker rotation

Here are some examples:
https://forum.infinity-code.com/viewtop … 6022#p6022
https://forum.infinity-code.com/viewtop … 6027#p6027

These examples use a 2D marker.
For a 3D marker, use rotationY instead of rotation.
http://infinity-code.com/doxygen/online … bae979dd9c

Kind Regards,
Infinity Code Team.

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

Re: Marker rotation

I tried this, it didn't help
Maybe issue in the way of rotation? I found out that some operations with Quaternions looking for shortest way to rotate (Like Quaternion.Slerp()). Maybe this is the reason?

Re: Marker rotation

Please explain in detail what exactly you tried, what did not help, and what you have as a result.

Kind Regards,
Infinity Code Team.

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

Re: Marker rotation

I tried to use examples from your previous post, they didn't help me
I want to rotate my marker from 0 degrees to 270 with saving rotating direction, but as a result marker rotates by 90 degrees in opposite direction. (please check image from my first post)

Re: Marker rotation

1. In the first post you mentioned 180 degrees.
Do you want to keep the rotation direction up to 180 or up to 270 degrees?
2. Do you want to have an animated rotation or just set a new rotation for the marker?

In the general case, if you just check the offset, and if it is more than some value, add or substract 360 degrees.
Something like:

using UnityEngine;

public class LerpMarker:MonoBehaviour
{
    public float compassThreshold = 8f;
    public float directionThreshold = 180; // or 270???

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

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

        if (offset > directionThreshold) offset -= 360;
        else if (offset < -directionThreshold) offset += 360;

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

        isFirst = false;
        compassValue = value * 360;
    }

    private void Start()
    {
        marker = OnlineMapsMarker3DManager.CreateItem(OnlineMaps.instance.position, null);

        OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;
    }

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

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

        if (Mathf.Abs(offset) < 1) markerRotation = compassValue;
        else
        {
            if (offset > directionThreshold) compassValue -= 360;
            else if (offset < -directionThreshold) compassValue += 360;

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

        marker.rotationY = 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.

9 (edited by lanserot 2020-03-16 18:00:57)

Re: Marker rotation

1. No, I want to keep the rotation direction up to real phone rotation direction. And if I rotate my phone clockwise from 0 degrees to more than 180 my marker keep rotating clockwise. But now if I rotate my phone on > 180 degrees clockwise marker start rotating in counterclockwise direction and vice versa (as showed on image example).
Also 270 it's just example, there can be any number that > 180.

2. Yes, I want to have an animated rotation. For this I'm using:

private void OnCompassChanged(float f)
    {
        rotation = f;
    }

and

private void RotateMarker()
    {
        if (Mathf.Abs(m_mainMarker.rotationY - rotation * 360) > compassThreshold)
        {
            float r = Mathf.SmoothDampAngle(m_mainMarker.rotationY, rotation * 360, ref velocity, smoothTime);
            m_mainMarker.rotationY = r;
            m_onlineMaps.Redraw();
        }
    }

First method subscribed to OnlineMapsLocationService.instance.OnCompassChange, second method call in Update

Re: Marker rotation

I checked your code and it works well, except for one line.

if (Mathf.Abs(m_mainMarker.rotationY - rotation * 360) > compassThreshold)
//Must be:
if (Mathf.Abs(m_mainMarker.rotationY - rotation * 360) > float.Epsilon) // or some small value
Kind Regards,
Infinity Code Team.

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

Re: Marker rotation

if (Mathf.Abs(m_mainMarker.rotationY - rotation * 360) > compassThreshold)

used as threshold for rotation.
Code works, but not as I want it. I noticed that Mathf.SmoothDampAngle use DeltaAngle(A,B) method which uses to finding shortest way from angle A to B, and this is the problem for me. I am looking for not shortest way of rotating my marker. I tried to use Mathf.SmoothDamp method for calculating angle for rotation but it working good only with angles that > 0. If rotation of my compass will > 0 it just goes messing up

Re: Marker rotation

About threshold:
In your case, this is not the threshold.
For example, you want to rotate from 0 to 90 degrees, with a threshold of 8 degrees.
Your code will stop at 82 degrees, not 90 degrees, as you expect it.

About the compass:
I reread all your posts in the thread, and I think there is a misunderstanding.
Heading of the compass is just a value from 0 to 360.
The device’s sensor does not return the direction of change (clockwise or counterclockwise).
The device’s sensor has an update period of 0.02 - 0.2 seconds (usually 0.06).
If you can rotate the device more than 180 degrees in 60 ms, my congratulations - you are flash.

About animation:
By quickly rotating the device, you may not have time to animate the rotation.
In other words, while the animation is in progress, you can get the difference between rotation of the marker and the compass value of more than 180 degrees, and the animation will go in the opposite direction.
It is very easy to prevent this - if the difference between the previous and current compass value is more than 180 degrees, add or subtract 360 degrees to normalize the transition through 0.
This will give you values of -90 degrees or 457 degrees, which is outside the range of 0-360, but it is absolutely does not matter for the rotation of the marker.
And use SmoothDamp or Lerp for the animation, instead of SmoothDampAngle.

Kind Regards,
Infinity Code Team.

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

13 (edited by lanserot 2020-03-17 22:47:39)

Re: Marker rotation

It's not necessary to be flash to get bad rotation effect. I guess sometimes marker still not rotated to desired point when I start rotating to next point. And at this moment marker looking for short distance to rotate, which is not what I'm looking for. It was when I used SmoothDampAngle or Lerp methods.
Now I'm using just SmoothDamp and it rotating just like I want it, but only when compass value is positive. When compass value start to be negative marker rotation going crazy, take a look:
https://www.dropbox.com/s/fnx6et0ys2wx8 … r.mp4?dl=0

Re: Marker rotation

You will never get -9 degrees on the device.
It will be 351 degrees.

I fixed this, and in the next version OnCompassChanged will get values from 0 to 1 when using the emulator.

Kind Regards,
Infinity Code Team.

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