Height Field Fluids simulation

BlitzMax Forums/BlitzMax Programming/Height Field Fluids simulation

There is a function in Chipmunk example that fake buoyancy quite well:
Function applyBuoyancy(body:CPBody, gravity:CPVect, damping:Float, dt:Float)

	Const numx:Int = 20
	Const numy:Int = 4
	
	Const stepx:Float = WIDTH/Float(numx)
	Const stepy:Float = HEIGHT/Float(numy)
	
	body.ResetForces()

	For Local x:Int = 0 Until numx
		For Local y:Int = 0 Until numy
			Local pSample:CPVect = Vec2((x + 0.5)*stepx - WIDTH/2, (y + 0.5)*stepy - HEIGHT/2)
			Local p:CPVect = body.Local2World(pSample)
			Local r:CPVect = p.Sub(body.GetPosition())
		
			If p.y > 0 Then
				Local v:CPVect = body.GetVelocity().Add(r.Perp().Mult(body.GetAngularVelocity()))
				Local fDamp:CPVect = v.Mult(-0.0003 * v.Length())
				Local f:CPVect = Vec2(0, -2.0).Add(fDamp)
				body.ApplyForce(f, r)
			End If
		Next
	Next
	
	body.UpdateVelocity(gravity, damping, dt)

End Function

Exactly what I was trying to do since it don't have to be very precise.

In fact my wave code was right !

Just need to tweak the effect by multiplying some values.
SuperStrict
SetGraphicsDriver GLMax2DDriver()
Graphics 640, 480, 0

Const m:Int = 126
Const damping:Float = 1.2
Const waveForce:Float = 100.0
Const factor:Float = 2.0
Local size:Float = 5

Local current:Float[m]
Local previous:Float[m]
Local result:Float[m]

While Not KeyHit(KEY_ESCAPE)
	Cls

	If KeyHit(KEY_SPACE)
		Local pos:Int = Rnd (4, m - 4)
		current[pos - 3] = waveForce
		current[pos - 2] = waveForce
		current[pos - 1] = waveForce
		current[pos + 3] = waveForce
		current[pos + 2] = waveForce
		current[pos + 1] = waveForce
		current[pos] = waveForce
	EndIf
	
	For Local x:Int = 1 To m - 2
	
		result[x]:+(current[x - 1] + current[x + 1] - previous[x] * 3) / 4
		result[x]:*damping

		SetColor 5 * Abs(previous[x]), 100, 100
		DrawRect(x * size, 300, size * 2, - 50 - current[x] * factor)
	Next

	For Local x:Int = 1 To m - 1
		previous[x] = current[x]
		current[x] = result[x]
	Next

'	Strange number occurs with this method :
'	previous = current[..]
'	current = result[..]

	Flip
'	Delay 100
Wend

Replacing drawrect by sprites should be visually enough to approach the "splash" animation of new super mario :-D