Help with water simulation needed?

Topics: User Forum
Mar 29, 2010 at 1:36 AM

Hello!


A few days ago I found this physics engine and I must say that it is incredibly good. It is easy to understand and use except for one thing, how to create water simulation! I have tried to find a simple explanation of how to do this but have not succeeded, so therefore I write here and ask how to do it.


I'm using XNA 3.1 and was thinking to start with a simple question and then take one piece at a time.

I want the water to have the same position, width and height of this rectangle (0,900,1920,180)

This is the box that will react with the water:
boxBody = BodyFactory.Instance.CreateRectangleBody (physics simulator, 190, 190, 1);
boxBody.Position = new Vector2 (400, 100);
boxGeom = GeomFactory.Instance.CreateRectangleGeom (physics simulator, boxBody, 190, 190);
boxGeom.FrictionCoefficient = 1f;

I just want to have a rectangular area at the moment and do not need any waves right now. So if anyone would help me, I'd be very grateful.

/ / CptBonex

Mar 29, 2010 at 6:06 AM

Hey,

I've been wondering the same thing as well, specially since I happened upon this video on YouTube recently:
http://www.youtube.com/watch?v=04LB7Hscw50

There doesn't seem to be any official documentation/tutorials on how to do fluids/water, and I'd really like to be able to implement something simple like that video for my game at some point.
Just a rectangular area, like CptBonex mentioned above, but also with the ability to have waves...

Anyone have any ideas/sample code to help out with this?
Many thanks in advance. 

Developer
Mar 29, 2010 at 10:07 PM

Have either of you looked at the WaterSampleXNA or WaterSampleSilverlight? That should be a good example of how to get water working in your games.

Mar 30, 2010 at 5:42 AM

@ Matt: Oh ok, gotcha. Didn't know about the new examples in the latest source revision. Will take a look and hopefully work it out. Thanks! :-)

@ CptBonex: In case you're still missing out, grab the latest source version of the Engine (65093) from here: http://farseerphysics.codeplex.com/SourceControl/list/changesets and then after unpacking the files,
navigate to "FarseerPhysics-65093\Samples\FP2.1\WaterSampleXNA\". Compile and run the solution to have a play around, and then check out the code to see how it works. 

 

Developer
Mar 31, 2010 at 12:13 AM

Please let me know if this helped you.

 

Mar 31, 2010 at 5:43 AM

Yup, from the looks of the example (WaterSampleXNA), that's pretty much what I need, and so should make it fairly easy to integrate it within my framework.
Not that I needed it immediately, but figured it would be good to get it sorted out early.
Many thanks, Matt! :-) 

Apr 2, 2010 at 1:26 PM
Edited Apr 2, 2010 at 1:53 PM

Ok i got it working but i got one smal problem. I have made a class that makes it esier to create the water and it works perfect when I set effect.view to my default matrix. and when i use my camera class matrix, then the water is being drawn whitout the alpha. why is that so and how do i fix that?

Here is my water.draw method:

 

 

public void Draw(GraphicsDevice graphicsDevice,
            Matrix worldMatrix, Matrix cameraMatrix, Matrix projectionMatrix)
        {
                graphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
                //ScreenManager.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;

                // create the triangle strip
                CreateWaterVertexBuffer();

                // set the devices vertex declaration NOTICE - the vertex type is the same as we use when we create the vertex buffer
                graphicsDevice.VertexDeclaration = new VertexDeclaration(graphicsDevice,
                                                                                       VertexPositionColor.VertexElements);

                // create a basic effect with no pool
                BasicEffect effect = new BasicEffect(graphicsDevice, null);

                // set the effects matrix's
                effect.World = worldMatrix;
                effect.View = cameraMatrix;
                effect.Projection = projectionMatrix;
                effect.Alpha = 0.5f;    // this effect supports a blending mode
                effect.VertexColorEnabled = true;   // we must enable vertex coloring with this effect
                effect.Begin();
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices, 0,
                                                                    waterModel.WaveController.NodeCount);
                    pass.End();
                }
                effect.End();
	}

 

Here is the matrix that works:

 

 

cameraMatrix = Matrix.Identity;

 

And here is my camera class and the _transform matrix that makes the alpha go away:

 

 

    public class Camera2d
    {
        public float _zoom; // Camera Zoom
        public Matrix _transform; // Matrix Transform
        public Vector2 _pos; // Camera Position
        public float _rotation; // Camera Rotation

        public Camera2d()
        {
            _zoom = 1.0f;
            _rotation = 0.0f;
            _pos = Vector2.Zero;
        }

        // Sets and gets zoom
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
        }

        public void Rotation(float rotation)
        {
            _rotation = rotation;
        }

        // Auxiliary function to move the camera
        public void Move(Vector2 amount)
        {
            _pos = amount;
        }

        // Get set position
        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }

        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
            _transform =
              Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
                                         Matrix.CreateRotationZ(MathHelper.ToRadians(_rotation)) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                                         Matrix.CreateTranslation(new Vector3(1920 * 0.5f, 1080 * 0.5f, 0));
            return _transform;
        }
    }

Help me!

 

 

 

Developer
Apr 4, 2010 at 11:45 PM

Camera transformations shouldn't have anything to do with whether or not alpha is enabled. Write up a quick sample app demonstrating the problem. Just comment whee you think your changes cause problems. I'll take a look and solve this problem for you.