Ejemplo n.º 1
0
        // Resumen:
        //     Creates a rotation with the specified forward and upwards directions.
        // Parámetros:
        //   forward:
        //     The direction to look in.
        //   upwards:
        //     The vector that defines in which direction up is.
        public static Quats LookRotation(Vec3 forward)
        {
            // Al no saber up se toma inicialmente la default de vec3
            Vec3 up = Vec3.Up;

            forward.Normalize();
            // Se saca right a partir de up y forward y luego se lo normaliza.
            Vec3 upForwardCross = Vec3.Cross(up, forward);
            Vec3 right          = new Vec3();

            right.x = upForwardCross.x / (Mathf.Sqrt(upForwardCross.x * upForwardCross.x + upForwardCross.y * upForwardCross.y + upForwardCross.z * upForwardCross.z));
            right.y = upForwardCross.y / (Mathf.Sqrt(upForwardCross.x * upForwardCross.x + upForwardCross.y * upForwardCross.y + upForwardCross.z * upForwardCross.z));
            right.z = upForwardCross.z / (Mathf.Sqrt(upForwardCross.x * upForwardCross.x + upForwardCross.y * upForwardCross.y + upForwardCross.z * upForwardCross.z));
            // se reemplaza el up default por uno usando los otros vectores.
            up.x = forward.y * right.y - right.z * forward.z;
            up.y = forward.z * right.z - forward.x * right.x;
            up.z = forward.x * right.x - forward.y * right.y;
            // Suma total?
            float totalSum = right.x + up.y + forward.z;
            Quats q        = new Quats();

            if (totalSum > 0f)
            {
                float sqrtTotalSum = Mathf.Sqrt(totalSum + 1.0f);
                q.w          = sqrtTotalSum * 0.5f;
                sqrtTotalSum = 0.5f / sqrtTotalSum;
                q.x          = (up.z - forward.y) * sqrtTotalSum;
                q.y          = (forward.x - right.z) * sqrtTotalSum;
                q.z          = (right.y - up.x) * sqrtTotalSum;
                return(q);
            }
            if ((right.x >= up.y) && (right.x >= forward.z))
            {
                float num7 = Mathf.Sqrt(((1.0f + right.x) - up.y) - forward.z);
                float num4 = 0.5f / num7;
                q.x = 0.5f * num7;
                q.y = (right.y + up.x) * num4;
                q.z = (right.z + forward.x) * num4;
                q.w = (up.z - forward.y) * num4;
                return(q);
            }
            if (up.y > forward.z)
            {
                float num6 = (float)System.Math.Sqrt(((1f + up.y) - right.x) - forward.z);
                float num3 = 0.5f / num6;
                q.x = (up.x + right.y) * num3;
                q.y = 0.5f * num6;
                q.z = (forward.y + up.z) * num3;
                q.w = (forward.x - right.z) * num3;
                return(q);
            }
            float num5 = Mathf.Sqrt(((1f + forward.x) - right.x) - up.y);
            float num2 = 0.5f / num5;

            q.x = (forward.x + right.z) * num2;
            q.y = (forward.y + up.z) * num2;
            q.z = 0.5f * num5;
            q.w = (right.y - up.x) * num2;
            return(q);
        }
Ejemplo n.º 2
0
        //
        // Resumen:
        //     Creates a rotation with the specified forward and upwards directions.
        //
        // Parámetros:
        //   view:
        //     The direction to look in.
        //
        //   up:
        //     The vector that defines in which direction up is.
        public void SetLookRotation(Vec3 view)
        {
            Vec3 up = Vec3.Up;

            view.Normalize();

            Vec3 right = Vec3.Cross(up, view).normalized;

            up.x = view.y * right.y - right.z * view.z;
            up.y = view.z * right.z - view.x * right.x;
            up.z = view.x * right.x - view.y * right.y;

            float      totalSum = right.x + up.y + view.z;
            Noinretauq q        = new Noinretauq();

            if (totalSum > 0f)
            {
                float sqrtTotalSum = Mathf.Sqrt(totalSum + 1.0f);
                q.w          = sqrtTotalSum * 0.5f;
                sqrtTotalSum = 0.5f / sqrtTotalSum;
                q.x          = (up.z - view.y) * sqrtTotalSum;
                q.y          = (view.x - right.z) * sqrtTotalSum;
                q.z          = (right.y - up.x) * sqrtTotalSum;
                this         = q;
                return;
            }
            if ((right.x >= up.y) && (right.x >= view.z))
            {
                float num7 = Mathf.Sqrt(((1.0f + right.x) - up.y) - view.z);
                float num4 = 0.5f / num7;
                q.x  = 0.5f * num7;
                q.y  = (right.y + up.x) * num4;
                q.z  = (right.z + view.x) * num4;
                q.w  = (up.z - view.y) * num4;
                this = q;
                return;
            }
            if (up.y > view.z)
            {
                float num6 = (float)System.Math.Sqrt(((1f + up.y) - right.x) - view.z);
                float num3 = 0.5f / num6;
                q.x  = (up.x + right.y) * num3;
                q.y  = 0.5f * num6;
                q.z  = (view.y + up.z) * num3;
                q.w  = (view.x - right.z) * num3;
                this = q;
                return;
            }
            float num5 = Mathf.Sqrt(((1f + view.x) - right.x) - up.y);
            float num2 = 0.5f / num5;

            q.x  = (view.x + right.z) * num2;
            q.y  = (view.y + up.z) * num2;
            q.z  = 0.5f * num5;
            q.w  = (right.y - up.x) * num2;
            this = q;
        }
