water / sand sim

Blitz3D Forums/Blitz3D Programming/water / sand sim

prefim(Posted 2011) [#1]
Hi Guys,

I'm playing with a sand/water sim. I'm using a map as my data table as I figured its easier to manipulate than arrays. Can anyone think of a better way of doing this? The water is favouring the left side (because the X loop runs left to right) I'm looking for a more even spread or a better approach altogether.

Graphics 220,220,24,3

size=200
map=CreateImage(200,200)
frames=0
oldTime=MilliSecs() 
SetBuffer ImageBuffer(map)
Color 255,255,255
Line 0,0,0,199
Line 0,199,199,199
Line 199,199,199,0
Line 130,199,130,0
Line 70,199,70,0

.start
	
	LockBuffer()
	WritePixelFast 100,25,-16776961,ImageBuffer(map)
	For y=size To 0 Step -1
		For x=0 To size
			moved=0
			pixel = ReadPixelFast(x,y,ImageBuffer(map))
			
			If pixel=-16776961
				below=ReadPixelFast(x,y+1,ImageBuffer(map))
				lft=ReadPixelFast(x-1,y,ImageBuffer(map))
				rgt=ReadPixelFast(x+1,y,ImageBuffer(map))
				
				;DROP ONLY
				If below=-16777216
					WritePixelFast x,y+1,pixel,ImageBuffer(map)
					WritePixelFast x,y,-16777216,ImageBuffer(map)
					moved=1
				EndIf
				
				;LEFT OR RIGHT CHOICE
				If lft=-16777216 And rgt=-16777216 And moved=0
					
					pick = Int(Rnd(1))
					If pick=0
						WritePixelFast x-1,y,pixel,ImageBuffer(map)
						WritePixelFast x,y,-16777216,ImageBuffer(map)
					EndIf
					If pick>0
						WritePixelFast x+1,y,pixel,ImageBuffer(map)
						WritePixelFast x,y,-16777216,ImageBuffer(map)
					EndIf
					moved=1
				EndIf
				
				;RIGHT ONLY
				If rgt=-16777216 And moved=0
					WritePixelFast x+1,y,pixel,ImageBuffer(map)
					WritePixelFast x,y,-16777216,ImageBuffer(map)
					moved=1
				EndIf
				
				;LEFT ONLY
				If lft=-16777216 And moved=0
					WritePixelFast x-1,y,pixel,ImageBuffer(map)
					WritePixelFast x,y,-16777216,ImageBuffer(map)
					moved=1
				EndIf
				
				
				
			EndIf
			
		Next 
	Next 
	
	UnlockBuffer()
	SetBuffer BackBuffer()
	Cls
	DrawImage map,10,10
	frames=frames+1
	
newTime=MilliSecs() 
time#=newtime-oldtime
fps#=frames/(time#/1000)
Text 0,0, Str$(Int(fps#))
Flip 
Goto start



Rroff(Posted 2011) [#2]
even tho its not a bad bad example of goto... I still shuddered to see it lol... use repeat...forever.

Before running it I changed .start to:

Repeat

	If (KeyHit(1)) Then
		End
	EndIf


and the goto statement to the forever statement - I like to be able to hit escape to exit programs.

Cool little program tho.

EDIT:

Its a bit of a hatchet approach but I added seedrnd millisecs() at the start of the program and changed pick = Int(Rnd(1)) to pick = Int(Rnd(4)) and it offsets the left bias a bit.

Last edited 2011


prefim(Posted 2011) [#3]
Thanks for the thoughts. I'm an old school programmer (spectrum basic) so it takes a lot to get out of the goto habits.

I also had the offset idea after posting but it seems a bit hit and miss. I am hoping someone has an idea ofr a different way of tackling this.

I thought maybe a dynamic array using a for all type statement. Will have to investigate further!


Andy(Posted 2011) [#4]
I get memory access violation in

pixel = ReadPixelFast(x,y,ImageBuffer(map))


Rroff(Posted 2011) [#5]
Try adding "SetBuffer ImageBuffer(map)" just before the first lockbuffer() command (or changing the lockbuffer command to lock and later on unlock the imagebuffer(map).

Last edited 2011