The index parameter determines at which spring the splash should originate, and the speed parameter determines how large the waves will be. Height) Īny time you want to make waves, call Splash(). LeftDeltas = Spread * (springs.Height - springs. I'll show the code first, and then go over it: for (int i = 0 i 0) Of course, if the springs are all independent, the waves will never spread out like real waves do. As shown in the first diagram, we're modelling the water using a series of parallel, vertical springs. Now that we can make a spring, let's use them to model water. A high dampening factor will make the water look thick like molasses, while a low value will allow the waves to oscillate for a long time. The demo uses a dampening factor of 0.025. It should be fairly small if you want your waves to oscillate. Here, v is velocity and d is the dampening factor - another constant you can tweak to adjust the feel of the water. This requires a small adjustment to our spring formula: The idea is to apply a force in the opposite direction that our spring is moving in order to slow it down. To solve this we must apply some dampening. Once a spring starts oscillating, it will never stop. There is a problem with our spring model so far. Even worse, the Euler method of integration tends to gain energy as the simulation becomes less accurate, causing stiff springs to explode. A very stiff spring can even have an oscillation period that's shorter than your time step. This does not play well with numerical integration, which simulates the springs as a series of discrete jumps at regular time intervals. Very stiff springs apply very strong forces that change greatly in a very small amount of time. A high spring constant will make the water look more like jiggling Jello.Ī word of warning: do not set the spring constant too high. Forces will create small waves that oscillate quickly. Conversely, a high spring constant will increase the tension in the spring. This means a force will cause large waves that oscillate slowly. A low spring constant will make the springs loose. You can adjust this value to change the properties of the water. I mentioned earlier that the spring constant, k, controls the stiffness of the spring. For the demo, I set it to halfway down the screen, at 240 pixels. You should set this value to where you want the surface of the water to be. Here, TargetHeight is the natural position of the top of the spring when it's neither stretched nor compressed. Putting it all together, our water surface particles will do the following each frame: public float Position, Velocity Ĭonst float k = 0.025f // adjust this value to your liking It's not the most accurate type of numerical integration, but it's fast, simple and adequate for our purposes. We're going to use the simplest form of numerical integration - each frame we simply do the following: Position += Velocity To determine position from acceleration, we need to do numerical integration. We'll assume that all our particles will have the same mass, so we can combine k/m into a single constant. This gives us the acceleration for our particles. This means the stronger a force pushes on an object, and the lighter the object is, the more it accelerates.Ĭombining these two formulas and rearranging gives us: Here, F is force, m is mass and a is acceleration. To do this, we need a couple more formulas from physics. To simulate springs, we must figure out how to move particles around based on Hooke's Law. The spring constant, k, determines the stiffness of the spring. The negative sign indicates the force is in the opposite direction to which the spring is displaced if you push the spring down, it will push back up, and vice versa. The force provided by a spring is given by Hooke's Law:į is the force produced by the spring, k is the spring constant, and x is the spring's displacement from its natural length. Springs have a certain natural length if you stretch or compress a spring, it will try to return to that natural length. One great thing about springs is that they're easy to simulate. We will then make water particles pull on their neighbouring particles to allow the waves to spread. This will allow the waves to bob up and down.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |