Water Physics
Posted: Wed Feb 06, 2008 6:49 pm
Don't really have enough skill in lolXNA to be useful in any development or testing or anything, but do you have any plans for water physics?
Former forum of queegs; use github discussions now!
https://forum.bepuentertainment.com/
Code: Select all
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 BEPUphysicsDemos
{
public class Water
{
public List<Spring> surfaceTensionSprings;
public List<Spring> densitySprings;
public float surfaceTension = 0.6f;
public float density = 0.6f;
public List<Entity> waterTris;
public List<WaterVertex> waterVertices;
public int wLength = 40;
public int wWidth = 40;
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();
}
public void generateWaterGrid()
{
for (int i = 0; i < wWidth; i+= Convert.ToInt32(0.6f))
{
for (int j = 0; j < wLength; j += Convert.ToInt32(0.6f))
{
sphere = new Sphere(new Vector3(i, 0, j), radius,waterDensity);
sphere.allowedPenetration = 1000;
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++;
}
}
}
}
}
Code: Select all
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 BEPUphysicsDemos
{
public class WaterVertex
{
//public Vector3 position;
public Sphere sphere;
public WaterVertex(Sphere sphere)
{
this.sphere = sphere;
}
public Vector3 GetPosition()
{
return new Vector3(sphere.worldTransform.M41, sphere.worldTransform.M42, sphere.worldTransform.M43);
}
}
}