예제 #1
0
        public bool MoveTowards(Vector3 targetPos, List <Core.BoundingVolumes.TgcBoundingAxisAlignBox> obstaculos, float ElapsedTime)
        {
            var  targetDistance = targetPos - Position;
            bool ret            = false;

            //El monstruo solo se mueve en el plano XZ
            targetDistance.Y = 0f;

            //Normalizar distancia para obtener versor direccion
            var targetDirection = Vector3.Normalize(targetDistance);

            //Obtener movimiento
            var movement = targetDirection * velocidad * ElapsedTime;

            //Si el movimiento es mayor que la distancia al objetivo lo reemplazamos por la misma para no pasarnos
            if (movement.LengthSq() >= targetDistance.LengthSq())
            {
                movement = targetDistance;
                ret      = true;
            }

            //Se obtiene el angulo de rotacion horizontal
            var targetAngleH = FastMath.Atan2(targetDirection.X, targetDirection.Z);

            //Se obtiene el angulo de rotacion vertical a partir de la altura del versor director(ya no es relevante)
            //var targetAngleV = FastMath.Asin(targetDirection.Y);

            var originalRot = mesh.Rotation;

            var originalPos = Position;

            if (chasingPlayer)
            {
                //Rotamos el mesh, se suma PI para que de la cara y no la espalda
                mesh.Rotation = new Vector3(0, targetAngleH + FastMath.PI, 0);
                move(movement, obstaculos);
            }
            else
            {
                //si tamos tranca rotamos lentamente a la dirección a la que queremos ir

                //sacamos la distancia entre angulos
                float distancia = AngleDistance(mesh.Rotation.Y, targetAngleH + FastMath.PI);
                mesh.Rotation = new Vector3(0f, Approach(mesh.Rotation.Y, mesh.Rotation.Y + distancia, rotation_speed), 0f);

                //si ya estamos como queremos nos movemos
                if (FastMath.EpsilonEquals(AngleDistance(mesh.Rotation.Y, targetAngleH + FastMath.PI), 0f))
                {
                    move(movement, obstaculos);
                }
            }
            return(ret);
        }