Water Physics

Post and discuss features you'd like to see in the BEPUphysics library.
Post Reply
Maniak11
Posts: 43
Joined: Mon Jul 17, 2006 3:51 am

Water Physics

Post 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?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Water Physics

Post 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.
User avatar
Zukarakox
Not a Site Admin
Posts: 426
Joined: Mon Jul 10, 2006 4:28 am

Re: Water Physics

Post by Zukarakox »

And by 'fun stuff' you mean the stuff with math that would make heads explode, rite?
i has multiple toes
Fe_Yoshi
Posts: 397
Joined: Tue Jul 04, 2006 5:05 pm
Location: New Tower!

Re: Water Physics

Post by Fe_Yoshi »

Just say "See Crysis". 'Nuff said.

o and btw BEPU gonna expire if you don't log on narbo o.O
It's simple, just take the hydraulic phase ship emulator and attach it to the photon particle emitter, BAM, new tower!
JHOW
Posts: 9
Joined: Mon Oct 26, 2009 12:52 am

Re: Water Physics

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