Water algorithm messed up a bit?

Blitz3D Forums/Blitz3D Programming/Water algorithm messed up a bit?

Guy Fawkes(Posted 2010) [#1]
How can I make it so my water appears as if you're looking at water on the beach, but I do it w/ a plane? I need to know how to randomly make it slowly sway up and down WITHOUT it eventually going over the top of my level




Stevie G(Posted 2010) [#2]
Use a simple integer timer and increment it as required:

global Mytimer
const Max_Wave_Height = 10

.
.
.

while not keyhit(1)

Mytimer = ( Mytimer + 1 ) mod 360

waterlvl# = sin( Mytimer ) * Max_Wave_Height

positionentity water, 0, waterlvl, 0

renderworld()
flip

wend


Andy(Posted 2010) [#3]
Use sine... This will also simulate the slowdown and reversal of real water

ang#=ang#+0.1
z#=Sin(ang#)
PositionEntity plane,0.0,z#,0.0
If z#> 360 Then z#=0.0

A quick demo, but this is crap code(TM), so replace with your own routine
; CreateTerrain Example 
; --------------------- 
SeedRnd MilliSecs()
Graphics3D 640,480 
SetBuffer BackBuffer() 

camera=CreateCamera() 
PositionEntity camera,0,7,0 

light=CreateLight() 
RotateEntity light,90,0,0 

sea_tex=CreateTexture( 256,256 ) 
SetBuffer TextureBuffer( sea_tex ) 
For x = 0 To 255
For y = 0 To 255
Color 0,0,Rnd(150,155) 
Rect x,y,1,1,1
Next
Next 

ter_tex=CreateTexture( 256,256 ) 
SetBuffer TextureBuffer( ter_tex ) 
For x = 0 To 255
For y = 0 To 255
:Color Rnd(0,50),Rnd(50,255),Rnd(0,50) 
Color 0,200,0
Rect x,y,1,1,1
Next
Next 




; Create terrain 
terrain=CreatePlane() 
EntityTexture terrain,ter_tex
RotateEntity terrain,0,0,5 

 

; Create plane 
plane=CreatePlane() 
EntityTexture plane,sea_tex 



While Not KeyDown( 1 ) 
ang#=ang#+0.1


If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0 
If KeyDown( 203 )=True Then TurnEntity camera,0,1,0 
If KeyDown( 208 )=True Then MoveEntity camera,0,0,-0.05 
If KeyDown( 200 )=True Then MoveEntity camera,0,0,0.05 

z#=Sin(ang#)
PositionEntity plane,0.0,z#,0.0
If z#> 360 Then z#=0.0

RenderWorld 

Text 0,0,"Use cursor keys to move about the terrain" 

Flip 

Wend 

End 



Stevie G(Posted 2010) [#4]
@ Andy, excellent advice - see the post immediately above yours ;)

p.s. What is the point of: "If z#> 360 Then z#=0.0" ?


Andy(Posted 2010) [#5]
You're right... Must be something along the lines of 'Great minds think alike' I suppose :)


xlsior(Posted 2010) [#6]
p.s. What is the point of: "If z#> 360 Then z#=0.0" ?


My guess is that should be " if ang# > 360 then ang# is 0.0" instead?
z# will always be between -1 and 1, given that it's the sin() of ang#...


Stevie G(Posted 2010) [#7]

My guess is that should be " if ang# > 360 then ang# is 0.0" instead?
z# will always be between -1 and 1, given that it's the sin() of ang#...



Clearly, but for the animation to be continuous if should actually be:

If ang# > 360 then ang# = ang# - 360


_PJ_(Posted 2010) [#8]
Keeping it to the sin either way would ensure the 'direction' is continous.


Guy Fawkes(Posted 2010) [#9]
theres a slight problem w/ doing it that way.

i have fixed it, but there was yet another problem w/ it

i cant get it when it reaches 0.7 to go down FROM 0.7 to -0.7 by .01

instead, it goes to a negative and goes back up



the problem is




Stevie G(Posted 2010) [#10]
@Rez,

Yes, there is a slight problem in that you are not doing it the way which was suggested.


Guy Fawkes(Posted 2010) [#11]
but i dont want it to go all the way to 360

i want it to go FROM a set position back down TO set position

i need it to go EXTREMELY slow so it looks like a beach


Stevie G(Posted 2010) [#12]
For someone who clearly has very little experience in programming your attitude sucks towards those trying to help.

A continuous sine wave which is simulating a water wave needs to wrap from 0 to 360 degrees. A ping-pong effect from -0.7 to +0.7 degrees will not look good.

If you really want to try it, instead of :

ang#=ang#+.01

If ang# > 0.7
ang# = .01-ang#
EndIf

If ang# < -0.7
ang# = .01+ang#
EndIf


Use

ang# = ang# + dir#
if abs( ang ) > 0.7 dir# = - dir#


Where you must define Dir# = 0.01 before the loop.


Alternatively, to slow the wave motion in the example below, either lower the value of Wave_Height (to reduce the maximum peak/trough of the wave) or reduce the number which ang is incremented by (to reduce the frequency of the wave). While not explained fully, this is exactly what I suggested in my first post.


; CreateTerrain Example 
; --------------------- 
SeedRnd MilliSecs()
Graphics3D 640,480,0,2
SetBuffer BackBuffer() 

const Wave_Height# = 0.7

camera=CreateCamera() 
PositionEntity camera,0,7,0 

light=CreateLight() 
RotateEntity light,90,0,0 

sea_tex=CreateTexture( 256,256 ) 
SetBuffer TextureBuffer( sea_tex ) 
For x = 0 To 255
For y = 0 To 255
Color 0,0,Rnd(150,155) 
Rect x,y,1,1,1
Next
Next 

ter_tex=CreateTexture( 256,256 ) 
SetBuffer TextureBuffer( ter_tex ) 
For x = 0 To 255
For y = 0 To 255
:Color Rnd(0,50),Rnd(50,255),Rnd(0,50) 
Color 0,200,0
Rect x,y,1,1,1
Next
Next 

SetBuffer BackBuffer()


; Create terrain 
terrain=CreatePlane() 
EntityTexture terrain,ter_tex
RotateEntity terrain,0,0,5 

 

; Create plane 
plane=CreatePlane() 
EntityTexture plane,sea_tex 



While Not KeyHit(1)

ang#= ( ang# + 1 ) Mod 360

If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0 
If KeyDown( 203 )=True Then TurnEntity camera,0,1,0 
If KeyDown( 208 )=True Then MoveEntity camera,0,0,-0.05 
If KeyDown( 200 )=True Then MoveEntity camera,0,0,0.05 

z#= Wave_Height * Sin(ang#)
PositionEntity plane,0,z#,0

UpdateWorld
RenderWorld 

Text 0,0,"Use cursor keys to move about the terrain" 
Text 0,10,"ang#:"+ang#

Flip 

Wend 

End



Guy Fawkes(Posted 2010) [#13]
thanks! that worked wonders!