示例#1
0
        /// <summary>
        ///     Actualizar parámetros del plano en base a los valores configurados
        /// </summary>
        public void updateValues()
        {
            var vertices = new CustomVertex.PositionColored[6];

            //Crear un Quad con dos triángulos sobre XZ con normal default (0, 1, 0)
            var min = new Vector3(-Size.X / 2, 0, -Size.Y / 2);
            var max = new Vector3(Size.X / 2, 0, Size.Y / 2);
            var c   = color.ToArgb();

            vertices[0] = new CustomVertex.PositionColored(min, c);
            vertices[1] = new CustomVertex.PositionColored(min.X, 0, max.Z, c);
            vertices[2] = new CustomVertex.PositionColored(max, c);

            vertices[3] = new CustomVertex.PositionColored(min, c);
            vertices[4] = new CustomVertex.PositionColored(max, c);
            vertices[5] = new CustomVertex.PositionColored(max.X, 0, min.Z, c);

            //Obtener matriz de rotacion respecto de la normal del plano
            normal.Normalize();
            var angle        = FastMath.Acos(Vector3.Dot(ORIGINAL_DIR, normal));
            var axisRotation = Vector3.Cross(ORIGINAL_DIR, normal);

            axisRotation.Normalize();
            var t = Matrix.RotationAxis(axisRotation, angle) * Matrix.Translation(Center);

            //Transformar todos los puntos
            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i].Position = Vector3.TransformCoordinate(vertices[i].Position, t);
            }

            //Cargar vertexBuffer
            vertexBuffer.SetData(vertices, 0, LockFlags.None);
        }
示例#2
0
        public static void updateObbFromSegment(TgcBoundingOrientedBox obb, TGCVector3 a, TGCVector3 b, float thickness)
        {
            var lineDiff   = b - a;
            var lineLength = lineDiff.Length();
            var lineVec    = TGCVector3.Normalize(lineDiff);

            //Obtener angulo y vector de rotacion
            var upVec        = TGCVector3.Up;
            var angle        = FastMath.Acos(TGCVector3.Dot(upVec, lineVec));
            var axisRotation = TGCVector3.Cross(upVec, lineVec);

            axisRotation.Normalize();

            //Obtener matriz de rotacion para este eje y angulo
            var rotM = TGCMatrix.RotationAxis(axisRotation, angle);

            //Actualizar orientacion de OBB en base a matriz de rotacion
            obb.Orientation[0] = new TGCVector3(rotM.M11, rotM.M12, rotM.M13);
            obb.Orientation[1] = new TGCVector3(rotM.M21, rotM.M22, rotM.M23);
            obb.Orientation[2] = new TGCVector3(rotM.M31, rotM.M32, rotM.M33);

            //Actualizar extent de OBB segun el thickness del segmento
            obb.Extents = new TGCVector3(thickness, lineLength / 2, thickness);

            //Actualizar centro del OBB segun centro del segmento
            obb.Center = a + TGCVector3.Scale(lineDiff, 0.5f);

            //Regenerar OBB
            obb.updateValues();
        }
示例#3
0
        public override void Update()
        {
            currentPick = PerformPicking();

            if (PickingInsideSphere())
            {
                if (dragging)
                {
                    TGCVector3 normalizedTo = currentPick;
                    normalizedTo.Normalize();

                    var cross           = TGCVector3.Cross(normalizedFrom, normalizedTo);
                    var newRotation     = TGCQuaternion.RotationAxis(cross, FastMath.Acos(TGCVector3.Dot(normalizedFrom, normalizedTo)));
                    var currentRotation = TGCQuaternion.Multiply(stacked, newRotation);

                    shark.Transform = baseSharkTransform * TGCMatrix.RotationTGCQuaternion(currentRotation);

                    if (Input.buttonUp(TgcD3dInput.MouseButtons.BUTTON_LEFT))
                    {
                        stacked         = currentRotation;
                        shark.Transform = baseSharkTransform * TGCMatrix.RotationTGCQuaternion(stacked);
                        dragging        = false;
                    }
                }
                else if (Input.buttonPressed(TgcD3dInput.MouseButtons.BUTTON_LEFT))
                {
                    dragging       = true;
                    fromDrag       = currentPick;
                    normalizedFrom = TGCVector3.Normalize(fromDrag);
                }
            }

            UpdateArrows();
        }
示例#4
0
        public static void updateObbFromSegment(TgcObb obb, Vector3 a, Vector3 b, float thickness)
        {
            Vector3 lineDiff   = b - a;
            float   lineLength = lineDiff.Length();
            Vector3 lineVec    = Vector3.Normalize(lineDiff);

            //Obtener angulo y vector de rotacion
            Vector3 upVec        = new Vector3(0, 1, 0);
            float   angle        = FastMath.Acos(Vector3.Dot(upVec, lineVec));
            Vector3 axisRotation = Vector3.Cross(upVec, lineVec);

            axisRotation.Normalize();

            //Obtener matriz de rotacion para este eje y angulo
            Matrix rotM = Matrix.RotationAxis(axisRotation, angle);

            //Actualizar orientacion de OBB en base a matriz de rotacion
            obb.Orientation[0] = new Vector3(rotM.M11, rotM.M12, rotM.M13);
            obb.Orientation[1] = new Vector3(rotM.M21, rotM.M22, rotM.M23);
            obb.Orientation[2] = new Vector3(rotM.M31, rotM.M32, rotM.M33);

            //Actualizar extent de OBB segun el thickness del segmento
            obb.Extents = new Vector3(thickness, lineLength / 2, thickness);

            //Actualizar centro del OBB segun centro del segmento
            obb.Center = a + Vector3.Scale(lineDiff, 0.5f);

            //Regenerar OBB
            obb.updateValues();
        }
