XNA Creators Club Online
Page 1 of 1 (1 items)
Sort Posts: Previous Next

Interactive water not rendering correctly.

Last post 11/20/2009 6:22 AM by JHow. 0 replies.
  • 11/20/2009 6:22 AM

    Interactive water not rendering correctly.

    I am working on a game that takes place in a pool so good water is a must. My problem is that the water is not rendering correctly. I used BEPU Physics for the collision and the WaterComponentDemo to draw the water. I create a grid of tiny spheres and use springs to connect them. This allows the water to be interactive. That's where my problem starts. I had to rewrite the way the water component sets up its mesh. Instead i set the vertex positions the position of each spher in the watervertex list. I think my problem has to do with the fact that i rebuild the waters drawable mesh every frame so that it moves with the waters collision vertices. It may have something to do with the vertices moving on their y axis which they don't in the water component demo.

    Here is a video of it.
    http://www.youtube.com/user/HaloRiderJHOW#p/a/u/0/Bv3ybjX6D6k

    This one may or may not be visible to some people because it is on facebook but it shows the water grid before i added the shader.
    It should be visible because it is set to public but sorry if it isn't.
    PS. Once this is finished i will release it because there is nothing on such a cool topic.

    http://www.facebook.com/profile.php?v=app_2392950137&ref=name&id=100000029795138#/video/video.php?v=105089136168746

    And some code where the problem may lie.

    The first block sets up the render mesh using the water grid positions.
    And the second is the collision part of the water.

    public void BuildWater(Vector3 position, int width, int length, GraphicsDevice graphics) 
            { 
                myPosition = position; 
                this.width = width; 
                this.length = length; 
                // Vertices 
                myVertices = new VertexMultitextured[width * length]; 
                mDecl = new VertexDeclaration(graphics, VertexMultitextured.VertexElements); 
     
                 
     
                    for (int j = 0; j < vertexPositions.Count; ++j) 
                    { 
                        // Negate the depth coordinate to put in quadrant four.   
                        // Then offset to center about coordinate system. 
                        myVertices[j].Position = vertexPositions[j].GetPosition(); 
                         
                        myVertices[j].Normal = new Vector3(0, -1, 0); 
                        //myVertices[j].TextureCoordinate.X = vertexPositions[j].GetPosition().X / width; 
                        //myVertices[j].TextureCoordinate.Y = vertexPositions[j].GetPosition().Z / length; 
                    } 
                    for (int i = 0; i < width; ++i) 
                    { 
                        for (int j = 0; j < length; ++j) 
                        { 
                            int index = i * width + j; 
                            
                            myVertices[index].TextureCoordinate = new Vector2((float)j / width, (float)i / length); 
                        } 
                    } 
                 
                 
     
     
                vb = new VertexBuffer(graphics, VertexMultitextured.SizeInBytes * width * length, BufferUsage.WriteOnly); 
                vb.SetData(myVertices); 
     
                short[] terrainIndices = new short[(width - 1) * (length - 1) * 6]; 
                for (short x = 0; x < width - 1; x++) 
                { 
                    for (short y = 0; y < length - 1; y++) 
                    { 
                        terrainIndices[(x + y * (width - 1)) * 6] = (short)((x + 1) + (y + 1) * width); 
                        terrainIndices[(x + y * (width - 1)) * 6 + 1] = (short)((x + 1) + y * width); 
                        terrainIndices[(x + y * (width - 1)) * 6 + 2] = (short)(x + y * width); 
     
                        terrainIndices[(x + y * (width - 1)) * 6 + 3] = (short)((x + 1) + (y + 1) * width); 
                        terrainIndices[(x + y * (width - 1)) * 6 + 4] = (short)(x + y * width); 
                        terrainIndices[(x + y * (width - 1)) * 6 + 5] = (short)(x + (y + 1) * width); 
                    } 
                } 
     
                ib = new IndexBuffer(graphics, typeof(short), (width - 1) * (length - 1) * 6, BufferUsage.WriteOnly); 
                ib.SetData(terrainIndices); 
            }





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

Page 1 of 1 (1 items) Previous Next