Пример #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 TGCVector3(-Size.X / 2, 0, -Size.Y / 2);
            var max = new TGCVector3(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(TGCVector3.Dot(ORIGINAL_DIR, Normal));
            var axisRotation = TGCVector3.Cross(ORIGINAL_DIR, Normal);

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

            //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);
        }
        public void RecalcularForward()
        {
            TGCVector3 targerLinterna = new TGCVector3(target.X - meshPersonaje.Position.X, 0, target.Z - meshPersonaje.Position.Z);


            xAxis = new TGCVector3(targerLinterna.X, 0, targerLinterna.Z);
            xAxis.Normalize();

            yAxis = new TGCVector3(0, 1, 0);
            yAxis.Normalize();

            //zAxis = new TGCVector3(0, up, 0);

            TGCMatrix deltaRM =
                TGCMatrix.RotationAxis(xAxis, anguloAbsolutoEnX) * TGCMatrix.RotationAxis(yAxis, 1 * anguloAbsolutoEnY);


            TGCVector4 result;

            result = TGCVector3.Transform(xAxis, deltaRM);
            xAxis  = new TGCVector3(result.X, result.Y, result.Z);

            result = TGCVector3.Transform(yAxis, deltaRM);
            yAxis  = new TGCVector3(result.X, result.Y, result.Z);

            result = TGCVector3.Transform(zAxis, deltaRM);
            zAxis  = new TGCVector3(result.X, result.Y, result.Z);

            forward = puntoDemira(anguloAbsolutoEnX, anguloAbsolutoEnY) - meshPersonaje.Position;
            forward = targerLinterna;
            //forward = TGCVector3.Cross(forward, up);
            forward.Normalize();
        }
Пример #3
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();
        }
Пример #4
0
 private void updateMesh()
 {
     mesh.Transform =
         TGCMatrix.RotationAxis(TGCVector3.Cross(lookAt, lookin),
                                -(float)Math.Acos(TGCVector3.Dot(lookAt, lookin)))
         * TGCMatrix.Scaling(TGCVector3.One * 30)
         * TGCMatrix.Translation(pos);
 }