示例#5
0
        public float AngleBetweenVectors(TGCVector3 v1, TGCVector3 v2)
        {
            var dot            = TGCVector3.Dot(v1, v2);
            var modulusProduct = v1.Length() * v2.Length();

            return(FastMath.Acos(dot / modulusProduct));
        }
示例#6
0
        public static float calcularAnguloEntreVectoresNormalizados(Vector3 vector1, Vector3 vector2)
        {
            vector1 = Vector3.Normalize(vector1);
            vector2 = Vector3.Normalize(vector2);

            return((float)FastMath.Acos(Vector3.Dot(vector1, vector2)));
        }
        public void Update(float elapsedTime)
        {
            TGCQuaternion rotationX  = TGCQuaternion.RotationAxis(new TGCVector3(0.0f, 1.0f, 0.0f), Geometry.DegreeToRadian(90f /* + anguloEntreVectores*15*/));
            TGCVector3    PosicionA  = posicionInicial;
            TGCVector3    PosicionB  = jugador.GetPosicion();
            TGCVector3    DireccionA = new TGCVector3(0, 0, -1);
            TGCVector3    DireccionB = PosicionB - PosicionA;

            if (DireccionB.Length() >= 15f && PosicionA.Z > PosicionB.Z + 10f)
            {
                DireccionB.Normalize();
                // anguloEntreVectores = (float)Math.Acos(TGCVector3.Dot(DireccionA, DireccionB));

                var cross       = TGCVector3.Cross(DireccionA, DireccionB);
                var newRotation = TGCQuaternion.RotationAxis(cross, FastMath.Acos(TGCVector3.Dot(DireccionA, DireccionB)));
                quaternionAuxiliar = rotationX * newRotation;
                mainMesh.Transform = baseScaleRotation *
                                     TGCMatrix.RotationTGCQuaternion(rotationX * newRotation) *
                                     baseQuaternionTranslation;
            }
            else
            {
                mainMesh.Transform = baseScaleRotation *
                                     TGCMatrix.RotationTGCQuaternion(quaternionAuxiliar) *
                                     baseQuaternionTranslation;
            }
            //codigo de prueba------
            tiempo += .1f + elapsedTime;
            if (tiempo > 15f)
            {
                Disparar(PosicionB);
                tiempo = 0f;
            }
            //--------
        }
        private TGCQuaternion QuaternionDireccion(TGCVector3 direccionDisparoNormalizado)
        {
            TGCVector3    DireccionA  = new TGCVector3(0, 0, -1);
            TGCVector3    cross       = TGCVector3.Cross(DireccionA, direccionDisparoNormalizado);
            TGCQuaternion newRotation = TGCQuaternion.RotationAxis(cross, FastMath.Acos(TGCVector3.Dot(DireccionA, direccionDisparoNormalizado)));

            return(newRotation);
        }
        /// <summary>
        /// Devuelve la rotacion que se debe aplicar a la matriz de transformacion para que la entidad apunte hacia una direccion.
        /// </summary>
        /// <param name="lookDir">Vector normalizado que define la direccion a la que debe mirar la entidad.</param>
        private void LookAt(TGCVector3 lookDir)
        {
            float      angle     = FastMath.Acos(TGCVector3.Dot(defaultLookDir, lookDir));
            TGCVector3 rotVector = TGCVector3.Cross(defaultLookDir, lookDir);

            rotVector.Z = 0;

            rotation = TGCQuaternion.RotationAxis(rotVector, angle);
        }
示例#10
0
        public void calcularOrientacion(Vector3 direccion, Vector3 centro, Vector3 normal)
        {
            float   angle     = FastMath.Acos(Vector3.Dot(normal, direccion));
            Vector3 rotationY = Vector3.Cross(normal, direccion);

            rotationY.Normalize();
            Matrix Move = Matrix.Translation(centro);

            Move.Invert();
            m_mRotacion = Move * Matrix.RotationAxis(rotationY, angle) * Matrix.Translation(centro);
        }
        public static float anguloEntreVectores(TGCVector3 vector1, TGCVector3 vector2)
        {
            if ((TGCVector3.Length(vector1) * TGCVector3.Length(vector2)) == 0)
            {
                return(0);
            }

            float cos = TGCVector3.Dot(vector1, vector2) / (TGCVector3.Length(vector1) * TGCVector3.Length(vector2));

            return(FastMath.Acos(cos));
        }
