Jump to content

How to get angle or rotation info?


Recommended Posts

Hello, this should work:

        private static double ActualRotation(double value, double min, double max)
        {
            if (value < min)
                return min;
            return value > max ? max : value;
        }

        public static double CalculateFacingCenter(Vector3 source, float playerRotation, Vector3 destination)
        {
            Vector3 v2 = destination - source;
            v2.Z = 0.0f;
            Vector3 v1 = new Vector3((float) Math.Cos(playerRotation), (float) Math.Sin(playerRotation), 0.0f);
            return Math.Acos(ActualRotation(Vector3.Dot(ref v1, ref v2) / (v2.Magnitude() * (double) v1.Magnitude()), -1.0, 1.0));
        }

And additional snippets which could help you:

    /// <summary>
    /// Used to calculate new position by parameter.
    /// </summary>
    /// <param name="from">The position to calculate from.</param>
    /// <param name="rotation">The rotation of the object in radians.</param>
    /// <param name="radius">The radius to add.</param>
    /// <returns></returns>
    public Vector3 CalculatePosition(Vector3 from, float rotation, float radius)
    {
        return new Vector3(System.Math.Sin(rotation) * radius + from.X, System.Math.Cos(rotation) * radius + from.Y, from.Z);
    }

    /// <summary>
    /// Used to calculate atan2 of to positions.
    /// </summary>
    /// <param name="from">Position 1.</param>
    /// <param name="to">Position 2.</param>
    /// <param name="addRadian">Radians to add.</param>
    /// <returns></returns>
    public float Atan2Rotation(Vector3 from, Vector3 to, float addRadian = 0)
    {
        return (float) System.Math.Atan2(to.Y - from.Y, to.X - from.X) + addRadian;
    }

    /// <summary>
    /// Used to face to the desired position.
    /// </summary>
    /// <param name="to">The position to face.</param>
    /// <param name="addRadian">Radians to add from the position.</param>
    public static void Face(Vector3 to, float addRadian = 0)
    {
        wManager.Wow.Helpers.ClickToMove.CGPlayer_C__ClickToMove(0, 0, 0, wManager.Wow.ObjectManager.ObjectManager.Me.Guid, 2, (float)System.Math.Atan2(to.Y - wManager.Wow.ObjectManager.ObjectManager.Me.Position.Y, to.X - wManager.Wow.ObjectManager.ObjectManager.Me.Position.X)+addRadian);
    }

Usage for second block(i guess first one should be understandable)

        //turn 180°
        Face(ObjectManager.Target.Position, (float)System.Math.PI);

        //turn left
        Face(ObjectManager.Target.Position, 1.53589f);//88° = 1.53589 rad

        //turn right
        Face(ObjectManager.Target.Position, -1.53589f);//88° = 1.53589 rad

 

Link to comment
Share on other sites

Shiet, I'm so weak in math, can you help me please adapt it to get an angle value between -180 to 180 depend on where target is. (-90 left, 0 facing, 90 right)

Link to comment
Share on other sites

This should finally do the desired work while the other method was just for calculating :)

    /// <summary>
    /// Used to get the facing from two positions based on the origin rotation.
    /// </summary>
    /// <param name="from">The 1. position.</param>
    /// <param name="to">The 2. position.</param>
    /// <param name="originRotation">The origin rotation.</param>
    /// <returns>Negative radian on the right; positive on the left.</returns>
    public float FacingCenter(Vector3 from, Vector3 to, float originRotation)
    {
        var face = NormalizeRadian(Atan2Rotation(from, to) - originRotation);
        if (face < System.Math.PI)
            return -face;
        return NormalizeRadian(originRotation-Atan2Rotation(from, to));
    }

    /// <summary>
    /// Used to normalize the input radian.
    /// </summary>
    /// <param name="radian">The radians to normalize.</param>
    public float NormalizeRadian(float radian)
    {
        if (radian < 0.0)
            return (float) (-(- radian % (2.0 * System.Math.PI)) + 2.0 * System.Math.PI);
        return radian % 6.283185f;
    }
    
    /// <summary>
    /// Used to calculate atan2 of to positions.
    /// </summary>
    /// <param name="from">The 1. position.</param>
    /// <param name="to">The 2. position.</param>
    /// <param name="addRadian">Radians to add.</param>
    /// <returns></returns>
    public float Atan2Rotation(Vector3 from, Vector3 to, float addRadian = 0)
    {
        return (float) System.Math.Atan2(to.Y - from.Y, to.X - from.X) + addRadian;
    }

 

Usage:

                    var rot = FacingCenter(ObjectManager.Me.Position, ObjectManager.Target.Position, ObjectManager.Me.Rotation);
                    if (rot < 0)
                        Logging.Write("Facing to right: "+rot);
                    Logging.Write("Facing to left: "+rot);

 

Link to comment
Share on other sites

I assume you are using the "Rotation" property from your playerclass to change the rotation, but this should be only used to get rotation values.

This behavior occures because you are only changing your client-side rotation but not your actual rotation on the server.

It can be fixed either using the mentioned "Face();" method in my previous posts (dunno if it works on vanilla)

or by a nudge turn via keypress / lua (http://wowwiki.wikia.com/wiki/API_TurnLeftStart & http://wowwiki.wikia.com/wiki/API_TurnLeftStop)

Link to comment
Share on other sites

16 minutes ago, sowelu said:

Dont work in my code, so , is there  no way to inplement swift rotation from code ? Key rotation  too slow. 

I forgot to illustrate the other method more detailed. You can actually write your radians to rotation property at the player class (like you did), but after the write it is needed to update the rotation via a nudge:

            ObjectManager.Me.Rotation = 1.1f;
            //or use robotmanager's keyboard class
            Lua.LuaDoString("TurnLeftStart();");
            Lua.LuaDoString("TurnLeftStop();");

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...