Пример #5
0
        /// <summary>
        ///     Actualiza la posicion de los vertices que componen las tapas y los vertices de las lineas laterales.
        /// </summary>
        private void updateDraw()
        {
            if (vertices == null)
            {
                vertices = new CustomVertex.PositionColored[END_CAPS_VERTEX_COUNT];
            }

            var color = this.color.ToArgb();

            //matriz que vamos a usar para girar el vector de dibujado
            var angle          = FastMath.TWO_PI / (END_CAPS_RESOLUTION / 4); // /4 ya que agregamos los bordes a la resolucion.
            var upVector       = TGCVector3.Up;
            var rotationMatrix = TGCMatrix.RotationAxis(upVector, angle);

            //vector de dibujado
            var n = new TGCVector3(1, 0, 0);

            //array donde guardamos los puntos dibujados
            var draw = new TGCVector3[vertices.Length];

            for (var i = 0; i < END_CAPS_VERTEX_COUNT / 4; i += 4)
            {
                //vertice inicial de la tapa superior
                draw[i] = upVector + n;
                //vertice inicial de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 4 + i] = -upVector + n;
                //vertice inicial del borde de la tapa superior
                draw[END_CAPS_VERTEX_COUNT / 2 + i] = upVector + n;
                //vertice inicial del borde de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 2 + i + 1] = -upVector + n;

                //rotamos el vector de dibujado
                n.TransformNormal(rotationMatrix);

                //vertice final de la tapa superior
                draw[i + 1] = upVector + n;
                //vertice final de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 4 + i + 1] = -upVector + n;
                //vertice final del borde de la tapa superior
                draw[END_CAPS_VERTEX_COUNT / 2 + i + 2] = upVector + n;
                //vertice final del borde de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 2 + i + 3] = -upVector + n;
            }

            //rotamos y trasladamos los puntos, y los volcamos al vector de vertices
            var transformation = Transform;

            for (var i = 0; i < END_CAPS_VERTEX_COUNT; i++)
            {
                vertices[i] = new CustomVertex.PositionColored(TGCVector3.TransformCoordinate(draw[i], transformation),
                                                               color);
            }
        }
        /// <summary>
        ///     Actualiza la posicion de los vertices que componen las tapas
        ///     y los vertices de las lineas laterales. FIXME este update puede ser una transformacion solamente.
        /// </summary>
        private void updateDraw()
        {
            if (vertices == null)
            {
                vertices = new CustomVertex.PositionColored[END_CAPS_VERTEX_COUNT];
            }

            var color      = this.color.ToArgb();
            var zeroVector = center;

            //matriz que vamos a usar para girar el vector de dibujado
            var angle          = FastMath.TWO_PI / (END_CAPS_RESOLUTION / 4); // /4 ya que agregamos los bordes a la resolucion.
            var upVector       = HalfHeight;
            var rotationMatrix = TGCMatrix.RotationAxis(TGCVector3.Up, angle);

            //vector de dibujado
            var n = new TGCVector3(Radius, 0, 0);

            //array donde guardamos los puntos dibujados
            var draw = new TGCVector3[END_CAPS_VERTEX_COUNT];

            for (var i = 0; i < END_CAPS_VERTEX_COUNT / 4; i += 4)
            {
                //vertice inicial de la tapa superior
                draw[i] = zeroVector + upVector + n;
                //vertice inicial de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 4 + i] = zeroVector - upVector + n;
                //vertice inicial del borde de la tapa superior
                draw[END_CAPS_VERTEX_COUNT / 2 + i] = zeroVector + upVector + n;
                //vertice inicial del borde de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 2 + i + 1] = zeroVector - upVector + n;

                //rotamos el vector de dibujado
                n.TransformNormal(rotationMatrix);

                //vertice final de la tapa superior
                draw[i + 1] = zeroVector + upVector + n;
                //vertice final de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 4 + i + 1] = zeroVector - upVector + n;
                //vertice final del borde de la tapa superior
                draw[END_CAPS_VERTEX_COUNT / 2 + i + 2] = zeroVector + upVector + n;
                //vertice final del borde de la tapa inferior
                draw[END_CAPS_VERTEX_COUNT / 2 + i + 3] = zeroVector - upVector + n;
            }

            for (var i = 0; i < END_CAPS_VERTEX_COUNT; i++)
            {
                vertices[i] = new CustomVertex.PositionColored(draw[i], color);
            }
        }
Пример #7
0
        private void updateDraw()
        {
            //vectores utilizados para el dibujado
            var upVector = TGCVector3.Up;
            var n        = new TGCVector3(1, 0, 0);

            var capsResolution = END_CAPS_RESOLUTION;

            //matriz de rotacion del vector de dibujado
            var   angleStep      = FastMath.TWO_PI / capsResolution;
            var   rotationMatrix = TGCMatrix.RotationAxis(-upVector, angleStep);
            float angle          = 0;

            //transformacion que se le aplicara a cada vertice
            var transformation  = Transform;
            var bcInverseRadius = 1 / BoundingCylinder.Radius;

            //arrays donde guardamos los puntos dibujados
            var topCapDraw    = new TGCVector3[capsResolution];
            var bottomCapDraw = new TGCVector3[capsResolution];

            //variables temporales utilizadas para el texture mapping
            float u;

            for (var i = 0; i < capsResolution; i++)
            {
                //establecemos los vertices de las tapas
                topCapDraw[i]    = upVector + n * TopRadius * bcInverseRadius;
                bottomCapDraw[i] = -upVector + n * BottomRadius * bcInverseRadius;

                u = angle / FastMath.TWO_PI;

                //triangulos de la cara lateral (strip)
                sideTrianglesVertices[2 * i]     = new CustomVertex.PositionColoredTextured(topCapDraw[i], color, u, 0);
                sideTrianglesVertices[2 * i + 1] = new CustomVertex.PositionColoredTextured(bottomCapDraw[i], color, u, 1);

                //rotamos el vector de dibujado
                n.TransformNormal(rotationMatrix);
                angle += angleStep;
            }

            //cerramos la cara lateral
            sideTrianglesVertices[2 * capsResolution] = new CustomVertex.PositionColoredTextured(topCapDraw[0], color, 1,
                                                                                                 0);
            sideTrianglesVertices[2 * capsResolution + 1] = new CustomVertex.PositionColoredTextured(bottomCapDraw[0],
                                                                                                     color, 1, 1);
        }