示例#12
0
        public override void calcularTraslacionYRotacion(float elapsedTime, TerrenoSimple agua, float time, Vector3 lastPosition)
        {
            distance = player.popa() - this.getPosition();
            Vector3 iaDirectionVersor = this.vectorDireccion();

            iaDirectionVersor.Normalize();
            Vector3 lookAtPopaVersor = new Vector3(distance.X, distance.Y, distance.Z);

            lookAtPopaVersor.Normalize();

            float   rotationAngle = FastMath.Acos(Vector3.Dot(iaDirectionVersor, lookAtPopaVersor));
            Vector3 cross         = Vector3.Cross(lookAtPopaVersor, iaDirectionVersor);

            //Vector3 lastPosition = getPosition();

            if (cross.Length() > 0.1)
            {
                if (cross.Y > 0.1)
                {
                    anguloRotacion -= elapsedTime * ROTATION_SPEED;
                }
                if (cross.Y < -0.1)
                {
                    anguloRotacion += elapsedTime * ROTATION_SPEED;
                }

                rotacion = Matrix.RotationY(anguloRotacion);
            }

            if (FastMath.Abs(distance.Length()) > 1000)
            {
                movementSpeed = Math.Min(movementSpeed + ESCALON_VEL, VEL_MAXIMA);
            }
            if (FastMath.Abs(distance.Length()) <= 1000)
            {
                if (FastMath.Abs(distance.Length()) > 400)
                {
                    movementSpeed = Math.Max(movementSpeed - movementSpeed / distance.Length(), 0);
                }
                else
                {
                    movementSpeed = 0;
                }
            }

            movZ -= movementSpeed * FastMath.Cos(anguloRotacion) * elapsedTime;
            movX -= movementSpeed * FastMath.Sin(anguloRotacion) * elapsedTime;
            movY  = agua.aplicarOlasA(getPosition(), time).Y + AltoBote / 2;

            administrarColisiones(lastPosition, new Vector3(movX, movY, movZ));
        }
示例#13
0
        public void updatePlayerPointer()
        {
            Vector3 currentView = CustomFpsCamera.Instance.getLookAt() - CustomFpsCamera.Instance.getPosition();

            Vector2 cur = new Vector2(currentView.X, currentView.Z);

            cur.Normalize();
            float rot   = (float)FastMath.Acos(Vector2.Dot(initialPos, cur));
            float cross = Vector2.Ccw(initialPos, cur);

            float arc = cross > 0 ? rot : -rot;

            playerPointerSprite.Rotation = (float)Math.PI / 2 - arc;
        }
示例#14
0
        public override void render(float elapsedTime)
        {
            Device d3dDevice = GuiController.Instance.D3dDevice;


            // 1) Crear un vector en 3D
            Vector3 v = new Vector3(0, 19, -1759.21f);


            // 2) Producto escalar entre dos vectores (dot product)
            Vector3 v1        = new Vector3(0, 19, -1759.21f);
            Vector3 v2        = new Vector3(0, 19, -1759.21f);
            float   dotResult = Vector3.Dot(v1, v2);


            // 3) Producto vectorial entre dos vectores (cross product). El orden de v1 y v2 influye en la orientacion del resultado
            Vector3 crossResultVec = Vector3.Cross(v1, v2);


            // 4) Distancia entre dos puntos
            Vector3 p1                = new Vector3(100, 200, 300);
            Vector3 p2                = new Vector3(1000, 2000, 3000);
            float   distancia         = Vector3.Length(p2 - p1);
            float   distanciaCuadrada = Vector3.LengthSq(p2 - p1); //Es mas eficiente porque evita la raiz cuadrada (pero te da el valor al cuadrado)


            // 5) Normalizar vector
            Vector3 norm = Vector3.Normalize(v1);


            // 6) Obtener el ángulo que hay entre dos vectores que están en XZ, expresion A.B=|A||B|cos(a)
            Vector3 v3    = new Vector3(-1, 0, 19);
            Vector3 v4    = new Vector3(3, 0, -5);
            float   angle = FastMath.Acos(Vector3.Dot(Vector3.Normalize(v3), Vector3.Normalize(v4))); //Tienen que estar normalizados


            // 7) Tenemos un objeto que rota un cierto ángulo en Y (ej: un auto) y queremos saber los componentes X,Z para donde tiene que avanzar al moverse
            float   rotacionY           = FastMath.PI_HALF;
            float   componenteX         = FastMath.Sin(rotacionY);
            float   componenteZ         = FastMath.Cos(rotacionY);
            float   velocidadMovimiento = 100; //Ojo que este valor deberia siempre multiplicarse por el elapsedTime
            Vector3 movimientoAdelante  = new Vector3(componenteX * velocidadMovimiento, 0, componenteZ * velocidadMovimiento);



            GuiController.Instance.Text3d.drawText("Este ejemplo no muestra nada por pantalla. Sino que es para leer el código y sus comentarios.", 5, 50, Color.Yellow);
        }
        public override void Render()
        {
            PreRender();

            // 1) Crear un vector en 3D
            var v = new TGCVector3(0, 19, -1759.21f);

            // 2) Producto escalar entre dos vectores (dot product)
            var v1        = new TGCVector3(0, 19, -1759.21f);
            var v2        = new TGCVector3(0, 19, -1759.21f);
            var dotResult = TGCVector3.Dot(v1, v2);

            // 3) Producto vectorial entre dos vectores (cross product). El orden de v1 y v2 influye en la orientacion del resultado
            var crossResultVec = TGCVector3.Cross(v1, v2);

            // 4) Distancia entre dos puntos
            var p1                = new TGCVector3(100, 200, 300);
            var p2                = new TGCVector3(1000, 2000, 3000);
            var distancia         = TGCVector3.Length(p2 - p1);
            var distanciaCuadrada = TGCVector3.LengthSq(p2 - p1);
            //Es mas eficiente porque evita la raiz cuadrada (pero te da el valor al cuadrado)

            // 5) Normalizar vector
            var norm = TGCVector3.Normalize(v1);

            // 6) Obtener el angulo que hay entre dos vectores que estan en XZ, expresion A.B=|A||B|cos(a)
            var v3    = new TGCVector3(-1, 0, 19);
            var v4    = new TGCVector3(3, 0, -5);
            var angle = FastMath.Acos(TGCVector3.Dot(TGCVector3.Normalize(v3), TGCVector3.Normalize(v4)));
            //Tienen que estar normalizados

            // 7) Tenemos un objeto que rota un cierto angulo en Y (ej: un auto) y queremos saber los componentes X,Z para donde tiene que avanzar al moverse
            var   rotacionY           = FastMath.PI_HALF;
            var   componenteX         = FastMath.Sin(rotacionY);
            var   componenteZ         = FastMath.Cos(rotacionY);
            float velocidadMovimiento = 100; //Ojo que este valor deberia siempre multiplicarse por el elapsedTime
            var   movimientoAdelante  = new TGCVector3(componenteX * velocidadMovimiento, 0, componenteZ * velocidadMovimiento);

            DrawText.drawText(
                "Este ejemplo no muestra nada por pantalla. Sino que es para leer el codigo y sus comentarios.", 5, 50,
                Color.Yellow);

            PostRender();
        }
