CodeBase - Ye olde water algorithm Mk. 2
Return to the CodeBase listing
Category: 3D Effects
Version: 1.1
Information
Uploaded: 10th May 2005 23:48
Modified: 15th May 2005 19:53
Author: Tinkerer
Summary
An old and well-known water effect, plus quick and dirty normals calculation.
Full Description
Quick and dirty normals calculation used to illuminate the matrix from three light sources. Spacebar selects matrix behaviour, mouse2 selects interference type, and mouse1 affects interference amount.<br /> <br /> The basic principle of the algorithm is surprisingly simple, with the main part being only four lines long. Two two-dimensional arrays are set up with zero values. The algorithm runs over every element in the first array and does this:<br /> <br /> For each element in the first array...<br /> Get the value of the four horizontal and vertical elements adjoining the current element in the second two-dimensional array space, divided by two.<br /> Remove from this value, the value of the current element in the first array.<br /> Apply the result to the current element in the first array.<br /> Multiply the value of the current element in the first array by a damping value of less than one.<br /> Repeat for all elements.<br /> <br /> The data in the two arrays is then swapped, and the cycle repeats. If a non-zero value is placed into an array, waves of non-zero values will propagate outwards from the point of interference, reflecting from the edge elements of the arrays. If an array is then used as the height information for a matrix object, that matrix will display the patterns as water-like waves. Because of the damping value, the interference will gradually appear to dissipate, eventually leaving a flat landscape of zero values.<br /> <br /> <update 1.1><br /> Code generally tidied up and optimised. The memory foam effect is no longer a broken water effect. Even more remarks added. That's about it for this script until I embed it in a function and use it in something bigger.<br /> <br /> Known bugs: This algorithm only approximates a wave-like effect on a 2D map. It isn't precise, but it's precise enough for computer games. If excessive interference is introduced into the arrays, the result can be chaotic at best. High-frequency waves will also not be rendered well.<br /> In this particular script's case, flicking rapidly between water and memory foam matrix behaviours can generate increasingly large values in the arrays. The workaraound is to be patient and let the values decrease.
Comments
No comments yet.