Пример #8
0
        private void rotateFirstPerson(float headingDegrees, float pitchDegrees)
        {
            accumPitchDegrees += pitchDegrees;

            if (accumPitchDegrees > 90.0f)
            {
                pitchDegrees      = 90.0f - (accumPitchDegrees - pitchDegrees);
                accumPitchDegrees = 90.0f;
            }

            if (accumPitchDegrees < -90.0f)
            {
                pitchDegrees      = -90.0f - (accumPitchDegrees - pitchDegrees);
                accumPitchDegrees = -90.0f;
            }

            var heading = Geometry.DegreeToRadian(headingDegrees);
            var pitch   = Geometry.DegreeToRadian(pitchDegrees);

            TGCMatrix rotMtx;
            Vector4   result;

            // Rotate camera's existing x and z axes about the world y axis.
            if (heading != 0.0f)
            {
                rotMtx = TGCMatrix.RotationY(heading);

                result = TGCVector3.Transform(xAxis, rotMtx);
                xAxis  = new TGCVector3(result.X, result.Y, result.Z);

                result = TGCVector3.Transform(zAxis, rotMtx);
                zAxis  = new TGCVector3(result.X, result.Y, result.Z);
            }

            // Rotate camera's existing y and z axes about its existing x axis.
            if (pitch != 0.0f)
            {
                rotMtx = TGCMatrix.RotationAxis(xAxis, pitch);

                result = TGCVector3.Transform(yAxis, rotMtx);
                yAxis  = new TGCVector3(result.X, result.Y, result.Z);

                result = TGCVector3.Transform(zAxis, rotMtx);
                zAxis  = new TGCVector3(result.X, result.Y, result.Z);
            }
        }
Пример #9
0
        private void PerformDeathMove(float elapsedTime)
        {
            CharacterOnSight  = false;
            DeathTimeCounter -= elapsedTime;
            var RotationStep = FastMath.PI * 0.4f * elapsedTime;

            if (FastUtils.GreaterThan(AcumulatedZRotation, Constants.MaxZRotation))
            {
                return;
            }

            AcumulatedZRotation += RotationStep;
            TotalRotation       *= TGCMatrix.RotationAxis(director, RotationStep);
            Mesh.Transform       = TotalRotation * TGCMatrix.Translation(new TGCVector3(Body.CenterOfMassPosition));
            Body.WorldTransform  = Mesh.Transform.ToBulletMatrix();
            Body.LinearVelocity  = Constants.DirectorY.ToBulletVector3() * 200;
        }
Пример #10
0
        private void PerformStalkerMove(float elapsedTime, float speed, float rotationAngle, TGCVector3 rotationAxis)
        {
            CharacterOnSight = true;
            var actualDirector = -1 * director;

            EventTimeCounter -= elapsedTime;
            var RotationStep = FastMath.PI * 0.3f * elapsedTime;

            if (rotationAngle <= RotationStep)
            {
                return;
            }

            actualDirector.TransformCoordinate(TGCMatrix.RotationAxis(rotationAxis, RotationStep));
            var newRotation = TGCMatrix.RotationAxis(rotationAxis, RotationStep);

            TotalRotation *= newRotation;

            Mesh.Transform      = TotalRotation * TGCMatrix.Translation(new TGCVector3(Body.CenterOfMassPosition));
            Body.WorldTransform = Mesh.Transform.ToBulletMatrix();

            director            = -1 * actualDirector;
            Body.LinearVelocity = director.ToBulletVector3() * -speed;
        }
Пример #11
0
        public override void Render()
        {
            PreRender();

            //Si hacen clic con el mouse, ver si hay colision con el suelo
            if (Input.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 TGCVector3(newPosition.X, 30f, newPosition.Z);

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

            var speed = speedModifier.Value;

            //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 TGCVector3(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 * TGCMatrix.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();
        }
Пример #12
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);
        }