示例#16
0
        /// <summary>
        /// Setear la camara con una determinada posicion y lookAt
        /// </summary>
        public void lookAt(Vector3 pos, Vector3 lookAt)
        {
            //TODO: solo funciona bien para hacer un TopView

            Vector3 v      = pos - lookAt;
            float   length = Vector3.Length(v);

            v.Scale(1 / length);

            cameraDistance = length;
            upVector       = new Vector3(0, 1, 0);
            diffX          = 0;
            diffY          = 0.01f;
            diffZ          = 1;
            baseRotX       = -FastMath.Acos(Vector3.Dot(new Vector3(0, 0, -1), v));
            //baseRotY = FastMath.Acos(Vector3.Dot(new Vector3(0, 0, -1), v));
            baseRotY     = 0;
            cameraCenter = lookAt;
        }
示例#17
0
        /// <summary>
        ///     Setear la camara con una determinada posicion y lookAt
        /// </summary>
        public void lookAt(TGCVector3 pos, TGCVector3 lookAt)
        {
            //TODO: solo funciona bien para hacer un TopView

            var v      = pos - lookAt;
            var length = TGCVector3.Length(v);

            v.Scale(1 / length);

            CameraDistance = length;
            upVector       = TGCVector3.Up;
            diffX          = 0;
            diffY          = 0.01f;
            diffZ          = 1;
            BaseRotX       = -FastMath.Acos(TGCVector3.Dot(new TGCVector3(0, 0, -1), v));
            //baseRotY = FastMath.Acos(TGCVector3.Dot(new TGCVector3(0, 0, -1), v));
            BaseRotY     = 0;
            CameraCenter = lookAt;
        }
        public void Start(Vector3 location, Single Power, Vector3 newUpDirection)
        {
            if (Power < 15)
            {
                return;
            }

            if (Animation != null)
            {
                Animation.Size     = _InitialSize * (_PowerFactor * Power);
                Animation.Position = location;// +newUpDirection * (Animation.Size.Y / 2);

                Single alfa = FastMath.Acos(Vector3.Dot(newUpDirection, _InitialUpDirection));
                if (newUpDirection.X > _InitialUpDirection.X)
                {
                    alfa *= -1;
                }
                Animation.RotationMatrix = Matrix.RotationZ(alfa);
                this.Start();
            }
        }
示例#19
0
        public void UpdateInternalValues()
        {
            frontVector = new TGCVector3(Vector3.TransformNormal(-Vector3.UnitZ, RigidBody.InterpolationWorldTransform));
            var velocityVector = new TGCVector3(RigidBody.InterpolationLinearVelocity.X, 0, RigidBody.InterpolationLinearVelocity.Z);

            if (velocityVector.Length() < 0.12f)
            {
                velocityVector = TGCVector3.Empty;
            }
            var speedAngle = FastMath.Acos(TGCVector3.Dot(frontVector, velocityVector) / (frontVector.Length() * velocityVector.Length()));

            velocityVector.Multiply(2f);

            currentSpeed = (int)velocityVector.Length();

            if (speedAngle >= FastMath.PI_HALF)
            {
                currentSpeed *= -1;
            }

            yawPitchRoll = Quat.ToEulerAngles(RigidBody.Orientation);
        }
