Jump to content
This site uses cookies! Learn More

This site uses cookies!

By continuing to use this site, you agree to allow us to store cookies on your computer. :)

TheSmokie

Help with some trigonometry,

Recommended Posts

Hello, 

i am trying to figure out how to get a facing target from the vector3, the angle, and targets vector3.

We have the following data:
- Our Vector3 Position (X, Y, Z) (100, 100, 0)
- Our Current Facing Angle (Pitch, Yaw) (90, 180)
- Targets Vector3 Position (X, Y, Z) (200, 200, 0)

How do we calculate the Angle (Pitch, Yaw) that I would need to reach to be facing our target.

I just need the math function to take the input and output the info i need. the rest i can do.

-Smokie

 

 

Share this post


Link to post
Share on other sites

I believe this should work:

        /// <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(WoWPoint from, WoWPoint to, float originRotation)
        {
            var face = NormalizeRadian(Atan2Rotation(from, to) - originRotation);
            if (face < 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 * Math.PI)) + 2.0 * Math.PI);
            return radian % 6.283185f;
        }
    
        /// <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 static float Atan2Rotation(WoWPoint from, WoWPoint to, float addRadian = 0)
        {
            return (float) Math.Atan2(to.Y - from.Y, to.X - from.X) + addRadian;
        }

        /// <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 static WoWPoint CalculateNewPosition(WoWPoint from, float rotation, float radius)
        {
            return new WoWPoint(Math.Sin(rotation) * radius + from.X, Math.Cos(rotation) * radius + from.Y, from.Z);
        }

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...