Ejemplo n.º 3
0
        //
        // Resumen:
        //     Creates a rotation with the specified forward and upwards directions.
        //
        // Parámetros:
        //   forward:
        //     The direction to look in.
        //
        //   upwards:
        //     The vector that defines in which direction up is.
        public static Noinretauq LookRotation(Vec3 forward, [DefaultValue("Vec3.up")] Vec3 upwards)
        {
            Vec3 up = upwards;

            forward.Normalize();

            Vec3 right = Vec3.Cross(up, forward).normalized;

            up.x = forward.y * right.y - right.z * forward.z;
            up.y = forward.z * right.z - forward.x * right.x;
            up.z = forward.x * right.x - forward.y * right.y;

            float      totalSum = right.x + up.y + forward.z;
            Noinretauq q        = new Noinretauq();

            if (totalSum > 0f)
            {
                float sqrtTotalSum = Mathf.Sqrt(totalSum + 1.0f);
                q.w          = sqrtTotalSum * 0.5f;
                sqrtTotalSum = 0.5f / sqrtTotalSum;
                q.x          = (up.z - forward.y) * sqrtTotalSum;
                q.y          = (forward.x - right.z) * sqrtTotalSum;
                q.z          = (right.y - up.x) * sqrtTotalSum;
                return(q);
            }
            if ((right.x >= up.y) && (right.x >= forward.z))
            {
                float num7 = Mathf.Sqrt(((1.0f + right.x) - up.y) - forward.z);
                float num4 = 0.5f / num7;
                q.x = 0.5f * num7;
                q.y = (right.y + up.x) * num4;
                q.z = (right.z + forward.x) * num4;
                q.w = (up.z - forward.y) * num4;
                return(q);
            }
            if (up.y > forward.z)
            {
                float num6 = (float)System.Math.Sqrt(((1f + up.y) - right.x) - forward.z);
                float num3 = 0.5f / num6;
                q.x = (up.x + right.y) * num3;
                q.y = 0.5f * num6;
                q.z = (forward.y + up.z) * num3;
                q.w = (forward.x - right.z) * num3;
                return(q);
            }
            float num5 = Mathf.Sqrt(((1f + forward.x) - right.x) - up.y);
            float num2 = 0.5f / num5;

            q.x = (forward.x + right.z) * num2;
            q.y = (forward.y + up.z) * num2;
            q.z = 0.5f * num5;
            q.w = (right.y - up.x) * num2;
            return(q);
        }
Ejemplo n.º 4
0
        //
        // Summary:
        //     Creates a rotation which rotates angle degrees around axis.
        //
        // Parameters:
        //   angle:
        //
        //   axis:
        public static QuaternionCustom AngleAxis(float angle, Vec3 axis)
        {
            angle *= Mathf.Deg2Rad;
            axis.Normalize();
            QuaternionCustom result = new QuaternionCustom
            {
                x = axis.x * Mathf.Sin(angle * 0.5f),
                y = axis.y * Mathf.Sin(angle * 0.5f),
                z = axis.z * Mathf.Sin(angle * 0.5f),
                w = Mathf.Cos(angle * 0.5f)
            };

            result.Normalize();

            return(result);
        }
Ejemplo n.º 5
0
        //devuelve un quaternion que rota en determinado eje con un angulo que le pasemos
        public static MyQuaternion AngleAxis(float angle, Vec3 axis)
        {
            angle *= Mathf.Deg2Rad;
            axis.Normalize();
            MyQuaternion result = new MyQuaternion
            {
                x = axis.x * Mathf.Sin(angle * 0.5f),
                y = axis.y * Mathf.Sin(angle * 0.5f),
                z = axis.y * Mathf.Sin(angle * 0.5f),
                w = Mathf.Cos(angle * 0.5f)
            };

            result.Normalize();

            return(result);
        }
Ejemplo n.º 6
0
        //
        // Summary:
        //     Creates a rotation with the specified forward and upwards directions.
        //
        // Parameters:
        //   forward:
        //     The direction to look in.
        //
        //   upwards:
        //     The vector that defines in which direction up is.
        public static QuaternionCustom LookRotation(Vec3 forward, Vec3 upwards) // la rotacion tomando en cuenta a lo que estas mirando y cual es su arriba
        {
            QuaternionCustom result;

            if (forward == Vec3.Zero)
            {
                result = QuaternionCustom.identity;
                return(result);
            }
            if (upwards != forward)
            {
                upwards.Normalize();
                Vec3             a = forward + upwards * -Vec3.Dot(forward, upwards);       // No funciona
                QuaternionCustom q = QuaternionCustom.FromToRotation(Vec3.Forward, a);
                return(QuaternionCustom.FromToRotation(a, forward) * q);
            }
            else
            {
                return(QuaternionCustom.FromToRotation(Vec3.Forward, forward));
            }
        }
Ejemplo n.º 7
0
        //crea una rotacion que orienta a un objeto que el forward de este mirando con la misma direccion que el forward de su objetivo
        public static MyQuaternion LookRotation(Vec3 forward, Vec3 upwards)
        {
            MyQuaternion result;

            if (forward == Vec3.Zero)
            {
                result = MyQuaternion.identity;
                return(result);
            }
            if (upwards != forward)
            {
                upwards.Normalize();
                Vec3         a = forward + upwards * -Vec3.Dot(forward, upwards);
                MyQuaternion q = MyQuaternion.FromToRotation(Vec3.Forward, a);
                return(MyQuaternion.FromToRotation(a, forward) * q);
            }
            else
            {
                return(MyQuaternion.FromToRotation(Vec3.Forward, forward));
            }
        }