示例#20
0
        /// <summary>
        /// Actualizar parámetros de la pared en base a los valores configurados
        /// </summary>
        public void updateValues()
        {
            //Calcular los 4 corners de la pared, segun el tipo de orientacion
            Vector3 bLeft  = new Vector3(-size.X / 2, 0, -size.Y / 2);
            Vector3 tLeft  = new Vector3(size.X / 2, 0, -size.Y / 2);
            Vector3 bRight = new Vector3(-size.X / 2, 0, size.Y / 2);
            Vector3 tRight = new Vector3(size.X / 2, 0, size.Y / 2);
            //Auto ajustar UV
            float offsetU = this.uvOffset.X;
            float offsetV = this.uvOffset.Y;

            //Primer triangulo
            vertices[0] = new CustomVertex.PositionTextured(bLeft, offsetU + uTile, offsetV + vTile);
            vertices[1] = new CustomVertex.PositionTextured(tLeft, offsetU, offsetV + vTile);
            vertices[2] = new CustomVertex.PositionTextured(tRight, offsetU, offsetV);

            //Segundo triangulo
            vertices[3] = new CustomVertex.PositionTextured(bLeft, offsetU + uTile, offsetV + vTile);
            vertices[4] = new CustomVertex.PositionTextured(tRight, offsetU, offsetV);
            vertices[5] = new CustomVertex.PositionTextured(bRight, offsetU + uTile, offsetV);

            //BoundingBox
            normal = (origin - GuiController.Instance.CurrentCamera.getLookAt());
            normal.Normalize();
            float   angle        = FastMath.Acos(Vector3.Dot(ORIGINAL_DIR, normal));
            Vector3 axisRotation = Vector3.Cross(ORIGINAL_DIR, normal);

            axisRotation.Normalize();
            Matrix t = Matrix.RotationAxis(axisRotation, angle) * Matrix.Translation(origin);

            //Transformar todos los puntos
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].Position = Vector3.TransformCoordinate(vertices[i].Position, t);
            }
        }
示例#21
0
 //Ángulo entre la dirección a la que apunta el auto IA y otro vector
 public float AnguloAlVector(TGCVector2 vector)
 {
     return(FastMath.ToDeg(FastMath.Acos((VersorDirector.X * vector.X + VersorDirector.Z * vector.Y) / (ModuloVector(new TGCVector2(VersorDirector.X, VersorDirector.Z)) * ModuloVector(vector)))));
 }
示例#22
0
        public void MoverHaciaObjetivo(float tiempoRenderizado, Vector3 posicionObjetivo)
        {
            if (this.Modelo.Enabled)
            {
                //Resto los dos vectores para hallar el vector distancia
                Vector3 Distancia = Vector3.Subtract(posicionObjetivo, this.Modelo.Position);

                //Otro vector, con valores absolutos para hallar la componente maxima
                Vector3 DistanciaAbs = TgcVectorUtils.abs(Distancia);

                //Calculo matriz de rotacion
                Vector3 DireccionObjetivo = Vector3.Normalize(posicionObjetivo - this.Modelo.Position);
                float   angulo            = FastMath.Acos(Vector3.Dot(RotacionOriginal, DireccionObjetivo));
                Vector3 axisRotation      = Vector3.Cross(this.Modelo.Rotation, DireccionObjetivo);
                MatrizRotacion = Matrix.RotationAxis(axisRotation, angulo);

                float cantidadDeMovimiento = this.VelocidadMovimiento * tiempoRenderizado;
                float giro = this.Modelo.Rotation.Y - angulo;
                if (giro < -0.1)
                {
                    this.Modelo.rotateY(Geometry.DegreeToRadian(-giro * 100 * tiempoRenderizado));
                    return;
                }
                else if (giro > 0.1)
                {
                    this.Modelo.rotateY(Geometry.DegreeToRadian(-giro * 100 * tiempoRenderizado));
                    return;
                }
                if (DistanciaAbs.X + DistanciaAbs.Y + DistanciaAbs.Z > 700f)
                {
                    //Hallo la componente de mayor valor y me muevo en esa direccion. VER SENTIDO.
                    if (DistanciaAbs.X >= DistanciaAbs.Y)
                    {
                        if (DistanciaAbs.X >= DistanciaAbs.Z)
                        {
                            // MOVER EN X
                            if (Distancia.X > cantidadDeMovimiento)
                            {
                                this.Modelo.move(cantidadDeMovimiento, 0, 0);
                            }
                            else
                            {
                                this.Modelo.move(cantidadDeMovimiento * -1, 0, 0);
                            }
                        }
                        else
                        {
                            // MOVER EN Z
                            if (Distancia.Z > 0)
                            {
                                this.Modelo.move(0, 0, cantidadDeMovimiento);
                            }
                            else
                            {
                                this.Modelo.move(0, 0, cantidadDeMovimiento * -1);
                            }
                        }
                    }
                    else
                    {
                        if (DistanciaAbs.Y >= DistanciaAbs.Z)
                        {
                            // MOVER EN Y
                            if (Distancia.Y > 0)
                            {
                                this.Modelo.move(0, cantidadDeMovimiento, 0);
                            }
                            else
                            {
                                this.Modelo.move(0, cantidadDeMovimiento * -1, 0);
                            }
                        }
                        else
                        {
                            // MOVER EN Z
                            if (Distancia.Z > 0)
                            {
                                this.Modelo.move(0, 0, cantidadDeMovimiento);
                            }
                            else
                            {
                                this.Modelo.move(0, 0, cantidadDeMovimiento * -1);
                            }
                        }
                    }
                }
                else
                {
                    //Disparar. Tambien deberia rotar para que el disparo vaya bien

                    if (TiempoParado == 0 || TiempoParado >= TiempoRecarga)
                    {
                        Disparo disparo = new Disparo(this.Modelo, MatrizRotacion);
                        Disparos.Add(disparo);
                        TiempoParado = 0f;
                    }
                    TiempoParado = TiempoParado + tiempoRenderizado * 4;
                }
            }
        }
