Liquid surface simulation

This is a quick and dirty adaptation of as2 script from http://www.har95.com, a weblog written by Antonio Calviño Esquer.

All credits belong to him but if you want to play and clean it :
main.lua :

--[[------------------------- ALL CREDITS GOES TO : -------------------------------  
  
http://char95.com/post/waterSurface2D/  
  
Copyright (c) 2010 char95.  
  
Permission is hereby granted, free of charge, to any person obtaining a copy  
of this software and associated documentation files (the "Software"), to deal  
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
copies of the Software, and to permit persons to whom the Software is  
furnished to do so, subject to the following conditions:  
  
The above copyright notice and this permission notice shall be included in  
all copies or substantial portions of the Software.  
  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
THE SOFTWARE.  
  
------------------------------------------------------------------------------]]--  
  
nNodes = 160  
kf = 0.850  
kp = 0.500  
kr = 0.985  
ho = 400  
wo = 340   
dxNode = 0  
dx = 0  
dy = 0  
x = {}  
y = {}  
y0 = {}  
x0 = {}  
vy = {}  
vx = {}  
mousexo = 0  
mouseyo = 0  
mousex = 0  
mousey = 0  
line = {}  
 onScreenTouch = function(event)  
 if event.phase == "began" then  
 mousex = event.x  
 mousey = event.y  
 if event.y \> 400 then  
 Runtime:addEventListener( "enterFrame", action )  
 elseif event.y \< 400 then  
 Runtime:removeEventListener( "enterFrame", action )  
 end  
  
 elseif event.phase == "ended" then  
 Runtime:removeEventListener( "enterFrame", action )  
  
 end  
 end  
 loop = function()  
  
 dy = mousey- mouseyo  
 dx = mousex - mousexo  
  
 nodeTransfer()  
 mousexo = mousex  
 mouseyo = mousey   
end  
  
  
 init = function()  
  
 NodeWidth = 480/nNodes  
  
 background = display.newRect(0, 0, 480, 800)  
 background:setFillColor(243, 226, 183)  
 circle = display.newCircle(240,400,120 )  
 circle:setFillColor(197, 61, 49)  
 myText = display.newText("TOUCH THE WATER !", 170, 150, native.systemFontBold, 12)  
 myText:setTextColor(158, 46, 35)  
 myText.size = 36  
 for n = 0, nNodes do  
  
 x[n] = dxNode + NodeWidth\*n  
 y[n] = 400  
  
 x0[n] = x[n]  
 y0[n] = y[n]  
  
 vx[n] = 0  
 vy[n] = 0  
  
 end  
 Runtime:addEventListener( "enterFrame", loop )  
 Runtime:addEventListener( "touch", onScreenTouch )  
  
 end  
  
  
 action = function()  
 closeNode = math.floor(mousex/NodeWidth)  
  
 yStrong = math.ceil(math.abs(mousey)/20)  
 yNoise = math.abs(mousey / 8) + 3  
  
 for n=0,nNodes do  
  
 yRandNoise = math.random(-5,5 )  
 nodeDist = math.abs(closeNode - n)  
  
 theta = math.max((yStrong - nodeDist) / yStrong, 0) \* math.pi - math.pi/2  
 yRange = (math.sin(theta) + 1)/20  
  
 dv = kf \* mousey \* yRange + yRandNoise  
  
 vy[n] = vy[n] + dv\*.05  
 vx[n]= 0   
 end  
 end  
  
  
 nodeTransfer = function()  
 for n=0,nNodes do  
  
  
 if (n==0) then   
 dvx = x[n + 1] - x[n]  
 dvy = y[n + 1] - y[n]  
  
 elseif (n == nNodes) then  
 dvx = x[n - 1] - x[n]  
 dvy = y[n - 1] - y[n]  
  
 else  
 dvx = x[n + 1] + x[n - 1] - 2 \* x[n]  
 dvy = y[n + 1] + y[n - 1] - 2 \* y[n]  
  
 end  
 dvy = dvy\*kp  
 dvx = dvx\*kp  
  
  
 vx[n] = (vx[n] + dvx) \* kr  
 vy[n] = (vy[n] + dvy) \* kr  
  
 end  
 nodeMove()  
 end  
 nodeMove = function()  
  
  
 for n=0,nNodes do  
  
 y[n] = y[n] + vy[n]  
 vy[n] = vy[n] + (y0[n] - y[n]) / 100  
  
 if n ~= 0 and n ~= nNodes then  
  
 x[n] = x[n] + vx[n]  
 vx[n] = vx[n] + (x0[n] - x[n]) / 100  
  
 else  
  
 vx[n] = 0  
 end  
  
 end  
 nodeDraw()  
 end  
 nodeDraw = function()  
  
 for n=0,nNodes do  
 if line[n] then  
 line[n]:removeSelf()  
 end  
 end  
  
 for n=0,nNodes do  
 line[n] = display.newLine(x[n], y[n], x[n], 800)  
 line[n]:setColor( 180, 201, 192, 230 )  
 line[n].width = NodeWidth  
 end   
  
  
 end  
init()  

config.lua :

application ={content ={width = 480,height = 800,scale = "letterbox",fps = 100},}[/code] [import]uid: 29419 topic_id: 8425 reply_id: 308425[/import]

Very impressive! [import]uid: 79135 topic_id: 8425 reply_id: 60435[/import]