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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
--[[------------------------- 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()

Very impressive!

views:1641 update:2011/10/19 8:59:29
corona forums © 2003-2011