示例#23
0
        /// <summary>
        ///     Add new BoxLine mesh
        /// </summary>
        public void addBoxLine(TGCVector3 pStart, TGCVector3 pEnd, float thickness, Color color)
        {
            const int vertexCount = 36;

            checkAndFlush(vertexCount);
            var initIdx = idx;

            var c = color.ToArgb();

            //Crear caja en vertical en Y con longitud igual al módulo de la recta.
            var lineVec    = TGCVector3.Subtract(pEnd, pStart);
            var lineLength = lineVec.Length();
            var min        = new TGCVector3(-thickness, 0, -thickness);
            var max        = new TGCVector3(thickness, lineLength, thickness);

            //Vértices de la caja con forma de linea
            // Front face
            addTriangle(
                new CustomVertex.PositionColored(min.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(min.X, min.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, max.Z, c)
                );
            addTriangle(
                new CustomVertex.PositionColored(min.X, min.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, max.Z, c)
                );

            // Back face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            addTriangle(
                new CustomVertex.PositionColored(min.X, max.Y, min.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, min.Z, c),
                new CustomVertex.PositionColored(min.X, min.Y, min.Z, c)
                );
            addTriangle(
                new CustomVertex.PositionColored(min.X, min.Y, min.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, min.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, min.Z, c)
                );

            // Top face
            addTriangle(
                new CustomVertex.PositionColored(min.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, min.Z, c),
                new CustomVertex.PositionColored(min.X, max.Y, min.Z, c)
                );
            addTriangle(
                new CustomVertex.PositionColored(min.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, min.Z, c)
                );

            // Bottom face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            addTriangle(
                new CustomVertex.PositionColored(min.X, min.Y, max.Z, c),
                new CustomVertex.PositionColored(min.X, min.Y, min.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, min.Z, c)
                );
            addTriangle(
                new CustomVertex.PositionColored(min.X, min.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, min.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, max.Z, c)
                );

            // Left face
            addTriangle(
                new CustomVertex.PositionColored(min.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(min.X, min.Y, min.Z, c),
                new CustomVertex.PositionColored(min.X, min.Y, max.Z, c)
                );
            addTriangle(
                new CustomVertex.PositionColored(min.X, max.Y, min.Z, c),
                new CustomVertex.PositionColored(min.X, min.Y, min.Z, c),
                new CustomVertex.PositionColored(min.X, max.Y, max.Z, c)
                );

            // Right face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            addTriangle(
                new CustomVertex.PositionColored(max.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, min.Z, c)
                );
            addTriangle(
                new CustomVertex.PositionColored(max.X, max.Y, min.Z, c),
                new CustomVertex.PositionColored(max.X, max.Y, max.Z, c),
                new CustomVertex.PositionColored(max.X, min.Y, min.Z, c)
                );

            //Obtener matriz de rotacion respecto del vector de la linea
            lineVec.Normalize();
            var angle        = FastMath.Acos(TGCVector3.Dot(BOX_LINE_ORIGINAL_DIR, lineVec));
            var axisRotation = TGCVector3.Cross(BOX_LINE_ORIGINAL_DIR, lineVec);

            axisRotation.Normalize();
            var t = TGCMatrix.RotationAxis(axisRotation, angle) * TGCMatrix.Translation(pStart);

            //Transformar todos los puntos
            for (var i = initIdx; i < initIdx + vertexCount; i++)
            {
                vertices[i].Position = TGCVector3.TransformCoordinate(TGCVector3.FromVector3(vertices[i].Position), t);
            }
        }
示例#24
0
        public static float AngleBetweenVectors(TGCVector3 vectorA, TGCVector3 vectorB)
        {
            var dotProduct = TGCVector3.Dot(vectorA, vectorB) / (vectorA.Length() * vectorB.Length());

            return(dotProduct < 1 ? FastMath.Acos(dotProduct) : 0);
        }
