예제 #1
0
        public override void Update()
        {
            PreUpdate();

            //Actualizar valores al sprite interno.
            sprite.Position = new TGCVector2(0f, 0f);
            sprite.Scaling  = new TGCVector2(0.5f, 0.5f);
            sprite.Rotation = 0f;
            //Internamente realiza TGCMatrix.Transformation2D(scalingCenter, 0, scaling, rotationCenter, rotation, position);

            /*El método Transformation2D calcula la matriz de transformación afín por medio de la fórmula siguiente, evaluando la concatenación de la matriz de izquierda a derecha.
             * M salida = (M ce )-1 * (M re )-1 * M s* M re* M ce * (M cr )-1 * M r* M cr* M t
             * donde:
             * M salida = matriz de transformación de salida (el valor devuelto)
             * M ce = matriz de centro de escala (scalingCenter)
             * M re = matriz de rotación de escala(scalingRotation)
             * M e = matriz de escala(scaling)
             * M cr = matriz de centro de rotación(rotationCenter)
             * M r = matriz de rotación(rotation)
             * M t = matriz de traslación(translation)
             */

            //Sacar toda transformación.
            matrixIdentity = TGCMatrix.Identity;

            //Traslación al centro de la pantalla.
            traslation = TGCMatrix.Translation(centerScreen.X, centerScreen.Y, 0f);

            //Un escalado, siempre olvidar la coordenada Z.
            scaling = TGCMatrix.Scaling(sprite.Scaling.X, sprite.Scaling.Y, 1f);

            //Las rotaciones en 2D serán siempre rotaciones con eje Z.
            rotation = TGCMatrix.RotationZ(FastMath.ToRad(45));

            //Rotación animada (dependiente de frames)
            rotationAnimateFrameDepend = rotationAnimateFrameDepend * TGCMatrix.RotationZ(FastMath.ToRad(25));

            //Una rotación animada cada 1 segundo.
            if (acumlatedTime > 1f)
            {
                acumlatedTime         = 0;
                rotationAnimateOneSec = rotationAnimateOneSec * TGCMatrix.RotationZ(FastMath.ToRad(25)); //roto 25 grados por segundo
            }
            acumlatedTime += ElapsedTime;

            //rotación animada por render (Sin acoplar),
            //Si ElapsedTime es muy chico (como suele pasar con buenas computadoras y poco procesamiento)
            //Al multiplicarlo por nuestra velocidad angular, se transformaran en "pequeñas rotaciones".
            rotationAnimate = rotationAnimate * TGCMatrix.RotationZ(FastMath.ToRad(25) * ElapsedTime);

            PostUpdate();
        }
예제 #2
0
        public override void Update()
        {
            PreUpdate();

            // Los movimientos de teclado no validan que la mesh se atraviecen, solo modifican el angulo o traslacion.
            if (Input.keyDown(Key.W))
            {
                ang += VELOCIDAD_ANGULAR * ElapsedTime;
            }
            else if (Input.keyDown(Key.S))
            {
                ang -= VELOCIDAD_ANGULAR * ElapsedTime;
            }
            if (Input.keyDown(Key.A))
            {
                pos += 5f * ElapsedTime;
            }
            else if (Input.keyDown(Key.D))
            {
                pos -= 5f * ElapsedTime;
            }
            angHelice += ElapsedTime * VELOCIDAD_ANGULAR;

            escalaAvion = TGCMatrix.Scaling(lengthAvion);

            escalaHelice = TGCMatrix.Scaling(lengthHelice);

            var T1 = TGCMatrix.Translation(pos, pos, 0);
            var R1 = TGCMatrix.RotationZ(ang);

            transformacionAvion = R1 * T1;

            var T2 = TGCMatrix.Translation(lengthAvion.X / 2 + lengthHelice.X / 2, 0, 0);

            var R3 = TGCMatrix.RotationX(angHelice);

            transformacionHelice = R3 * T2 * transformacionAvion;

            PostUpdate();
        }
