using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Storage; namespace OGD_Animation { class TerrainCollision { public Vector3[] vertex = new Vector3[289]; public Vector3[] four = new Vector3[4]; public int[] checking = new int[4]; public Vector3 posi; float xDistance; float[] xdistances = new float[4]; float[] zdistances = new float[4]; float zDistance; public Vector3 Collision(Model model, float angle, Vector3 posi, string mesh) { model.Meshes[mesh].VertexBuffer.GetData(vertex); this.posi = posi; this.posi.Y = CheckVertex(); return this.posi; } private float CheckVertex() { int count2 = 0; for (int count = 0; count < vertex.Length; count++) { if (count > 0) { for (count2 = 0; count2 < 4; count2++) { if (four[count2] == Vector3.Zero) four[count2] = vertex[count]; else if (Math.Abs(Math.Sqrt((Math.Pow((vertex[count].X - posi.X), 2) + Math.Pow((vertex[count].Z - posi.Z), 2)))) <= Math.Abs(Math.Sqrt((Math.Pow((four[count2].X - posi.X), 2) + Math.Pow((four[count2].Z - posi.Z), 2))))) { four[count2] = vertex[count]; count2 = 4; } } } else four[0] = vertex[0]; count2 = 0; } int closest = 0; for(int count = 0; count < 4; count++) { xDistance += (posi.X - four[count].Z); xdistances[count] = (posi.X - four[count].Z); zDistance += (posi.X - four[count].Z); zdistances[count] = (posi.Z - four[count].Z); for(count2 = 0; count2 < 4; count2 ++) { for (int x = 0; x < 4; x++) { if (Math.Abs(Math.Sqrt((Math.Pow((four[count].X - posi.X), 2) + Math.Pow((four[count].Z - posi.Y), 2)))) < Math.Abs(Math.Sqrt(Math.Pow((four[count2].X - posi.X), 2) + Math.Pow((four[count].Z - posi.Y), 2)))) { closest = count2; } } } } float y = ((xdistances[0] / xDistance) * four[closest].Y) + ((xdistances[1] / xDistance) * four[closest].Y) + ((xdistances[2] / xDistance) * four[closest].Y) + ((xdistances[3] / xDistance) * four[closest].Y) + 5; return y; } } }