Пример #13
0
        private void PerformNormalMove(float elapsedTime, float speed, TGCVector3 headPosition)
        {
            time -= elapsedTime;
            ChangeDirectionTimeCounter -= elapsedTime;

            float XRotation = 0f, YRotation = 0f;
            var   meshPosition = GetMeshPosition();

            Terrain.world.InterpoledHeight(headPosition.X, headPosition.Z, out float floorHeight);
            var distanceToFloor = FastUtils.Distance(meshPosition.Y, floorHeight);

            var XRotationStep = FastMath.PI * 0.1f * elapsedTime;
            var YRotationStep = FastMath.PI * 0.03f * elapsedTime;

            if (FastUtils.LessThan(distanceToFloor, Constants.FishHeight.X - 40) && FastUtils.LessThan(acumulatedXRotation, Constants.MaxAxisRotation))
            {
                XRotation = XRotationStep;
            }
            else if (FastUtils.IsNumberBetweenInterval(distanceToFloor, Constants.FishHeight) && FastUtils.GreaterThan(acumulatedXRotation, 0.0012f))
            {
                XRotation = -XRotationStep;
            }

            if (FastUtils.GreaterThan(distanceToFloor, Constants.FishHeight.Y + 40) && FastUtils.GreaterThan(acumulatedXRotation, -Constants.MaxAxisRotation))
            {
                XRotation = -XRotationStep;
            }
            else if (FastUtils.IsNumberBetweenInterval(distanceToFloor, Constants.FishHeight) && FastUtils.LessThan(acumulatedXRotation, -0.0012f))
            {
                XRotation = XRotationStep;
            }

            if (ChangeDirectionTimeCounter <= 0)
            {
                if (FastUtils.LessThan(FastMath.Abs(acumulatedYRotation), Constants.MaxYRotation))
                {
                    YRotation = YRotationStep * RotationYSign();
                }
                else
                {
                    ChangeDirectionTimeCounter = Constants.CHANGE_DIRECTION_TIME;
                }
            }
            else
            {
                acumulatedYRotation = 0;
            }

            acumulatedXRotation += XRotation;
            acumulatedYRotation += YRotation;

            TGCMatrix rotation = TGCMatrix.Identity;

            if (XRotation != 0 || FastUtils.GreaterThan(FastMath.Abs(acumulatedXRotation), 0.0012f))
            {
                var rotationAxis = TGCVector3.Cross(TGCVector3.Up, director);
                director.TransformCoordinate(TGCMatrix.RotationAxis(rotationAxis, XRotation));
                rotation = TGCMatrix.RotationAxis(rotationAxis, XRotation);
                speed   /= 1.5f;
            }
            else if (YRotation != 0)
            {
                director.TransformCoordinate(TGCMatrix.RotationY(YRotation));
                rotation = TGCMatrix.RotationY(YRotation);
            }

            TotalRotation *= rotation;
            TGCMatrix traslation = TGCMatrix.Translation(meshPosition + director * -speed * elapsedTime);

            Mesh.Mesh.Transform = TGCMatrix.Scaling(Constants.Scale) * TotalRotation * traslation;
            Mesh.Mesh.BoundingBox.transform(Mesh.Mesh.Transform);
        }
Пример #14
0
        /// <summary>
        /// Rota en los deltas indicados.
        /// </summary>
        /// <param name="rotX"></param>
        /// <param name="rotY"></param>
        private void look(float rotX, float rotY)
        {
            // Controlar los limites de rotacion sobre X (pitch)

            absoluteRotationX += rotX;

            if (absoluteRotationX > maxTopAngle)
            {
                rotX = maxTopAngle - (absoluteRotationX - rotX);
                absoluteRotationX = maxTopAngle;
            }
            else if (absoluteRotationX < maxBottomAngle)
            {
                rotX = maxBottomAngle - (absoluteRotationX - rotX);
                absoluteRotationX = maxBottomAngle;
            }

            // rotar la camara
            //

            // \todo optimize ?
            TGCMatrix deltaRM =
                TGCMatrix.RotationAxis(xAxis, rotX) *
                TGCMatrix.RotationAxis(up, rotY);

            TGCVector4 result;

            result = TGCVector3.Transform(xAxis, deltaRM);
            xAxis  = new TGCVector3(result.X, result.Y, result.Z);

            result = TGCVector3.Transform(yAxis, deltaRM);
            yAxis  = new TGCVector3(result.X, result.Y, result.Z);

            result = TGCVector3.Transform(zAxis, deltaRM);
            zAxis  = new TGCVector3(result.X, result.Y, result.Z);

            // Recalcular las dependencias

            forward = TGCVector3.Cross(xAxis, up);

            forward.Normalize();
            if (Math.Abs(forward.X) > Math.Abs(forward.Y) + Math.Abs(forward.Z))
            {
                if (forward.X > 0)
                {
                    key_right   = 'S';
                    key_back    = 'A';
                    key_forward = 'D';
                    key_left    = 'W';
                }
                else
                {
                    key_right   = 'W';
                    key_back    = 'D';
                    key_forward = 'A';
                    key_left    = 'S';
                }


                //miro para adelante en x
            }
            else if (Math.Abs(forward.Y) > Math.Abs(forward.X) + Math.Abs(forward.Z))
            {
                //miro para adelante en Y
            }
            else
            {
                //Miro para adelante en z
                if (forward.Z > 0)
                {
                    key_right   = 'A';
                    key_back    = 'W';
                    key_forward = 'S';
                    key_left    = 'D';
                }
                else
                {
                    key_right   = 'D';
                    key_back    = 'S';
                    key_forward = 'W';
                    key_left    = 'A';
                }
            }

            target = eye + zAxis;

            rotationChanged = true;
        }