示例#25
0
        /// <summary>
        ///     Actualizar parámetros de la línea en base a los valores configurados
        /// </summary>
        public void updateValues()
        {
            var c        = color.ToArgb();
            var vertices = new CustomVertex.PositionColored[36];

            //Crear caja en vertical en Y con longitud igual al módulo de la recta.
            var lineVec    = Vector3.Subtract(PEnd, PStart);
            var lineLength = lineVec.Length();
            var min        = new Vector3(-Thickness, 0, -Thickness);
            var max        = new Vector3(Thickness, lineLength, Thickness);

            //Vértices de la caja con forma de linea
            // Front face
            vertices[0] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, c);
            vertices[1] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, c);
            vertices[2] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, c);
            vertices[3] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, c);
            vertices[4] = new CustomVertex.PositionColored(max.X, min.Y, max.Z, c);
            vertices[5] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, c);

            // Back face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            vertices[6]  = new CustomVertex.PositionColored(min.X, max.Y, min.Z, c);
            vertices[7]  = new CustomVertex.PositionColored(max.X, max.Y, min.Z, c);
            vertices[8]  = new CustomVertex.PositionColored(min.X, min.Y, min.Z, c);
            vertices[9]  = new CustomVertex.PositionColored(min.X, min.Y, min.Z, c);
            vertices[10] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, c);
            vertices[11] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, c);

            // Top face
            vertices[12] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, c);
            vertices[13] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, c);
            vertices[14] = new CustomVertex.PositionColored(min.X, max.Y, min.Z, c);
            vertices[15] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, c);
            vertices[16] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, c);
            vertices[17] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, c);

            // Bottom face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            vertices[18] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, c);
            vertices[19] = new CustomVertex.PositionColored(min.X, min.Y, min.Z, c);
            vertices[20] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, c);
            vertices[21] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, c);
            vertices[22] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, c);
            vertices[23] = new CustomVertex.PositionColored(max.X, min.Y, max.Z, c);

            // Left face
            vertices[24] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, c);
            vertices[25] = new CustomVertex.PositionColored(min.X, min.Y, min.Z, c);
            vertices[26] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, c);
            vertices[27] = new CustomVertex.PositionColored(min.X, max.Y, min.Z, c);
            vertices[28] = new CustomVertex.PositionColored(min.X, min.Y, min.Z, c);
            vertices[29] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, c);

            // Right face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            vertices[30] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, c);
            vertices[31] = new CustomVertex.PositionColored(max.X, min.Y, max.Z, c);
            vertices[32] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, c);
            vertices[33] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, c);
            vertices[34] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, c);
            vertices[35] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, c);

            //Obtener matriz de rotacion respecto del vector de la linea
            lineVec.Normalize();
            var angle        = FastMath.Acos(Vector3.Dot(ORIGINAL_DIR, lineVec));
            var axisRotation = Vector3.Cross(ORIGINAL_DIR, lineVec);

            axisRotation.Normalize();
            var t = Matrix.RotationAxis(axisRotation, angle) * Matrix.Translation(PStart);

            //Transformar todos los puntos
            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i].Position = Vector3.TransformCoordinate(vertices[i].Position, t);
            }

            //Cargar vertexBuffer
            vertexBuffer.SetData(vertices, 0, LockFlags.None);
        }
        public override void Render()
        {
            PreRender();

            //Si hacen clic con el mouse, ver si hay colision con el suelo
            if (TgcD3dInput.Instance.buttonPressed(TgcD3dInput.MouseButtons.BUTTON_LEFT))
            {
                //Actualizar Ray de colisión en base a posición del mouse
                pickingRay.updateRay();

                //Detectar colisión Ray-AABB
                if (TgcCollisionUtils.intersectRayAABB(pickingRay.Ray, suelo.BoundingBox, out newPosition))
                {
                    //Fijar nueva posición destino
                    applyMovement = true;

                    collisionPointMesh.Position = newPosition;
                    directionArrow.PEnd = new Vector3(newPosition.X, 30f, newPosition.Z);

                    //Rotar modelo en base a la nueva dirección a la que apunta
                    var direction = Vector3.Normalize(newPosition - mesh.Position);
                    var angle = FastMath.Acos(Vector3.Dot(originalMeshRot, direction));
                    var axisRotation = Vector3.Cross(originalMeshRot, direction);
                    meshRotationMatrix = Matrix.RotationAxis(axisRotation, angle);
                }
            }

            var speed = (float)Modifiers["speed"];

            //Interporlar movimiento, si hay que mover
            if (applyMovement)
            {
                //Ver si queda algo de distancia para mover
                var posDiff = newPosition - mesh.Position;
                var posDiffLength = posDiff.LengthSq();
                if (posDiffLength > float.Epsilon)
                {
                    //Movemos el mesh interpolando por la velocidad
                    var currentVelocity = speed * ElapsedTime;
                    posDiff.Normalize();
                    posDiff.Multiply(currentVelocity);

                    //Ajustar cuando llegamos al final del recorrido
                    var newPos = mesh.Position + posDiff;
                    if (posDiff.LengthSq() > posDiffLength)
                    {
                        newPos = newPosition;
                    }

                    //Actualizar flecha de movimiento
                    directionArrow.PStart = new Vector3(mesh.Position.X, 30f, mesh.Position.Z);
                    directionArrow.updateValues();

                    //Actualizar posicion del mesh
                    mesh.Position = newPos;

                    //Como desactivamos la transformacion automatica, tenemos que armar nosotros la matriz de transformacion
                    mesh.Transform = meshRotationMatrix * Matrix.Translation(mesh.Position);

                    //Actualizar camara
                    camaraInterna.Target = mesh.Position;
                }
                //Se acabo el movimiento
                else
                {
                    applyMovement = false;
                }
            }

            //Mostrar caja con lugar en el que se hizo click, solo si hay movimiento
            if (applyMovement)
            {
                collisionPointMesh.render();
                directionArrow.render();
            }

            suelo.render();
            mesh.render();

            PostRender();
        }
示例#27
0
 public float ObtenerRotacion(TGCVector3 a, TGCVector3 b)
 {
     return(FastMath.Acos(TGCVector3.Dot(a, b) / (a.Length() * b.Length())));
 }