예제 #3
0
        public override void Update()
        {
            PreUpdate();

            // Los movimientos de teclado no validan que la mesh se atraviecen, solo modifican el angulo o traslacion.
            if (Input.keyDown(Key.A))
            {
                pinzaDang += VELOCIDAD_ANGULAR * ElapsedTime;
            }
            else if (Input.keyDown(Key.S))
            {
                pinzaDang -= VELOCIDAD_ANGULAR * ElapsedTime;
            }

            if (Input.keyDown(Key.Q))
            {
                pinzaTraslacion += VELOCIDAD_ANGULAR * ElapsedTime;
            }
            else if (Input.keyDown(Key.W))
            {
                pinzaTraslacion -= VELOCIDAD_ANGULAR * ElapsedTime;
            }

            if (Input.keyDown(Key.Left))
            {
                antebrazoDAng += VELOCIDAD_ANGULAR * ElapsedTime;
            }
            else if (Input.keyDown(Key.Right))
            {
                antebrazoDAng -= VELOCIDAD_ANGULAR * ElapsedTime;
            }

            if (Input.keyDown(Key.Up))
            {
                brazoDAng += VELOCIDAD_ANGULAR * ElapsedTime;
            }
            else if (Input.keyDown(Key.Down))
            {
                brazoDAng -= VELOCIDAD_ANGULAR * ElapsedTime;
            }

            // 1- Base del brazo
            // ajusto a la medida fija
            // Estas medidas fijas de escalas podrian calcularse en Init, a fines didacticos se hacen en cada update.
            escalaBase = TGCMatrix.Scaling(baseDX, baseDY, baseDZ);

            // 2- Brazo
            // ajusto a la medida fija
            escalaBrazo = TGCMatrix.Scaling(brazoDx, brazoDY, brazoDZ);
            // y lo traslado un poco para arriba, para que quede ubicado arriba de la base
            var T = TGCMatrix.Translation(0, brazoDY / 2.0f + baseDY / 2.0f, 0);
            // Guardo el punto donde tiene que girar el brazo = en la parte de abajo del brazo
            // le aplico la misma transformacion que al brazo (sin tener en cuenta el escalado)
            var pivoteBrazo = TGCVector3.TransformCoordinate(new TGCVector3(0, -brazoDY / 2.0f, 0.0f), T);
            // Ahora giro el brazo sobre el pivote, para ello, primero traslado el centro del mesh al pivote,
            // ahi aplico la rotacion, y luego vuelvo a trasladar a la posicion original
            var Rot = TGCMatrix.RotationZ(brazoDAng);
            var A   = TGCMatrix.Translation(-pivoteBrazo.X, -pivoteBrazo.Y, -pivoteBrazo.Z);
            var B   = TGCMatrix.Translation(pivoteBrazo.X, pivoteBrazo.Y, pivoteBrazo.Z);

            // Se calcula la matriz resultante, para utilizarse en render.
            transformacionBrazo = T * A * Rot * B;

            // 3- ante brazo
            // ajusto a la medida fija
            escalaAntebrazo = TGCMatrix.Scaling(antebrazoDX, antebrazoDY, antebrazoDZ);
            T = TGCMatrix.Translation(0, brazoDY / 2 + antebrazoDY / 2.0f, 0) * transformacionBrazo;
            // Guardo el punto donde tiene que girar el antebrazo
            var pivoteAntebrazo = TGCVector3.TransformCoordinate(new TGCVector3(0, -antebrazoDY / 2.0f, 0.0f), T);

            // orientacion del antebrazo
            Rot = TGCMatrix.RotationZ(antebrazoDAng);
            A   = TGCMatrix.Translation(-pivoteAntebrazo.X, -pivoteAntebrazo.Y, -pivoteAntebrazo.Z);
            B   = TGCMatrix.Translation(pivoteAntebrazo.X, pivoteAntebrazo.Y, pivoteAntebrazo.Z);
            // Se calcula la matriz resultante, para utilizarse en render.
            transformacionAntebrazo = T * A * Rot * B;

            // 4- pinza izquierda
            escalaPinza = TGCMatrix.Scaling(pinzaDX, pinzaDY, pinzaDZ);
            var C = TGCMatrix.Translation(pinzaTraslacion, 0f, 0f);

            T = C * TGCMatrix.Translation(pinzaDX / 2 - antebrazoDX / 2, antebrazoDY / 2.0f + pinzaDY / 2.0f, 0) *
                transformacionAntebrazo;
            // Guardo el punto donde tiene que girar la pinza
            var pivotePinzaIzquierda = TGCVector3.TransformCoordinate(new TGCVector3(0, -pinzaDY / 2.0f, 0.0f), T);

            // orientacion de la pinza
            Rot = TGCMatrix.RotationZ(pinzaDang);
            A   = TGCMatrix.Translation(-pivotePinzaIzquierda.X, -pivotePinzaIzquierda.Y, -pivotePinzaIzquierda.Z);
            B   = TGCMatrix.Translation(pivotePinzaIzquierda.X, pivotePinzaIzquierda.Y, pivotePinzaIzquierda.Z);
            // Se calcula la matriz resultante, para utilizarse en render.
            transformacionPinzaIzquierda = T * A * Rot * B;

            // mano derecha
            escalaPinza = TGCMatrix.Scaling(pinzaDX, pinzaDY, pinzaDZ);
            C           = TGCMatrix.Translation(-pinzaTraslacion, 0f, 0f);
            T           = C * TGCMatrix.Translation(antebrazoDX / 2 - pinzaDX / 2, antebrazoDY / 2 + pinzaDY / 2.0f, 0) * transformacionAntebrazo;
            // Guardo el punto donde tiene que girar la pinza
            var pivotePinzaDerecha = TGCVector3.TransformCoordinate(new TGCVector3(0, -pinzaDY / 2.0f, 0.0f), T);

            // orientacion de la pinza
            Rot = TGCMatrix.RotationZ(-pinzaDang);
            A   = TGCMatrix.Translation(-pivotePinzaDerecha.X, -pivotePinzaDerecha.Y, -pivotePinzaDerecha.Z);
            B   = TGCMatrix.Translation(pivotePinzaDerecha.X, pivotePinzaDerecha.Y, pivotePinzaDerecha.Z);
            // Se calcula la matriz resultante, para utilizarse en render.
            transformacionPinzaDerecha = T * A * Rot * B;

            PostUpdate();
        }