Beispiel #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);
        }
Beispiel #2
0
        public static Vec3 operator *(Quats rotation, Vec3 point)
        {
            float sinX   = 2 * (rotation.w * rotation.x + rotation.y * rotation.z);
            float cosX   = 1 - 2 * (rotation.x * rotation.x + rotation.y * rotation.y);
            float eulerX = Mathf.Atan2(sinX, cosX) * Mathf.PI / 180.0f;
            float sinY   = 2 * (rotation.w * rotation.y - rotation.z * rotation.x);
            float eulerY;

            if (Mathf.Abs(sinY) >= 1)
            {
                eulerY = Mathf.PI / 2;
                if ((eulerY < 0 && sinY > 0) || (eulerY > 0 && sinY < 0))
                {
                    eulerY = -eulerY;
                }
            }
            else
            {
                eulerY = Mathf.Asin(sinY) * Mathf.PI / 180.0f;
            }
            float sinZ   = 2 * (rotation.w * rotation.z + rotation.x * rotation.y);
            float cosZ   = 1 - 2 * (rotation.y * rotation.y + rotation.z * rotation.z);
            float eulerZ = Mathf.Atan2(sinZ, cosZ) * Mathf.PI / 180.0f;
            Vec3  aux    = new Vec3(eulerX, eulerY, eulerZ);
            Vec3  result = Vec3.Cross(aux, point);

            return(result);
        }
Beispiel #3
0
        //establecemos un plano utilizando tres puntos que se encuentren dentro de el
        //para ello necesitamos setear la normal y distance del plano
        //como son tres puntos creamos dos vectores que representen los dos lados del plano
        //luego calculamos el producto cruz de ambos vectores y normalizamos el resultado
        //calculamos la distance invirtiendo el resultado de dot entre la normal y el primer punto recibido
        public void Set3Points(Vec3 a, Vec3 b, Vec3 c)
        {
            Vec3 side1 = b - a;
            Vec3 side2 = c - a;

            normal   = Vec3.Cross(side1, side2).normalized;
            distance = -Vec3.Dot(normal, a);
        }
Beispiel #4
0
        public PlaneA(Vec3 a, Vec3 b, Vec3 c)
        {
            Vec3 sideA = b - a;
            Vec3 sideB = c - a;

            normal   = Vec3.Cross(sideA, sideB).normalized;
            distance = -Vec3.Dot(normal, a);
        }
Beispiel #5
0
        public MyPlane(Vec3 a, Vec3 b, Vec3 c)
        {
            Vec3 side1 = b - a;
            Vec3 side2 = c - a;

            normal   = Vec3.Cross(side1, side2).normalized;
            distance = -Vec3.Dot(normal, a);
        }
Beispiel #6
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;
        }
Beispiel #7
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);
        }
Beispiel #8
0
        //
        // Summary:
        //     Creates a rotation which rotates from fromDirection to toDirection.
        //
        // Parameters:
        //   fromDirection:
        //
        //   toDirection:
        public static QuaternionCustom FromToRotation(Vec3 fromDirection, Vec3 toDirection)
        {
            Vec3             cross  = Vec3.Cross(fromDirection, toDirection);
            QuaternionCustom result = QuaternionCustom.identity;

            result.x = cross.x;
            result.y = cross.y;
            result.z = cross.z;
            result.w = Mathf.Sqrt(fromDirection.magnitude * fromDirection.magnitude) * Mathf.Sqrt(toDirection.magnitude * toDirection.magnitude) + Vec3.Dot(fromDirection, toDirection);
            result.Normalize();
            return(result);
        }
        //
        // Resumen:
        //     Creates a rotation which rotates from fromDirection to toDirection.
        //
        // Parámetros:
        //   fromDirection:
        //
        //   toDirection:
        public static Quarentenion FromToRotation(Vec3 fromDirection, Vec3 toDirection)
        {
            Vec3         cross = Vec3.Cross(fromDirection, toDirection);
            Quarentenion q;

            q.x = cross.x;
            q.y = cross.y;
            q.z = cross.z;
            q.w = fromDirection.magnitude * toDirection.magnitude + Vec3.Dot(fromDirection, toDirection);
            q.Normalize();
            return(q);
        }
        public static MyQuaternion FromToRotation(Vec3 from, Vec3 to)
        {
            Vec3         cross  = Vec3.Cross(from, to);
            MyQuaternion result = MyQuaternion.identity;

            result.x = cross.x;
            result.y = cross.y;
            result.z = cross.z;
            result.w = Mathf.Sqrt(from.magnitude * to.magnitude) * Mathf.Sqrt(from.magnitude * to.magnitude) + Vec3.Dot(from, to);
            result.Normalize();
            return(result);
        }
Beispiel #11
0
        public Enalp(Vec3 a, Vec3 b, Vec3 c)
        {
            this.a = a;
            this.b = b;
            this.c = c;

            Vec3 side1 = b - a;
            Vec3 side2 = c - a;

            normal   = Vec3.Cross(side1, side2);
            distance = Vec3.Dot(-a, normal) / Vec3.Magnitude(normal);
            flipped  = -normal;
        }
Beispiel #12
0
        //Convierto los Vector3 a vec3, asi luego seteo la normal y el distance del plano
        public PlaneA(Vector3 a, Vector3 b, Vector3 c)
        {
            Vec3 myVec3a;
            Vec3 myVec3b;
            Vec3 myVec3c;

            myVec3a.x = a.x;
            myVec3a.y = a.y;
            myVec3a.z = a.z;
            myVec3b.x = b.x;
            myVec3b.y = b.y;
            myVec3b.z = b.z;
            myVec3c.x = c.x;
            myVec3c.y = c.y;
            myVec3c.z = c.z;
            Vec3 sideA = myVec3b - myVec3a;
            Vec3 sideB = myVec3c - myVec3a;

            normal   = Vec3.Cross(sideA, sideB).normalized;
            distance = -Vec3.Dot(normal, myVec3a);
        }
Beispiel #13
0
        public void Set3Points(Vector3 a, Vector3 b, Vector3 c)
        {
            Vec3 myVec3a;
            Vec3 myVec3b;
            Vec3 myVec3c;

            myVec3a.x = a.x;
            myVec3a.y = a.y;
            myVec3a.z = a.z;
            myVec3b.x = b.x;
            myVec3b.y = b.y;
            myVec3b.z = b.z;
            myVec3c.x = c.x;
            myVec3c.y = c.y;
            myVec3c.z = c.z;
            Vec3 side1 = myVec3b - myVec3a;
            Vec3 side2 = myVec3c - myVec3a;

            normal   = Vec3.Cross(side1, side2).normalized;
            distance = -Vec3.Dot(normal, myVec3a);
        }