Пример #15
0
        private void PerformNormalMove(float elapsedTime, float speed, TGCVector3 headPosition)
        {
            CharacterOnSight = false;
            var XRotation = 0f;
            var YRotation = 0f;

            ChangeDirectionTimeCounter -= elapsedTime;
            Terrain.world.InterpoledHeight(headPosition.X, headPosition.Z, out float floorHeight);
            var distanceToFloor = Body.CenterOfMassPosition.Y - floorHeight;
            var XRotationStep   = FastMath.PI * 0.1f * elapsedTime;
            var YRotationStep   = FastMath.PI * 0.4f * elapsedTime;

            var        distanceToWater    = Constants.WATER_HEIGHT - floorHeight - 200;
            TGCVector2 sharkRangePosition =
                new TGCVector2(Constants.SHARK_HEIGHT.X, FastMath.Min(distanceToWater, Constants.SHARK_HEIGHT.Y));

            if (distanceToFloor < sharkRangePosition.X - 150 && AcumulatedXRotation < Constants.MaxAxisRotation)
            {
                XRotation = XRotationStep;
            }
            else if (FastUtils.IsNumberBetweenInterval(distanceToFloor, sharkRangePosition) && AcumulatedXRotation > 0.0012)
            {
                XRotation = -XRotationStep;
            }
            else if (distanceToFloor > sharkRangePosition.Y + 150 && AcumulatedXRotation > -Constants.MaxAxisRotation)
            {
                XRotation = -XRotationStep;
            }
            else if (FastUtils.IsNumberBetweenInterval(distanceToFloor, sharkRangePosition) && AcumulatedXRotation < -0.0012)
            {
                XRotation = XRotationStep;
            }

            if (ChangeDirectionTimeCounter <= 0)
            {
                if (FastMath.Abs(AcumulatedYRotation) < Constants.MaxYRotation)
                {
                    YRotation = YRotationStep * RotationYSign();
                }
                else
                {
                    ChangeDirectionTimeCounter = Constants.CHANGE_DIRECTION_TIME;
                }
            }
            else
            {
                AcumulatedYRotation = 0;
            }

            AcumulatedXRotation += XRotation;
            AcumulatedYRotation += YRotation;

            Body.ActivationState = ActivationState.ActiveTag;
            TGCMatrix rotation = TGCMatrix.Identity;

            if (XRotation != 0 || FastMath.Abs(AcumulatedXRotation) > 0.0012)
            {
                var rotationAxis = TGCVector3.Cross(TGCVector3.Up, director);
                director.TransformCoordinate(TGCMatrix.RotationAxis(rotationAxis, XRotation));
                rotation = TGCMatrix.RotationAxis(rotationAxis, XRotation);
                speed   /= 1.5f;
            }
            else if (YRotation != 0)
            {
                director.TransformCoordinate(TGCMatrix.RotationY(YRotation));
                rotation = TGCMatrix.RotationY(YRotation);
            }
            TotalRotation      *= rotation;
            Mesh.Transform      = TotalRotation * TGCMatrix.Translation(new TGCVector3(Body.CenterOfMassPosition));
            Body.WorldTransform = Mesh.Transform.ToBulletMatrix();
            Body.LinearVelocity = director.ToBulletVector3() * -speed;
        }
Пример #16
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);
            }
        }