WASDQE keys move around the terrain.
Left and Right clicks modify the mesh up and down.
Code: Select all
'======================================
'--------------------------------------
'3d Liquid Terrain by Ezekiel Gutierrez
'--------------------------------------
'======================================
type point3d
x AS single
y AS single
z AS single
declare sub draw
end type
type camera
x AS single
y AS single
z AS single
declare sub move
end type
type rope
points(200,200) as point3d
declare sub render(cam as camera)
declare sub move
end type
dim shared as integer LENS=256,FOV=256,XCENTER=320,YCENTER=240
' Main
'==============================================
dim as integer mx,my,click
dim shared rope1 as rope
screenres 640,480,32,,1
for z as integer=0 to ubound(rope1.points,1)
for x as integer=0 to ubound(rope1.points,2)
rope1.points(x,z).x=(x*20)-480
rope1.points(x,z).z=(z*20)-70
rope1.points(x,z).y=0
next
next
dim as camera mycamera
do
getmouse mx,my,,click
screenlock
cls
rope1.render(mycamera)
screenunlock
rope1.move
mycamera.move
dim as integer xx=(mx+30)\5,yy=my\5
if xx<1 then xx=1
if yy<1 then yy=1
if xx>ubound(rope1.points,1)-1 then xx=ubound(rope1.points,1)-1
if yy>ubound(rope1.points,1)-1 then xx=ubound(rope1.points,1)-1
if click and 2 then rope1.points(xx,yy).y+=5000:rope1.move:rope1.move:rope1.move:rope1.move
if click and 1 then rope1.points(xx,yy).y-=5000:rope1.move:rope1.move:rope1.move:rope1.move
'if click=2 then rope1.points(ubound(rope1.points)).x=mx:rope1.points(ubound(rope1.points)).y=my
sleep 10
loop until multikey(1)
'==============================================
sub rope.move
for y as integer=1 to ubound(points,1)-1
for x as integer=1 to ubound(points,2)-1
dim as single py
py=(points(x+1,y).y+points(x-1,y).y+points(x,y+1).y+points(x,y-1).y)/4
points(x,y).y=py'-=1
points(x,y).y=py'+=1
next
next
end sub
sub rope.render(cam as camera)
for y as integer=ubound(points,1)-1 to 1 step -1
for x as integer=ubound(points,2)-1 to 1 step -1
dim as single a,b,aa(3),bb(3)
a=(points(x,y).x-cam.x) / ((points(x,y).z-cam.z) + LENS) * FOV + XCENTER
b=(points(x,y).y+200-cam.y) / ((points(x,y).z-cam.z) + LENS) * FOV + YCENTER
for i as integer=0 to 3
dim as integer x2=x,y2=y
select case i
case 0
y2-=1
case 1
y2+=1
case 2
x2-=1
case 3
x2+=1
end select
aa(i) = (points(x2,y2).x-cam.x) / ((points(x2,y2).z-cam.z) + LENS) * FOV + XCENTER
bb(i) = (points(x2,y2).y+200-cam.y) / ((points(x2,y2).z-cam.z) + LENS) * FOV + YCENTER
next
dim as integer g=(points(x,y).y)/5
if g<0 then g=-g
if (points(x,y).z-cam.z)>-240 then line (a,b)-(aa(0),bb(0)),rgb(60,g,(g/2)/2)
if (points(x,y).z-cam.z)>-240 then line (a,b)-(aa(1),bb(1)),rgb(60,g,(g/2)/2)
if (points(x,y).z-cam.z)>-240 then line (a,b)-(aa(2),bb(2)),rgb(60,g,(g/2)/2)
if (points(x,y).z-cam.z)>-240 then line (a,b)-(aa(3),bb(3)),rgb(60,g,(g/2)/2)
'circle (a,b),3,rgb(0,points(x,y).y,0)
next
next
end sub
sub camera.move
if multikey(17) then z+=5
if multikey(30) then x-=5
if multikey(31) then z-=5
if multikey(32) then x+=5
if multikey(16) then y+=5
if multikey(18) then y-=5
end sub
Here is something more like water.
Code: Select all
'=====================================
'-------------------------------------
'Fluid Simulation by Ezekiel Gutierrez
'-------------------------------------
'=====================================
Const wid = 640, hei = 480, hhei = hei Shr 1
Const As Single k = 0.025
Const maxPts = 60, pdist = 10
dim shared as integer LENS=256,FOV=256,XCENTER=320,YCENTER=240
type camera
x AS single
y AS single
z AS single
declare sub move
end type
Type wpoint
As Single velocity,y,x,z
declare sub render(cam as camera)
end type
dim as camera mycamera:mycamera.x=300:mycamera.y=300
Dim As wpoint points(maxPts,maxPts)
'init values
For x As UInteger = 0 To maxPts
For z As UInteger = 0 To maxPts
With points(x,z)
.y = hhei
.velocity = 0.0
.x=x*pdist:.z=z*pdist
End With
next
Next
' ===== MAIN =====
ScreenRes(wid, hei, 32)
Dim As Integer mx, my,click
Dim As Single l(maxPts,maxPts), r(maxPts,maxPts)
Do
ScreenLock
Cls
For x As UInteger = 0 To maxPts
For z As UInteger = 0 To maxPts
points(x,z).render(mycamera)
next
Next
ScreenUnLock
For x As UInteger = 0 To maxPts
For z As UInteger = 0 To maxPts
With points(x,z)
Var x1 = hhei - .y
.velocity += k * x1 - k * .velocity
.y += .velocity
End with
next
Next
For j as Uinteger = 0 To 9
For x As UInteger = 0 To maxPts
For z As UInteger = 0 To maxPts
If x > 0 Then
l(x,z) = k * (points(x,z).y - points(x-1,z).y)
points(x-1,z).velocity += l(x,z)
End If
If x < maxPts Then
r(x,z) = k * (points(x,z).y - points(x+1,z).y)
points(x+1,z).velocity += r(x,z)
End If
If x > 0 Then points(x-1,z).y += l(x,z)
If x < maxPts Then points(x+1,z).y += r(x,z)
If z > 0 Then
l(x,z) = k * (points(x,z).y - points(x,z-1).y)
points(x,z-1).velocity += l(x,z)
End If
If z < maxPts Then
r(x,z) = k * (points(x,z).y - points(x,z+1).y)
points(x,z+1).velocity += r(x,z)
End If
If z > 0 Then points(x,z-1).y += l(x,z)
If z < maxPts Then points(x,z+1).y += r(x,z)
next
Next
Next
mycamera.move
GetMouse mx,my,,click
if click=1 then points(30,30).y-=300
Sleep 50
Loop Until MultiKey(1)
sub wpoint.render(cam as camera)
dim as single a,b,aa(3),bb(3)
a=(x-cam.x) / ((z-cam.z) + LENS) * FOV + XCENTER
b=(y+200-cam.y) / ((z-cam.z) + LENS) * FOV + YCENTER
if (z-cam.z)>-240 then circle (a,b),1,rgb(0,0,140+y)
end sub
sub camera.move
if multikey(17) then z+=5
if multikey(30) then x-=5
if multikey(31) then z-=5
if multikey(32) then x+=5
if multikey(16) then y+=5
if multikey(18) then y-=5
end sub