public void colisionEntreAutos(List <TgcBox> obbsAuto, List <TgcBox> obbsOtroAuto, Jugador jugadorAuto, Auto auto, Auto otroAuto, TgcMesh meshAuto, TgcMesh meshOtroAuto, float elapsedTime) { TgcBoundingBox dDAuto = obbsAuto[0].BoundingBox; TgcBoundingBox dIAuto = obbsAuto[2].BoundingBox; TgcBoundingBox tDAuto = obbsAuto[1].BoundingBox; TgcBoundingBox tIAuto = obbsAuto[3].BoundingBox; TgcBoundingBox dDOtroAuto = obbsOtroAuto[0].BoundingBox; TgcBoundingBox dIOtroAuto = obbsOtroAuto[2].BoundingBox; TgcBoundingBox tDOtroAuto = obbsOtroAuto[1].BoundingBox; TgcBoundingBox tIOtroAuto = obbsOtroAuto[3].BoundingBox; if (!jugadorAuto.estaMarchaAtras()) { if (TgcCollisionUtils.testAABBAABB(tIAuto, dIOtroAuto) || TgcCollisionUtils.testAABBAABB(tIAuto, dDOtroAuto) || TgcCollisionUtils.testAABBAABB(tDAuto, dIOtroAuto) || TgcCollisionUtils.testAABBAABB(tDAuto, dDOtroAuto)) { //auto gira cierto angulo hacia izquierda y sube velocidad if (Math.Truncate(auto.velocidad) == 0) { auto.velocidad = 300; } else { auto.velocidad = Math.Abs(auto.velocidad * 0.5f); } //otroAuto reduce bastante velocidad y se lo traslada un poco hacia atrás para no seguir chocando meshOtroAuto.moveOrientedY(10 * otroAuto.velocidad * elapsedTime); //Lo hago "como que rebote un poco" para no seguir colisionando otroAuto.velocidad = -(otroAuto.velocidad * 0.3f); if (tiempoDeChoque == 0) { tiempoDeChoque = 5; } } else if (TgcCollisionUtils.testAABBAABB(tIOtroAuto, dIAuto) || TgcCollisionUtils.testAABBAABB(tIOtroAuto, dDAuto) || TgcCollisionUtils.testAABBAABB(tDOtroAuto, dIAuto) || TgcCollisionUtils.testAABBAABB(tDOtroAuto, dDAuto)) { //auto gira cierto angulo hacia derecha y sube velocidad if (Math.Truncate(otroAuto.velocidad) == 0) { otroAuto.velocidad = 300; } else { otroAuto.velocidad = Math.Abs(otroAuto.velocidad * 0.5f); } //otroAuto reduce bastante velocidad y se lo traslada un poco hacia atrás para no seguir chocando meshAuto.moveOrientedY(10 * auto.velocidad * elapsedTime); //Lo hago "como que rebote un poco" para no seguir colisionando auto.velocidad = -(auto.velocidad * 0.3f); if (tiempoDeChoque == 0) { tiempoDeChoque = 5; } } else if (TgcCollisionUtils.testAABBAABB(dIAuto, dIOtroAuto) || TgcCollisionUtils.testAABBAABB(dDAuto, dIOtroAuto) || TgcCollisionUtils.testAABBAABB(dIAuto, dDOtroAuto) || TgcCollisionUtils.testAABBAABB(dDAuto, dDOtroAuto)) { //auto se mueve hacia atrás y cambia de sentido 180° (rebota) if (Math.Truncate(auto.velocidad) == 0) { auto.velocidad = -300; } else { meshAuto.moveOrientedY(10 * auto.velocidad * elapsedTime); //Lo hago "como que rebote un poco" para no seguir colisionando auto.velocidad = -(auto.velocidad * 0.3f); //Lo hago ir atrás un tercio de velocidad de choque } //otroAuto hace lo mismo que el auto meshOtroAuto.moveOrientedY(10 * otroAuto.velocidad * elapsedTime); //Lo hago "como que rebote un poco" para no seguir colisionando otroAuto.velocidad = -(otroAuto.velocidad * 0.3f); //Lo hago ir atrás un tercio de velocidad de choque if (tiempoDeChoque == 0) { tiempoDeChoque = 5; } } } else { if (TgcCollisionUtils.testAABBAABB(tIAuto, dIOtroAuto) || TgcCollisionUtils.testAABBAABB(tIAuto, dDOtroAuto) || TgcCollisionUtils.testAABBAABB(tDAuto, dIOtroAuto) || TgcCollisionUtils.testAABBAABB(tDAuto, dDOtroAuto)) { //ambos autos rebotan (cambia sentido 180°) if (Math.Truncate(auto.velocidad) == 0) { auto.velocidad = 300; } else { auto.velocidad = Math.Abs(auto.velocidad * 0.5f); } meshOtroAuto.moveOrientedY(10 * otroAuto.velocidad * elapsedTime); //Lo hago "como que rebote un poco" para no seguir colisionando otroAuto.velocidad = -(otroAuto.velocidad * 0.3f); //Lo hago ir atrás un tercio de velocidad de choque if (tiempoDeChoque == 0) { tiempoDeChoque = 5; } } else if (TgcCollisionUtils.testAABBAABB(tIAuto, tIOtroAuto) || TgcCollisionUtils.testAABBAABB(tIAuto, tDOtroAuto) || TgcCollisionUtils.testAABBAABB(tDAuto, tIOtroAuto) || TgcCollisionUtils.testAABBAABB(tDAuto, tDOtroAuto)) { //rebota auto meshAuto.moveOrientedY(10 * auto.velocidad * elapsedTime); //Lo hago "como que rebote un poco" para no seguir colisionando auto.velocidad = -(auto.velocidad * 0.3f); //Lo hago ir atrás un tercio de velocidad de choque if (tiempoDeChoque == 0) { tiempoDeChoque = 5; } } } }
public override void init() { hud = new HUD(); hud.init(); Microsoft.DirectX.Direct3D.Device d3dDevice = GuiController.Instance.D3dDevice; //Crear Sprite sprite = new TgcSprite(); sprite.Texture = TgcTexture.createTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Inicio\\imagenInicio.jpg"); //Ubicarlo centrado en la pantalla Size screenSize = GuiController.Instance.Panel3d.Size; Size textureSize = sprite.Texture.Size; sprite.Position = new Vector2(0, 0); sprite.Scaling = new Vector2((float)screenSize.Width / textureSize.Width, (float)screenSize.Height / textureSize.Height + 0.01f); //Texturas para particulas texturaHumo = TgcTexture.createTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Particulas\\Textures\\humo.png"); texturaFuego = TgcTexture.createTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Particulas\\Textures\\fuego.png"); Vector3 centerHumo = new Vector3(0, 0, 0); Vector3 sizeHumo = new Vector3(7, 3, 10); humo = TgcBox.fromSize(centerHumo, sizeHumo, texturaHumo); humo.AlphaBlendEnable = true; fuego = TgcBox.fromSize(centerHumo, sizeHumo, texturaFuego); fuego.AlphaBlendEnable = true; cantidadDeNitro = 100f; // cosas del tiempo tiempoTrans = 100f; //tiempo transcurrido desde el defasaje de rotacion de camara y rotacion del mesh segundosAuxiliares = 1; //En este ejemplo primero cargamos una escena 3D entera. TgcSceneLoader loader = new TgcSceneLoader(); //Luego cargamos otro modelo aparte que va a hacer el objeto que controlamos con el teclado TgcScene scene1 = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\autoRojo-TgcScene.xml"); TgcScene scene2 = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\Auto_Rueda_Derecha-TgcScene.xml"); TgcScene scene3 = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\Auto_Rueda_Derecha-TgcScene.xml"); TgcScene scene4 = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\Auto_Rueda_Izquierda-TgcScene.xml"); TgcScene scene5 = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\Auto_Rueda_Izquierda-TgcScene.xml"); scenePista = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\pistaentera-TgcScene.xml"); TgcScene sceneAutoIA = loader.loadSceneFromFile(GuiController.Instance.AlumnoEjemplosMediaDir + "TheC#\\Auto\\\\autoVerde-TgcScene.xml"); //cargo obbs objetosColisionables = new List <TgcObb>(); /* Esto de aca comentado sirve para cuando se agregan nuevos meshes a pistaentera-TgcScene.xml entonces * obtiene su OBB de los AABB de los meshes, esta comentado porque el archivo .xml esta actualizado * y aparte detiene mucho su inicio * foreach (TgcMesh mesh in scenePista.Meshes) * { * if (mesh.Name.Contains("borde") || mesh.Name.Contains("col") || mesh.Name.Contains("Patrullero")) * { * Vector3[] vertices = mesh.getVertexPositions(); * objetosColisionables.Add(TgcObb.computeFromPoints(vertices)); * } * } * guardarObbs(objetosColisionables);*/ objetosColisionables = cargarObbObjetos(); posteDeSemaforoOBB = cargarobbParticular(new Vector3(-1193.647f, -649.2448f, 948.8185f), new Vector3(-1147f, 596.6053f, 1051.182f)); objetosColisionables.Add(posteDeSemaforoOBB); //Solo nos interesa el primer modelo de estas escenas autoMesh = scene1.Meshes[0]; ruedaDerechaDelanteraMesh = scene2.Meshes[0]; ruedaDerechaTraseraMesh = scene3.Meshes[0]; ruedaIzquierdaDelanteraMesh = scene4.Meshes[0]; ruedaIzquierdaTraseraMesh = scene5.Meshes[0]; //creo el mesh del auto de IA meshAutoIA = sceneAutoIA.Meshes[0]; //creo la lista de ruedas ruedas = new List <TgcViewer.Utils.TgcSceneLoader.TgcMesh> { ruedaDerechaDelanteraMesh, ruedaDerechaTraseraMesh, ruedaIzquierdaDelanteraMesh, ruedaIzquierdaTraseraMesh }; //creo la lineas de frenado for (int i = 0; i < 4; i++) { lineaDeFrenado[i] = new LineaDeFrenado(12, 25, 3, 250, Color.Black); } //posicion del auto autoMesh.Position = new Vector3(-278f, 0f, 281f); //posiciones relativas al auto dx = new List <float> { 45, -45, -45, 45 }; dy = new List <float> { -61, 71, -61, 71 }; //posiciones relativas al auto para los box de colision entre autos dxAColision = new List <float> { 20, -20, -20, 20 }; dyAColision = new List <float> { -35, 45, -35, 45 }; //posiciono al autoIA meshAutoIA.Position = new Vector3(211f, 0f, 216f); //Vamos a utilizar la cámara en 3ra persona para que siga al objeto principal a medida que se mueve GuiController.Instance.ThirdPersonCamera.Enable = true; GuiController.Instance.ThirdPersonCamera.RotationY = 300; GuiController.Instance.ThirdPersonCamera.setCamera(autoMesh.Position, 200, 500); GuiController.Instance.BackgroundColor = Color.Black;// Black; //Le asigno su oriented bounding box que me permite rotar la caja de colisiones (no así bounding box) oBBAuto = TgcObb.computeFromAABB(autoMesh.BoundingBox); oBBAutoIa = TgcObb.computeFromAABB(meshAutoIA.BoundingBox); //creo al auto y al jugador auto = new Auto(110, ruedas); jugador = new Jugador(auto); //creo al auto del IA y al IA autoIA = new Auto(110, ruedas); jugadorIA = new IA(autoIA, new Vector3(0, 0, 0)); //Inicializo el circuito, tanto para la persona como para la IA posicionesPuntosDeControl = new List <Vector3> { new Vector3(360, 20, 2634), new Vector3(-1040, 20, 5524), new Vector3(-1749, 20, 11113), new Vector3(-4484, 20, 14460), new Vector3(-7377, 20, 16890), new Vector3(-12790, 20, 17407), new Vector3(-15308, 20, 12649), new Vector3(-15153, 20, 8531), new Vector3(-16068, 20, 7030), new Vector3(-16342, 20, 4149), new Vector3(-16222, 20, -1083), new Vector3(-14986, 20, -3020), new Vector3(-15734, 20, -8568), new Vector3(-15281, 20, -12097), new Vector3(-12022, 20, -15335), new Vector3(-6142, 20, -14446), new Vector3(-4411, 20, -11766), new Vector3(-2016, 20, -8940), new Vector3(-1287, 20, -4142), new Vector3(20, 20, -2566) }; posicionesPuntosDeControlDeIA = new List <Vector3> { new Vector3(360, 20, 2634), new Vector3(-1040, 20, 5524), new Vector3(-1749, 20, 11113), new Vector3(-4484, 20, 14460), new Vector3(-7377, 20, 16890), new Vector3(-12790, 20, 17407), new Vector3(-15308, 20, 12649), new Vector3(-15153, 20, 8531), new Vector3(-16068, 20, 7030), new Vector3(-16342, 20, 4149), new Vector3(-16222, 20, -1083), new Vector3(-14986, 20, -3020), new Vector3(-15734, 20, -8568), new Vector3(-15281, 20, -12097), new Vector3(-12022, 20, -15335), new Vector3(-6142, 20, -14446), new Vector3(-4411, 20, -11766), new Vector3(-2016, 20, -8940), new Vector3(-1287, 20, -4142), new Vector3(20, 20, -2566) }; for (int i = 0; i < posicionesPuntosDeControl.Count; i++) { TgcCylinder unCilindro = new TgcCylinder(posicionesPuntosDeControl[i], 130, 30); trayecto.Add(unCilindro); unCilindro = new TgcCylinder(posicionesPuntosDeControlDeIA[i], 130, 30); trayectoDeIA.Add(unCilindro); } //Asigno los obb que me permiten detectar las colisiones entre los autos obbsAuto.Add(TgcBox.fromSize(new Vector3(autoMesh.Position.X - 100, 15, autoMesh.Position.Z + 100), new Vector3(65, 65, 65), texturaFuego)); obbsAuto.Add(TgcBox.fromSize(new Vector3(autoMesh.Position.X - 100, 15, autoMesh.Position.Z - 100), new Vector3(65, 65, 65), texturaHumo)); obbsAuto.Add(TgcBox.fromSize(new Vector3(autoMesh.Position.X + 100, 15, autoMesh.Position.Z + 100), new Vector3(65, 65, 65), texturaFuego)); obbsAuto.Add(TgcBox.fromSize(new Vector3(autoMesh.Position.X + 100, 15, autoMesh.Position.Z - 100), new Vector3(65, 65, 65), texturaFuego)); obbsOtroAuto.Add(TgcBox.fromSize(new Vector3(meshAutoIA.Position.X - 100, 15, meshAutoIA.Position.Z + 100), new Vector3(65, 65, 65), texturaFuego)); obbsOtroAuto.Add(TgcBox.fromSize(new Vector3(meshAutoIA.Position.X - 100, 15, meshAutoIA.Position.Z - 100), new Vector3(65, 65, 65), texturaHumo)); obbsOtroAuto.Add(TgcBox.fromSize(new Vector3(meshAutoIA.Position.X + 100, 15, meshAutoIA.Position.Z + 100), new Vector3(65, 65, 65), texturaFuego)); obbsOtroAuto.Add(TgcBox.fromSize(new Vector3(meshAutoIA.Position.X + 100, 15, meshAutoIA.Position.Z - 100), new Vector3(65, 65, 65), texturaFuego)); /////////////TEXTOS/////////////////////// textPuntosDeControlAlcanzados = new TgcText2d(); textPuntosDeControlAlcanzados.Position = new Point(0, 50); textPuntosDeControlAlcanzados.Text = "Puntos De Control Alcanzados = "; textPuntosDeControlAlcanzados.Color = Color.White; textFPS = new TgcText2d(); textFPS.Position = new Point((screenSize.Width / (-2)), 0); textFPS.Text = "FPS: "; textFPS.Color = Color.Yellow; textGanaste = new TgcText2d(); textGanaste.Position = new Point(0, 200); textGanaste.Color = Color.LightGreen; textPerdiste = new TgcText2d(); textPerdiste.Position = new Point(0, 200); textPerdiste.Text = "Perdiste y lograste "; textPerdiste.Color = Color.Red; textPerdiste.Size = new Size(900, 700); textPosicionDelAutoActual = new TgcText2d(); textPosicionDelAutoActual.Text = "Posicion del auto actual = "; textPosicionDelAutoActual.Color = Color.White; textPosicionDelAutoActual.Position = new Point(100, 450); this.horaInicio = DateTime.Now; textTiempo = new TgcText2d(); textTiempo.Position = new Point(screenSize.Width / 2 - 50, 20); textTiempo.Text = "10"; textTiempo.changeFont(new System.Drawing.Font("TimesNewRoman", 23, FontStyle.Bold | FontStyle.Bold)); textTiempo.Color = Color.Red; stringTiempo = new TgcText2d(); stringTiempo.Position = new Point(screenSize.Width / 2 - 130, 20); stringTiempo.Text = "Tiempo: "; stringTiempo.changeFont(new System.Drawing.Font("TimesNewRoman", 23, FontStyle.Bold | FontStyle.Bold)); stringTiempo.Color = Color.Red; textIngreseTecla = new TgcText2d(); textIngreseTecla.Text = " Utilize las flechas o W,A,S,D para moverse. \n Shift o control izquierdo para activar el nitro \n M para cambiar la camara \n TAB mirar para atrás \n Ingrese barra espaciadora para comenzar "; textIngreseTecla.Position = new Point(150, 290); textIngreseTecla.Align = TgcText2d.TextAlign.LEFT; textIngreseTecla.changeFont(new System.Drawing.Font("TimesNewRoman", 23, FontStyle.Bold | FontStyle.Bold)); textIngreseTecla.Color = Color.White; textIngreseTeclaSombra = new TgcText2d(); textIngreseTeclaSombra.Text = " Utilize las flechas o W,A,S,D para moverse. \n Shift o control izquierdo para activar el nitro \n M para cambiar la camara \n TAB mirar para atrás \n Ingrese barra espaciadora para comenzar "; textIngreseTeclaSombra.Position = new Point(149, 288); textIngreseTeclaSombra.Align = TgcText2d.TextAlign.LEFT; textIngreseTeclaSombra.changeFont(new System.Drawing.Font("TimesNewRoman", 23, FontStyle.Bold | FontStyle.Bold)); textIngreseTeclaSombra.Color = Color.Black; textoVelocidad.inicializarTextoVelocidad(auto.velocidad); ///////////////MODIFIERS////////////////// GuiController.Instance.Modifiers.addFloat("velocidadMaxima", 1000, 7000, 1800f); GuiController.Instance.Modifiers.addBoolean("jugarConTiempo", "jugar con tiempo:", true); GuiController.Instance.Modifiers.addBoolean("motionBlurFlag", "Motion Blur", false); //////////////Reflejo de luz en auto//////////////// reflejo = new ReflejoLuzEnAuto(autoMesh); //contador de puntos de control contadorDeActivacionesDePuntosDeControl = 0; contadorDeActivacionesDePuntosDeControlDeIA = 0; //flag de victoria gano = false; List <TgcMesh> autoIAList = new List <TgcMesh>(); autoIAList.Add(meshAutoIA); //motionBlur = new MotionBlur(scenePista.Meshes); //motionBlur.motionBlurInit(0); musica.inicializar(); motionBlur = new MotionBlur(scenePista.Meshes); motionBlur.motionBlurInit(0); emisorHumo = new EmisorHumo(); emisorHumo.crearQuads(20); }