Hi!
I posted a primitive version of this in 20liners but this one has stuff that i didnt manage to squeeze into a mere 20 lines.
Its basically a way of simulating waves ( not real waves just fun looking ones ). Every point of the surface is drawn to its neighbouring points. this results in a cool wave effect. Also this code calculates the normals for the points ( rough ones ) and that normal is then used to color the surface to make it seem as if there was a light shining.
HOW to use:
press W/S to move forward/backward
use mouse to look
press SPACE repeatedly to soothe the waves
hold SPACE to pause ( you can still move )
hold leftmouse to select a point.
then move mouse up/down to influence that point to create waves
hold ENTER to stop the two moving points ( the ones that cause waves in the beginning )
Tap ENTER to put the light to camera orientation
HOW to tweak:
basefriction# = a lower value will make the waves dissipate faster, a value of more than one will make the waves increase in size
lineforce# = will increase the pull between the lines ( not reccomended to change)
gravity# = self explanatory
light# = increase light strength
diffuse# = increase ambient light strength# ( yeah wrongly named )
a and b (at the top) = the height and width of the surface
i use 80x80 but then i have a very good comp. you might want to try lower settings.
hope you enjoy it
OH it uses D3D_plugin
Rem Project: cloth
Rem Created: Saturday, March 17, 2012
Rem ***** Main Source File *****
Rem Project: Dark Basic Pro Project
Rem Created: Saturday, March 17, 2012
Rem ***** Main Source File *****
sync on : sync rate 60
w = screen width() : h = screen height() : a =80 : b =80: var# = 1
dim x#(a,b) : dim y#(a,b) : dim z#(a,b) dim v(a,b) : dim m#(a,b) : dim o(a,b) : : dim n(a,b) : dim c#(a,b)
for i = 1 to a : : for j = 1 to b
x#(i,j) = i*var# : y#(i,j) = j*var# : m#(i,j) = 5 : v(i,j) = createvector() : n(i,j) = createvector()
o(i,j) = find free object() : make object plane o(i,j),1,1
rem o2(i,j) = find free object() : make object plane o(i,j),1,1
hide object o(i,j)
NEXT : next
F1 = createvector() : F2 = createvector()
F3 = createvector() : F4 = createvector() : G = createvector()
F5 = createvector()
a2setblendmode 2,2,1
rad = 5 : basefriction# = 0.999 : lineforce# = 0.15
c =3
gravity# = -0.008
light# = 4.0
diffuse# = 9.0
id1 = a2createfont("txt",12,1,1,0)
position camera 55,100,50
point camera a/2,b/2,0
zrotate camera 0
color backdrop rgb(0,0,0)
set vector3 F5,-1,-1,-0.5 : normalize vector3 F5,F5
do
position mouse w/2,h/2
if selected = 0
zrotate camera wrapvalue(camera angle z()+mousemovex())
xrotate camera wrapvalue(camera angle x()+mousemovey())
endif
if keystate(17) = 1 then move camera 1
if keystate(31) = 1 then move camera -1
mx# = mousex() : my# = mousey() : if mouseclick() = 0 then selected = 0
for i = 1 to a : x#(i,1) = i : y#(i,1) = 1 : z#(i,1) = 0 : next
for j = 1 to b : x#(1,j) = 1 : y#(1,j) = j : z#(1,j) = 0 : next
for i = 1 to a : x#(i,b) = i : y#(i,b) = b : z#(i,b) = 0 : next
for j = 1 to b : x#(a,j) = a : y#(a,j) = j : z#(a,j) = 0 : next
if returnkey() = 0
z#(10,10) = sin(vv*3)*25 : set vector3 v(10,10),0,0,0
z#(40,10) = sin(vv*8)*10 : set vector3 v(40,10),0,0,0
endif
if returnkey() = 1
pick screen screen width()/2, screen height()/2, 1.0
set vector3 F5,get pick vector x(),get pick vector y(),get pick vector z()
normalize vector3 F5,F5
ENDIF
if spacekey() then friction# = 0 else friction# = basefriction#
rem xgravity# = sin(vv)*gravity#
rem ygravity# = cos(vv)*gravity#
vv = wrap(vv+1,0,36000)
d3d_line3d 0,0,0,x vector3(F5)*100,y vector3(F5)*100,z vector3(F5)*100,rgb(255,255,255),1
for i = 2 to a-1 : for j = 2 to b-1
set vector3 F1,x#(i-1,j)-x#(i,j),y#(i-1,j)-y#(i,j),z#(i-1,j)-z#(i,j) : set vector3 F2,x#(i+1,j)-x#(i,j),y#(i+1,j)-y#(i,j),z#(i+1,j)-z#(i,j)
set vector3 F3,x#(i,j-1)-x#(i,j),y#(i,j-1)-y#(i,j),z#(i,j-1)-z#(i,j) : set vector3 F4,x#(i,j+1)-x#(i,j),y#(i,j+1)-y#(i,j),z#(i,j+1)-z#(i,j)
set vector3 G,xgravity#*m#(i,j),ygravity#*m#(i,j),gravity#*m#(i,j)
cross product vector3 n(i,j),F3,F2 : normalize vector3 n(i,j),n(i,j) : if z vector3(n(i,j)) < 0 then multiply vector3 n(i,j),-1
add vector3 n(i,j),n(i,j),F5 : c#(i,j) = light#/(1.0+length vector3(n(i,j))*diffuse#)
add vector3 F1,F1,F3 : add vector3 F1,F1,F2 : add vector3 F1,F1,F4 : add vector3 F1,F1,G : multiply vector3 F1,lineforce#
add vector3 v(i,j),v(i,j),F1 : multiply vector3 v(i,j),friction#
NEXT : next
maxz# = -1000 : minz# = 1000
for i = 1 to a : for j = 1 to b
x#(i,j) = x#(i,j)+x vector3(v(i,j)) : y#(i,j) = y#(i,j)+y vector3(v(i,j)) : z#(i,j) = z#(i,j)+z vector3(v(i,j))
if z#(i,j) > maxz# then maxz# = z#(i,j)
if z#(i,j) < minz# then minz# = z#(i,j)
if selected = o(i,j) then z#(i,j) = z#(i,j)+mousemovey() : set vector3 v(i,j),0,0,0
if button(o(i,j)) = 2 and selected = 0 then selected = o(i,j) : m#(i,j) = 5
if button(o(i,j)) = 3 and selected = 0 then m#(i,j) = 100
position object o(i,j),x#(i,j),y#(i,j),z#(i,j)
rem point object o(i,j),x#(i,j)+x vector3(v(i,j)),y#(i,j)+y vector3(v(i,j)),z#(i,j)+z vector3(v(i,j))
NEXT : next
zdif# = maxz#-minz#
for i = 2 to a-1 : for j = 2 to b-1
rem zv# = (3*(z#(i,j)-minz#))/(zdif#) : d3d_line3d x#(i,j),y#(i,j),z#(i,j),x#(i-1,j),y#(i-1,j),z#(i-1,j),rgb(20*zv,70*zv#,200),1
rem d3d_line3d x#(i,j),y#(i,j),z#(i,j),x#(i,j-1),y#(i,j-1),z#(i,j-1),rgb(20*zv#,70*zv#,200),1
c = rgb(min(200*c#(i,j),255),min(100*c#(i,j),255),min(40*c#(i,j),255))
d3d_line3d x#(i,j),y#(i,j),z#(i,j),x#(i,j-1),y#(i,j-1),z#(i,j-1),c,1
d3d_line3d x#(i,j),y#(i,j),z#(i,j),x#(i-1,j),y#(i-1,j),z#(i-1,j),c,1
rem normal d3d_line3d x#(i,j),y#(i,j),z#(i,j),x#(i,j)+x vector3(n(i,j)),y#(i,j)+y vector3(n(i,j)),z#(i,j)+z vector3(n(i,j)),rgb(20*zv#,70*zv#,200),1
NEXT : next
a2text id1,0,0,"MIN = "+str$(minz#)+" || MAX = "+str$(maxz#),rgb(255,255,255)
sync
LOOP
function lenvec(vec,len#)
normalize vector3 vec,vec : multiply vector3 vec,len#
ENDFUNCTION
function createvector()
num = find free vector() : null = make vector3(num)
ENDFUNCTION num
function button(obj)
if squaredist(object screen x(obj),object screen y(obj),mousex(),mousey()) < 200
selected = mouseclick()+1
endif
endfunction selected
function squaredist(x1#,y1#,x2#,y2#)
d# = (x1#-x2#)^2+(y1#-y2#)^2
ENDFUNCTION d#
function dist(x1#,y1#,z1#,x2#,y2#,z2#)
d# = sqrt((x1#-x2#)^2+(y1#-y2#)^2+(z1#-z2#)^2)
ENDFUNCTION d#