| using System; |
| using System.Collections.Generic; |
| using Microsoft.Xna.Framework; |
| using Microsoft.Xna.Framework.Audio; |
| using Microsoft.Xna.Framework.Content; |
| using Microsoft.Xna.Framework.Graphics; |
| using Microsoft.Xna.Framework.Input; |
| using Microsoft.Xna.Framework.Storage; |
| using BEPUphysics; |
| using System.Diagnostics; |
| using System.Threading; |
| using BEPUphysics.BroadPhases; |
| using BEPUphysics.Entities; |
| using BEPUphysics.Constraints; |
| using BEPUphysics.ForceFields; |
| using BEPUphysics.DataStructures; |
| using System.Collections; |
| |
| namespace BlockTanx |
| { |
| public class Water |
| { |
| public List<Spring> surfaceTensionSprings; |
| public List<Spring> densitySprings; |
| public float surfaceTension = 0.006f; |
| public float density = 0.6f; |
| public List<Entity> waterTris; |
| public List<WaterVertex> waterVertices; |
| |
| public int wLength = 25; |
| public int wWidth = 25; |
| float radius = 0.0001f; |
| float waterDensity = 5; |
| float strength = float.MaxValue; |
| Spring spring; |
| Sphere sphere; |
| |
| Space space; |
| |
| public Water(Space space) |
| { |
| this.space = space; |
| BuildWater(); |
| } |
| |
| public void BuildWater() |
| { |
| |
| waterVertices = new List<WaterVertex>(); |
| waterTris = new List<Entity>(); |
| surfaceTensionSprings = new List<Spring>(); |
| densitySprings = new List<Spring>(); |
| |
| generateWaterGrid(); |
| setupSprings(); |
| } |
| float scale = 1f; |
| public void generateWaterGrid() |
| { |
| float xOffset = -wWidth * 0.5f; |
| float zOffset = -wLength * 0.5f; |
| |
| for (int i = 0; i < wWidth; i+= Convert.ToInt32(scale)) |
| { |
| for (int j = 0; j < wLength; j += Convert.ToInt32(scale)) |
| { |
| sphere = new Sphere(new Vector3(i + xOffset, 0, j + zOffset), radius,waterDensity); |
| |
| WaterVertex vertex = new WaterVertex(sphere); |
| |
| space.add(sphere); |
| waterVertices.Add(vertex); |
| } |
| |
| } |
| } |
| public void setupSprings() |
| { |
| int counter = 0; |
| for (int i = 0; i < wWidth; i++) |
| { |
| for (int j = 0; j < wLength; j++) |
| { |
| |
| |
| if (j != 0 && j < wLength - 1) |
| { |
| spring = new Spring(waterVertices[counter].sphere, waterVertices[counter - 1].sphere, |
| new Vector3(waterVertices[counter].sphere.worldTransform.M41, waterVertices[counter].sphere.worldTransform.M42, waterVertices[counter].sphere.worldTransform.M43), |
| new Vector3(waterVertices[counter - 1].sphere.worldTransform.M41, waterVertices[counter - 1].sphere.worldTransform.M42, waterVertices[counter - 1].sphere.worldTransform.M43), |
| 5000, surfaceTension, strength, strength, strength); |
| |
| space.add(spring); |
| |
| surfaceTensionSprings.Add(spring); |
| } |
| |
| if (j < wLength - 1) |
| { |
| spring = new Spring(waterVertices[counter].sphere, waterVertices[counter + 1].sphere, |
| new Vector3(waterVertices[counter].sphere.worldTransform.M41, waterVertices[counter].sphere.worldTransform.M42, waterVertices[counter].sphere.worldTransform.M43), |
| new Vector3(waterVertices[counter + 1].sphere.worldTransform.M41, waterVertices[counter + 1].sphere.worldTransform.M42, waterVertices[counter + 1].sphere.worldTransform.M43), |
| 5000, surfaceTension, strength, strength, strength); |
| |
| space.add(spring); |
| |
| surfaceTensionSprings.Add(spring); |
| } |
| |
| if ((counter + wLength) < waterVertices.Count) |
| { |
| spring = new Spring(waterVertices[counter].sphere, waterVertices[counter + wLength].sphere, |
| new Vector3(waterVertices[counter].sphere.worldTransform.M41, waterVertices[counter].sphere.worldTransform.M42, waterVertices[counter].sphere.worldTransform.M43), |
| new Vector3(waterVertices[counter + wLength].sphere.worldTransform.M41, waterVertices[counter + wLength].sphere.worldTransform.M42, waterVertices[counter + wLength].sphere.worldTransform.M43), |
| 5000, surfaceTension, strength, strength, strength); |
| |
| space.add(spring); |
| |
| |
| surfaceTensionSprings.Add(spring); |
| } |
| |
| spring = new Spring(null, waterVertices[counter].sphere, |
| new Vector3(waterVertices[counter].sphere.worldTransform.M41, waterVertices[counter].sphere.worldTransform.M42, waterVertices[counter].sphere.worldTransform.M43), |
| new Vector3(waterVertices[counter].sphere.worldTransform.M41, waterVertices[counter].sphere.worldTransform.M42, waterVertices[counter].sphere.worldTransform.M43), |
| 160, density, strength, strength, strength); |
| space.add(spring); |
| densitySprings.Add(spring); |
| /* |
| if ((counter + wLength) < waterVertices.Count && j != 0 && j < wLength - 1) |
| { |
| tri = new Triangle(new Vector3( waterVertices[counter].worldTransform.M41, waterVertices[counter].worldTransform.M42, waterVertices[counter].worldTransform.M43), |
| new Vector3( waterVertices[counter + 1].worldTransform.M41, waterVertices[counter + 1].worldTransform.M42, waterVertices[counter + 1].worldTransform.M43), |
| new Vector3( waterVertices[counter + wLength].worldTransform.M41, waterVertices[counter + wLength].worldTransform.M42, waterVertices[counter + wLength].worldTransform.M43),10); |
| space.add(tri); |
| waterTris.Add(tri); |
| } |
| */ |
| counter++; |
| } |
| |
| } |
| } |
| } |
| } |
| |