Page 1 of 1

Water Physics

Posted: Wed Feb 06, 2008 6:49 pm
by Maniak11
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?

Re: Water Physics

Posted: Wed Feb 06, 2008 8:44 pm
by Norbo
I plan on adding simple buoyancy into v0.5.0, but full dynamic water will come much later, probably after v1.0.0 (which should be the name of what would otherwise be called "v0.6.0").

Basically, once the fundamentals are all there, I'll start branching out into fun stuff.

Re: Water Physics

Posted: Wed Feb 06, 2008 10:41 pm
by Zukarakox
And by 'fun stuff' you mean the stuff with math that would make heads explode, rite?

Re: Water Physics

Posted: Thu Feb 07, 2008 2:56 am
by Fe_Yoshi
Just say "See Crysis". 'Nuff said.

o and btw BEPU gonna expire if you don't log on narbo o.O

Re: Water Physics

Posted: Fri Nov 20, 2009 7:09 pm
by JHOW
Not perfect but it works quite well. To use it Just type Water water = new Water(space);
Also you will need Constraint drawing enabled.

Add these 2 classes to the BEPUphysicsDemos on this site.

Water Class

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++;
                }

            }
        }
    }
}
Water Vertex Class
Made as a helper for rendering the water.

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);
        }
    }
}