/*************************************** MOVIMIENTO ************************************************/ public virtual void calcularTraslacionYRotacion(float elapsedTime, TerrenoSimple agua, float time, Vector3 lastPosition) { Vector3 lastPositionEnemy = EjemploAlumno.Instance.shipContrincante.getPosition(); if (input.keyDown(Key.Left)) { anguloRotacion -= elapsedTime * ROTATION_SPEED; rotacion = Matrix.RotationY(anguloRotacion); } else if (input.keyDown(Key.Right)) { anguloRotacion += elapsedTime * ROTATION_SPEED; rotacion = Matrix.RotationY(anguloRotacion); } if (input.keyDown(Key.Up)) { movementSpeed = Math.Min(movementSpeed + ESCALON_VEL, VEL_MAXIMA); } else if (input.keyDown(Key.Down)) { movementSpeed = Math.Max(movementSpeed - ESCALON_VEL, 0); } movZ -= Convert.ToSingle(movementSpeed * Math.Cos(anguloRotacion) * elapsedTime); movX -= Convert.ToSingle(movementSpeed * Math.Sin(anguloRotacion) * elapsedTime); movY = agua.aplicarOlasA(getPosition(), time).Y + AltoBote / 2; administrarColisiones(lastPosition, new Vector3(movX, movY, movZ), lastPositionEnemy); }
// Utilidad para convertir directorios de texturas en texturas public static Texture toTexture(string path) { TerrenoSimple conversor = new TerrenoSimple(); conversor.loadTexture(path); return(conversor.terrainTexture); }
private void renderJuego(float elapsedTime, Device d3dDevice) { //Poner luz a los meshes Vector3 posLuz = new Vector3(POS_SHIP.X - 10000, POS_SHIP.Y + 5000, POS_SHIP.Z - 25000); lightMesh.Position = posLuz; this.cargarLuces(posLuz); lightMesh.render(); //Obtener valor modifier heightOlas = (float)GuiController.Instance.Modifiers["heightOlas"]; //Device device = GuiController.Instance.D3dDevice; time += elapsedTime; d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0); update(elapsedTime, time); terrain.render(); skyBox.render(); //skyBoundingBox.render(); // Cargar variables de shader, por ejemplo el tiempo transcurrido. effect.SetValue("time", time); effect.SetValue("height", heightOlas); effect.SetValue("menorAltura", terrain.menorVerticeEnY); effect.SetValue("offset", (float)(terrain.Center.Y * currentScaleY)); effect.SetValue("texHeightmap", TerrenoSimple.toTexture(currentHeightmap)); //GuiController.Instance.Logger.log("OFFSET: " + (terrain.Center.Y * currentScaleY).ToString() + "MENOR ALTURA: " + terrain.menorVerticeEnY); efectoSombra.SetValue("time", time); efectoSombra.SetValue("height", heightOlas); efectoSombra.SetValue("menorAltura", terrain.menorVerticeEnY); efectoSombra.SetValue("offset", (float)(terrain.Center.Y * currentScaleY)); efectoSombra.SetValue("texHeightmap", TerrenoSimple.toTexture(currentHeightmap)); agua.heightOlas = heightOlas; agua.AlphaBlendEnable = true; agua.render(); d3dDevice.Transform.World = Matrix.Identity; TgcD3dInput input = GuiController.Instance.D3dInput; if (input.keyPressed(Microsoft.DirectX.DirectInput.Key.L)) { lloviendo = lloviendo ? false : true; } ship.renderizar(); shipContrincante.renderizar(); lluvia.render(); }
public float CalcularAltura(float x, float z) { float currentScaleXZ = EjemploAlumno.Instance.currentScaleXZ; float currentScaleY = EjemploAlumno.Instance.currentScaleY; TerrenoSimple terrain = EjemploAlumno.Instance.terrain; float largo = currentScaleXZ * 64; float pos_i = 64f * (0.5f + x / largo); float pos_j = 64f * (0.5f + z / largo); int pi = (int)pos_i; float fracc_i = pos_i - pi; int pj = (int)pos_j; float fracc_j = pos_j - pj; if (pi < 0) { pi = 0; } else if (pi > 63) { pi = 63; } if (pj < 0) { pj = 0; } else if (pj > 63) { pj = 63; } int pi1 = pi + 1; int pj1 = pj + 1; if (pi1 > 63) { pi1 = 63; } if (pj1 > 63) { pj1 = 63; } // 2x2 percent closest filtering usual: float H0 = terrain.HeightmapData[pi, pj] * currentScaleY; float H1 = terrain.HeightmapData[pi1, pj] * currentScaleY; float H2 = terrain.HeightmapData[pi, pj1] * currentScaleY; float H3 = terrain.HeightmapData[pi1, pj1] * currentScaleY; float H = (H0 * (1 - fracc_i) + H1 * fracc_i) * (1 - fracc_j) + (H2 * (1 - fracc_i) + H3 * fracc_i) * fracc_j; return(H); }
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)); }
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)); }
//COLISION TERRENO public bool colisionTerreno(bool collide) { TerrenoSimple terreno = EjemploAlumno.Instance.terrain; float YProa = CalcularAltura(proa().X, proa().Z); float YPopa = CalcularAltura(popa().X, popa().Z); float currentScaleY = EjemploAlumno.Instance.currentScaleY; float offset = terreno.Center.Y * currentScaleY; if ((Math.Abs(getPosition().Y - offset - YProa) < 1f) && (YPopa - getPosition().Y + offset < 1)) { collide = true; } return(collide); }
public virtual void actualizar(float elapsedTime, TerrenoSimple agua, float time) { timer.doWhenItsTimeTo(() => this.alternarVisibilidad(), elapsedTime); Vector3 lastPosition = getPosition(); calcularTraslacionYRotacion(elapsedTime, agua, time, lastPosition); Matrix transformacion = rotacion * traslacion; delante = new Vector3((float)Math.Sin(anguloRotacion), 0, (float)Math.Cos(anguloRotacion)); mesh.Transform = transformacion; mesh.BoundingBox.transform(transformacion); alterarVelocidadPorOlas(lastPosition); actualizarCanion(anguloRotacion, elapsedTime, transformacion); }
public virtual void actualizar(float elapsedTime, TerrenoSimple agua, float time) { timer.doWhenItsTimeTo(() => this.alternarVisibilidad(), elapsedTime); Vector3 lastPosition = getPosition(); calcularTraslacionYRotacion(elapsedTime, agua, time, lastPosition); Matrix transformacion = rotacion * traslacion; delante = new Vector3((float)Math.Sin(anguloRotacion), 0, (float)Math.Cos(anguloRotacion)); mesh.Transform = transformacion; mesh.BoundingBox.transform(transformacion); alterarVelocidadPorOlas(lastPosition); actualizarCanion(anguloRotacion, elapsedTime, transformacion); }
/*************************************** MOVIMIENTO ************************************************/ public virtual void calcularTraslacionYRotacion(float elapsedTime, TerrenoSimple agua, float time, Vector3 lastPosition) { Vector3 lastPositionEnemy = EjemploAlumno.Instance.shipContrincante.getPosition(); if (input.keyDown(Key.Left)) { anguloRotacion -= elapsedTime * ROTATION_SPEED; rotacion = Matrix.RotationY(anguloRotacion); } else if (input.keyDown(Key.Right)) { anguloRotacion += elapsedTime * ROTATION_SPEED; rotacion = Matrix.RotationY(anguloRotacion); } if (input.keyDown(Key.Up)) { movementSpeed = Math.Min(movementSpeed + ESCALON_VEL, VEL_MAXIMA); } else if (input.keyDown(Key.Down)) { movementSpeed = Math.Max(movementSpeed - ESCALON_VEL, 0); } movZ -= Convert.ToSingle(movementSpeed * Math.Cos(anguloRotacion) * elapsedTime); movX -= Convert.ToSingle(movementSpeed * Math.Sin(anguloRotacion) * elapsedTime); movY = agua.aplicarOlasA(getPosition(), time).Y + AltoBote / 2; administrarColisiones(lastPosition, new Vector3(movX, movY, movZ), lastPositionEnemy); }
/// <summary> /// Método que se llama una sola vez, al principio cuando se ejecuta el ejemplo. /// Escribir aquí todo el código de inicialización: cargar modelos, texturas, modifiers, uservars, etc. /// Borrar todo lo que no haga falta /// </summary> public override void init() { EjemploAlumno.Instance = this; //GuiController.Instance: acceso principal a todas las herramientas del Framework //Device de DirectX para crear primitivas Device d3dDevice = GuiController.Instance.D3dDevice; //Carpeta de archivos Media del alumno string alumnoMediaFolder = GuiController.Instance.AlumnoEjemplosMediaDir; //Terreno currentHeightmap = GuiController.Instance.AlumnoEjemplosMediaDir + "Heightmap3.jpg"; currentTexture = GuiController.Instance.AlumnoEjemplosMediaDir + "TerrainTexture3.jpg"; terrain = new TerrenoSimple(); terrain.loadHeightmap(currentHeightmap, currentScaleXZ, currentScaleY, new Vector3(0, -125, 0)); terrain.loadTexture(currentTexture); //Agua agua = new TerrenoSimple(); agua.loadHeightmap(GuiController.Instance.AlumnoEjemplosMediaDir + "18_vertex_texture_02.jpg", 50f, 0.5f, new Vector3(0, -125, 0)); agua.loadTexture(GuiController.Instance.AlumnoEjemplosMediaDir + "4141-diffuse.jpg"); agua.AlphaBlendEnable = true; //heightOlas = agua.heightOlas; //Modifier GuiController.Instance.Modifiers.addFloat("heightOlas", 10, 50, 40); //Estado EjemploAlumno.Instance.estado = EstadoDelJuego.SinEmpezar; menu = new Menu(); // Crear SkyBox: skyBox = new TgcSkyBox(); skyBox.Center = new Vector3(0, 0, 0); skyBox.Size = new Vector3(12000, 12000, 12000); skyboxSoleado(skyBox); skyBoundingBox = new TgcBox(); skyBoundingBox.Size = skyBox.Size; skyBoundingBox.Position = skyBox.Center; skyBoundingBox.AlphaBlendEnable = true; skyBoundingBox.updateValues(); lloviendo = false; //Cargar meshes TgcViewer.Utils.TgcSceneLoader.TgcSceneLoader loader = new TgcViewer.Utils.TgcSceneLoader.TgcSceneLoader(); TgcViewer.Utils.TgcSceneLoader.TgcScene scene = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Meshes\\Vehiculos\\Canoa\\Canoa-TgcScene.xml"); meshShip = scene.Meshes[0]; meshShip.setColor(Color.Chocolate); scene = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Meshes\\Vehiculos\\Canoa\\Canoa-TgcScene.xml"); meshes.Add(meshShip); meshShipContrincante = scene.Meshes[0]; meshShipContrincante.setColor(Color.BlueViolet); meshes.Add(meshShipContrincante); scene = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Meshes\\Armas\\Canon\\Canon.max-TgcScene.xml"); meshCanion = scene.Meshes[0]; meshes.Add(meshCanion); scene = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Meshes\\Armas\\Canon\\Canon.max-TgcScene.xml"); meshCanionContrincante = scene.Meshes[0]; meshes.Add(meshCanionContrincante); //Shader effect = TgcShaders.loadEffect(alumnoMediaFolder + "shader agua.fx"); agua.Effect = effect; agua.Technique = "RenderScene"; time = 0; agua.AlphaBlendEnable = true; efectoSombra = TgcShaders.loadEffect(alumnoMediaFolder + "shader agua.fx"); //Creaciones ship = new Ship(POS_SHIP, meshShip, new Canion(POS_SHIP, 5, meshCanion, true)); shipContrincante = new EnemyShip(ship, POS_CONTRINCANTE, meshShipContrincante, new Canion(POS_CONTRINCANTE, 5, meshCanionContrincante, false)); mainCamera = new MainCamera(ship); //Crear caja para indicar ubicacion de la luz lightMesh = TgcBox.fromSize(new Vector3(20, 20, 20), Color.Yellow); }
// Utilidad para convertir directorios de texturas en texturas public static Texture toTexture(string path) { TerrenoSimple conversor = new TerrenoSimple(); conversor.loadTexture(path); return conversor.terrainTexture; }