private static void CrearPista() { var r = new System.Random(); int m = 500; Punto[] puntos = new Punto[m]; for (int i = 0; i < m; i++) { //COORDENADAS POLARES puntos[i] = new Punto(Convert.ToSingle(r.NextDouble() * 125), Convert.ToSingle(r.NextDouble() * 2 * Math.PI), 1); } List <Punto> l = new List <Punto>(); for (int i = 0; i < puntos.Length; i++) { l.Add(puntos[i]); } l.Add(puntos[0]); puntos = l.ToArray(); ConvexHull ch = new ConvexHull(); List <float> ll = new List <float>(); for (int i = 0; i < puntos.Length; i++) { ll.Add(puntos[i].x); ll.Add(puntos[i].y); } FloatArray fa = new FloatArray(ll.ToArray()); FloatArray contorno = ch.computePolygon(fa, false); float[] xx; float[] yy; float[] xs, ys; int n = 1000; desglosar(contorno, out xx, out yy); Punto[] convexo = Punto.CrearArregloDesdeArreglos(xx, yy); CubicSpline.FitParametric(xx, yy, n, out xs, out ys, 1, -1, 1, -1); ////////////////////////////////////// AGREGAR DIFICULTAD //agregarDificultad(ref convexo, 30); deformar(ref convexo); xx = Punto.getAbscisas(convexo); yy = Punto.getOrdenadas(convexo); CubicSpline.FitParametric(xx, yy, n, out xs, out ys); }
public void CrearPista(Vector3 a) { var r = new System.Random(); int m = 500; Punto[] puntos = new Punto[m]; Punto Centro = new Punto(a.x, a.z); for (int i = 0; i < m; i++) { //COORDENADAS POLARES argumentos en radianes puntos[i] = new Punto(125 * UnityEngine.Random.value, UnityEngine.Random.value * 2 * Mathf.PI, 1); puntos[i].x += Centro.x; puntos[i].y += Centro.y; } List <Punto> l = new List <Punto>(); for (int i = 0; i < puntos.Length; i++) { l.Add(puntos[i]); } l.Add(puntos[0]); puntos = l.ToArray(); ConvexHull ch = new ConvexHull(); List <float> ll = new List <float>(); for (int i = 0; i < puntos.Length; i++) { ll.Add(puntos[i].x); ll.Add(puntos[i].y); } FloatArray fa = new FloatArray(ll.ToArray()); FloatArray contorno = ch.computePolygon(fa, false); float[] xx; float[] yy; float[] xs, ys; desglosarFloatArrayEnVectoresNormales(contorno, out xx, out yy); Punto[] convexo = Punto.CrearArregloDesdeArreglos(xx, yy); ////////////////////////////////////// AGREGAR DIFICULTAD float ancho = UnityEngine.Random.Range(5f, 9f); ancho = (isSeguidorDeLinea) ? ancho / 3 : ancho; Punto[] convexo2 = new Punto[convexo.Length * 2]; if (isDeforme) { deformar(ref convexo); } separarPuntos(ref convexo); xx = Punto.getAbscisas(convexo); yy = Punto.getOrdenadas(convexo); CubicSpline.FitParametric(xx, yy, n, out xs, out ys, 1, -1, 1, -1); ///////////////////////////////////////////////////////////////////////// /* * en xs y en ys estan las posiciones x,y de cada punto de la pista */ LS.Add(Instantiate(this.suelo, new Vector3(0, -1, 0), Quaternion.Euler(Vector3.zero), this.transform)); float[] angulosEnGrados = new float[n]; for (int i = 0; i < xs.Length - 1; i++) { angulosEnGrados[i] = 180 * Mathf.Atan2(ys[i + 1] - ys[i], -xs[i + 1] + xs[i]) / Mathf.PI; } IList <Punto> listaRoja = new List <Punto>(); IList <Punto> listaBuena = new List <Punto>(); Punto[] puntosInteriores; Punto[] puntosLinea; int offset = r.Next(); Vector3 pos, pos2 = new Vector3(); Quaternion rot; float offsetPos; float offsetBuenas;// = UnityEngine.Random.Range(0.5f, ancho - 0.5f); for (int i = 0; i < xs.Length - 1; i++) { pos = new Vector3(xs[i], 0, ys[i]); rot = Quaternion.Euler(0, angulosEnGrados[i], 0); pared.transform.position = pos; pared.transform.rotation = rot; LPA.Add(Instantiate(pared, pos, rot, this.transform)); if (isSeguidorDeLinea) { offsetBuenas = ancho / 4 * Mathf.Sin(i / n) - ancho / 4; pared.transform.Translate((ancho / 2 - offsetBuenas) * -Vector3.back, Space.Self); listaBuena.Add(new Punto(pared.transform.position)); pared.transform.Translate((ancho - offsetBuenas) * -Vector3.back, Space.Self); } if (i % 10 == 0) { offsetPos = UnityEngine.Random.Range(0, ancho / 2); pared.transform.Translate(offsetPos * -Vector3.back, Space.Self); pos2 = pared.transform.position; pared.transform.Translate((ancho - offsetPos) * -Vector3.back, Space.Self); listaRoja.Add(new Punto(pared.transform.position)); } else { pared.transform.Translate((ancho) * -Vector3.back, Space.Self); listaRoja.Add(new Punto(pared.transform.position)); } if (i % 5 == 0) { pared.transform.Translate(ancho / 2 * Vector3.back, Space.Self); checkpoint.transform.rotation = Quaternion.Euler(0, angulosEnGrados[i], 0); checkpoint.transform.position = pared.transform.position; checkpoint.transform.localScale = new Vector3(0.1f, checkpoint.transform.localScale.y, ancho); LCH.Add(Instantiate(checkpoint, pared.transform.position, Quaternion.Euler(0, angulosEnGrados[i], 0), this.transform)); } if ((UnityEngine.Random.value > 0.65f) && !isSeguidorDeLinea) { powerUp.transform.position = pos; powerUp.transform.rotation = rot; powerUp.transform.Translate(UnityEngine.Random.Range(1, ancho - 1) * Vector3.forward, Space.Self); LPU.Add(Instantiate(powerUp, this.transform)); } if (isObstaculos) { if (UnityEngine.Random.value > 0.988f) { obstaculo.transform.position = pos; obstaculo.transform.rotation = rot; obstaculo.transform.Translate(UnityEngine.Random.Range(1, ancho - 1) * Vector3.forward, Space.Self); LO.Add(Instantiate(obstaculo, this.transform)); } } posIni[i / (EvolutionManager.CarCount / 10)] = pos2; EvolutionManager.ini = posIni[i / (EvolutionManager.CarCount / 10)]; } puntosLinea = listaBuena.ToArray(); if (listaBuena.Count > 0) { puntosLinea[puntosLinea.Length - 1] = puntosLinea[0]; arreglarAngulos(ref puntosLinea); } puntosInteriores = listaRoja.ToArray(); puntosInteriores[puntosInteriores.Length - 1] = puntosInteriores[0]; arreglarAngulos(ref puntosInteriores); float[] xx2, xx3; float[] yy2, yy3; float[] xs2, ys2, xs3, ys3; float[] angulosEnGrados2 = new float[n2]; float[] angulosEnGrados3 = new float[n2]; xx2 = Punto.getAbscisas(puntosInteriores); yy2 = Punto.getOrdenadas(puntosInteriores); if (isSeguidorDeLinea) { xx3 = Punto.getAbscisas(puntosLinea); yy3 = Punto.getOrdenadas(puntosLinea); CubicSpline.FitParametric(xx3, yy3, n2, out xs3, out ys3, 1, -1, 1, -1); for (int i = 0; i < xx3.Length; i++) { angulosEnGrados3[i] = 180 * Mathf.Atan2(ys3[i + 1] - ys3[i], -xs3[i + 1] + xs3[i]) / Mathf.PI; Linea.transform.rotation = Quaternion.Euler(0, angulosEnGrados3[i], 0); Linea.transform.position = new Vector3(xs3[i], 0, ys3[i]); LB.Add(Instantiate(Linea, new Vector3(xs3[i], 0, ys3[i]), Quaternion.Euler(0, angulosEnGrados3[i], 0), this.transform)); } } CubicSpline.FitParametric(xx2, yy2, n2, out xs2, out ys2, 1, -1, 1, -1); for (int i = 0; i < xs2.Length - 1; i++) { angulosEnGrados2[i] = 180 * Mathf.Atan2(ys2[i + 1] - ys2[i], -xs2[i + 1] + xs2[i]) / Mathf.PI; } for (int i = 0; i < xs2.Length - 1; i++) { paredRoja.transform.rotation = Quaternion.Euler(0, angulosEnGrados2[i], 0); paredRoja.transform.position = new Vector3(xs2[i], 0, ys2[i]); LPR.Add(Instantiate(paredRoja, new Vector3(xs2[i], 0, ys2[i]), Quaternion.Euler(0, angulosEnGrados2[i], 0), this.transform)); } }
public void CrearPista(Vector3 a) { var r = new System.Random(); int m = 500; Punto[] puntos = new Punto[m]; Punto Centro = new Punto(a.x, a.z); for (int i = 0; i < m; i++) { //COORDENADAS POLARES argumentos en radianes puntos[i] = new Punto(125 * UnityEngine.Random.value, UnityEngine.Random.value * 2 * Mathf.PI, 1); puntos[i].x += Centro.x; puntos[i].y += Centro.y; } List <Punto> l = new List <Punto>(); for (int i = 0; i < puntos.Length; i++) { l.Add(puntos[i]); } l.Add(puntos[0]); puntos = l.ToArray(); ConvexHull ch = new ConvexHull(); List <float> ll = new List <float>(); for (int i = 0; i < puntos.Length; i++) { ll.Add(puntos[i].x); ll.Add(puntos[i].y); } FloatArray fa = new FloatArray(ll.ToArray()); FloatArray contorno = ch.computePolygon(fa, false); float[] xx; float[] yy; float[] xs, ys; desglosarFloatArrayEnVectoresNormales(contorno, out xx, out yy); Punto[] convexo = Punto.CrearArregloDesdeArreglos(xx, yy); ////////////////////////////////////// AGREGAR DIFICULTAD float ancho = UnityEngine.Random.Range(5f, 9f); Punto[] convexo2 = new Punto[convexo.Length * 2]; deformar(ref convexo); separarPuntos(ref convexo); xx = Punto.getAbscisas(convexo); yy = Punto.getOrdenadas(convexo); CubicSpline.FitParametric(xx, yy, n, out xs, out ys, 1, -1, 1, -1); ///////////////////////////////////////////////////////////////////////// /* * en xs y en ys estan las posiciones x,y de cada punto de la pista */ LS.Add(Instantiate(this.suelo, new Vector3(0, -1, 0), Quaternion.Euler(Vector3.zero), this.transform)); float[] angulosEnGrados = new float[n]; for (int i = 0; i < xs.Length - 1; i++) { angulosEnGrados[i] = 180 * Mathf.Atan2(ys[i + 1] - ys[i], -xs[i + 1] + xs[i]) / Mathf.PI; } IList <Punto> listaRoja = new List <Punto>(); IList <Punto> listaCheckpoint = new List <Punto>(); Punto[] puntosInteriores; int offset = r.Next(); Vector3 pos, pos2 = new Vector3(); Quaternion rot; for (int i = 0; i < xs.Length - 1; i++) { pos = new Vector3(xs[i], 0, ys[i]); rot = Quaternion.Euler(0, angulosEnGrados[i], 0); pared.transform.position = pos; pared.transform.rotation = rot; LPA.Add(Instantiate(pared, pos, rot, this.transform)); if (i % 10 == 0) { pared.transform.Translate(ancho / 2 * -Vector3.back, Space.Self); pos2 = pared.transform.position; pared.transform.Translate(ancho / 2 * -Vector3.back, Space.Self); listaRoja.Add(new Punto(pared.transform.position)); } else { pared.transform.Translate((ancho + Mathf.Sin(i / 10)) * -Vector3.back, Space.Self); listaRoja.Add(new Punto(pared.transform.position)); } if (i % 5 == 0) { pared.transform.Translate(ancho / 2 * Vector3.back, Space.Self); checkpoint.transform.rotation = Quaternion.Euler(0, angulosEnGrados[i], 0); checkpoint.transform.position = pared.transform.position; checkpoint.transform.localScale = new Vector3(0.1f, checkpoint.transform.localScale.y, ancho); LCH.Add(Instantiate(checkpoint, pared.transform.position, Quaternion.Euler(0, angulosEnGrados[i], 0), this.transform)); } if (UnityEngine.Random.value > 0.97f) { obstaculo.transform.position = pos; obstaculo.transform.rotation = rot; obstaculo.transform.Translate(UnityEngine.Random.Range(1, ancho - 1) * Vector3.forward, Space.Self); LO.Add(Instantiate(obstaculo, this.transform)); } posIni[i / 10] = pos2; EvolutionManager.ini = posIni[i / 10]; } puntosInteriores = listaRoja.ToArray(); puntosInteriores[puntosInteriores.Length - 1] = puntosInteriores[0]; arreglarAngulos(ref puntosInteriores); Punto[] PC = listaCheckpoint.ToArray(); float[] xx2; float[] yy2; float[] xs2, ys2; float[] angulosEnGrados2 = new float[n2]; float[] angulosEnGrados3 = new float[n / 10]; xx2 = Punto.getAbscisas(puntosInteriores); yy2 = Punto.getOrdenadas(puntosInteriores); CubicSpline.FitParametric(xx2, yy2, n2, out xs2, out ys2, 1, -1, 1, -1); for (int i = 0; i < xs2.Length - 1; i++) { angulosEnGrados2[i] = 180 * Mathf.Atan2(ys2[i + 1] - ys2[i], -xs2[i + 1] + xs2[i]) / Mathf.PI; } for (int i = 0; i < xs2.Length - 1; i++) { paredRoja.transform.rotation = Quaternion.Euler(0, angulosEnGrados2[i], 0); paredRoja.transform.position = new Vector3(xs2[i], 0, ys2[i]); LPR.Add(Instantiate(paredRoja, new Vector3(xs2[i], 0, ys2[i]), Quaternion.Euler(0, angulosEnGrados2[i], 0), this.transform)); } //Cuando lee esta variable, los autos van a estar creados para una pista vieja ////////////////////////////////////7 }