示例#28
0
        /// <summary>
        ///     Actualizar parámetros de la flecha en base a los valores configurados
        /// </summary>
        public void updateValues()
        {
            var vertices = new CustomVertex.PositionColored[54];

            //Crear caja en vertical en Y con longitud igual al módulo de la recta.
            var lineVec    = TGCVector3.Subtract(PEnd, PStart);
            var lineLength = lineVec.Length();
            var min        = new TGCVector3(-Thickness, 0, -Thickness);
            var max        = new TGCVector3(Thickness, lineLength, Thickness);

            //Vertices del cuerpo de la flecha
            var bc = bodyColor.ToArgb();

            // Front face
            vertices[0] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, bc);
            vertices[1] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, bc);
            vertices[2] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, bc);
            vertices[3] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, bc);
            vertices[4] = new CustomVertex.PositionColored(max.X, min.Y, max.Z, bc);
            vertices[5] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, bc);

            // Back face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            vertices[6]  = new CustomVertex.PositionColored(min.X, max.Y, min.Z, bc);
            vertices[7]  = new CustomVertex.PositionColored(max.X, max.Y, min.Z, bc);
            vertices[8]  = new CustomVertex.PositionColored(min.X, min.Y, min.Z, bc);
            vertices[9]  = new CustomVertex.PositionColored(min.X, min.Y, min.Z, bc);
            vertices[10] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, bc);
            vertices[11] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, bc);

            // Top face
            vertices[12] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, bc);
            vertices[13] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, bc);
            vertices[14] = new CustomVertex.PositionColored(min.X, max.Y, min.Z, bc);
            vertices[15] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, bc);
            vertices[16] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, bc);
            vertices[17] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, bc);

            // Bottom face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            vertices[18] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, bc);
            vertices[19] = new CustomVertex.PositionColored(min.X, min.Y, min.Z, bc);
            vertices[20] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, bc);
            vertices[21] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, bc);
            vertices[22] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, bc);
            vertices[23] = new CustomVertex.PositionColored(max.X, min.Y, max.Z, bc);

            // Left face
            vertices[24] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, bc);
            vertices[25] = new CustomVertex.PositionColored(min.X, min.Y, min.Z, bc);
            vertices[26] = new CustomVertex.PositionColored(min.X, min.Y, max.Z, bc);
            vertices[27] = new CustomVertex.PositionColored(min.X, max.Y, min.Z, bc);
            vertices[28] = new CustomVertex.PositionColored(min.X, min.Y, min.Z, bc);
            vertices[29] = new CustomVertex.PositionColored(min.X, max.Y, max.Z, bc);

            // Right face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            vertices[30] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, bc);
            vertices[31] = new CustomVertex.PositionColored(max.X, min.Y, max.Z, bc);
            vertices[32] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, bc);
            vertices[33] = new CustomVertex.PositionColored(max.X, max.Y, min.Z, bc);
            vertices[34] = new CustomVertex.PositionColored(max.X, max.Y, max.Z, bc);
            vertices[35] = new CustomVertex.PositionColored(max.X, min.Y, min.Z, bc);

            //Vertices del cuerpo de la flecha
            var hc   = headColor.ToArgb();
            var hMin = new TGCVector3(-HeadSize.X, lineLength, -HeadSize.X);
            var hMax = new TGCVector3(HeadSize.X, lineLength + HeadSize.Y, HeadSize.X);

            //Bottom face
            vertices[36] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMax.Z, hc);
            vertices[37] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMin.Z, hc);
            vertices[38] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMin.Z, hc);
            vertices[39] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMax.Z, hc);
            vertices[40] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMin.Z, hc);
            vertices[41] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMax.Z, hc);

            //Left face
            vertices[42] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMin.Z, hc);
            vertices[43] = new CustomVertex.PositionColored(0, hMax.Y, 0, hc);
            vertices[44] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMax.Z, hc);

            //Right face
            vertices[45] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMin.Z, hc);
            vertices[46] = new CustomVertex.PositionColored(0, hMax.Y, 0, hc);
            vertices[47] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMax.Z, hc);

            //Back face
            vertices[48] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMin.Z, hc);
            vertices[49] = new CustomVertex.PositionColored(0, hMax.Y, 0, hc);
            vertices[50] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMin.Z, hc);

            //Front face
            vertices[51] = new CustomVertex.PositionColored(hMin.X, hMin.Y, hMax.Z, hc);
            vertices[52] = new CustomVertex.PositionColored(0, hMax.Y, 0, hc);
            vertices[53] = new CustomVertex.PositionColored(hMax.X, hMin.Y, hMax.Z, hc);

            //Obtener matriz de rotacion respecto del vector de la linea
            lineVec.Normalize();
            var angle        = FastMath.Acos(TGCVector3.Dot(ORIGINAL_DIR, lineVec));
            var axisRotation = TGCVector3.Cross(ORIGINAL_DIR, lineVec);

            axisRotation.Normalize();
            var t = TGCMatrix.RotationAxis(axisRotation, angle) * TGCMatrix.Translation(PStart);

            //Transformar todos los puntos
            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i].Position = TGCVector3.TransformCoordinate(TGCVector3.FromVector3(vertices[i].Position), t);
            }

            //Cargar vertexBuffer
            vertexBuffer.SetData(vertices, 0, LockFlags.None);
        }
示例#29
0
        public static float AngleBetween(TGCVector3 v1, TGCVector3 v2)
        {
            float cosine = TGCVector3.Dot(v1, v2) / (TGCVector3.Length(v1) * TGCVector3.Length(v2));

            return(FastMath.Acos(cosine));
        }
示例#30
0
        public float AnguloEntreVectores(TGCVector3 v1, TGCVector3 v2)
        {
            var angle = FastMath.Acos(TGCVector3.Dot(TGCVector3.Normalize(v1), TGCVector3.Normalize(v2)));

            return(angle);
        }