Пример #1
0
        /// <summary>
        /// Sets the Pitch and Yaw to point in the direction of the vector.
        /// </summary>
        /// <param name="vector">Point the entity will look at</param>
        /// <returns>The location with the new Pitch and Yaw</returns>
        public Location SetDirection(Vector vector)
        {
            const double double2Pi = 2 * Math.PI;
            double       x         = vector.GetX();
            double       z         = vector.GetZ();

            if (x == 0 && z == 0)
            {
                Pitch = vector.GetY() > 0 ? -90 : 90;
                return(this);
            }

            double theta = Math.Atan2(-x, z);

            Yaw = (float)MathE.ToDegrees((theta + double2Pi) % double2Pi);

            double x2 = Math.Pow(x, 2);
            double z2 = Math.Pow(z, 2);
            double xz = Math.Sqrt(x2 + z2);

            Pitch = (float)MathE.ToDegrees(Math.Atan(-vector.GetY() / xz));

            return(this);
        }