Posted: 20th May 2003 1:24
I've noticed that some people are wondering how to make a rain effect, so I made this. It is made in DB Classic, not sure if it works in Pro, but I dont see why not. Also, I'm not sure about the speed on other machines, as it goes pretty fast on mine, so it'll probably fly on most of yours

Edit: As I took down the image, I took the useless link away.

+ Code Snippet
`Rain particle generator
`By Kentaree 19/05/03
`http://members.lycos.co.uk/darknessrising/

`Setup program environment.
sync on
sync rate 60
autocam off

`Load media.
load image "rain.jpg",1

`Set up an array holding position and speed of particles
dim rain(50,4)
xPos=0
yPos=1
zPos=2
speed=3

`Position the camera
position camera 0,12,-20

`Make all the particles
gosub _setupParticles

`Main game loop
do
randomize timer()
gosub _moveParticles
sync
loop


_setupParticles:
`Make textured and ghosted plains numbered 1 to 50, and set transparency on
for i=1 to 50
  make object plain i, 1,1
  set object i,1,0,0
  texture object i,1
  ghost object on i
  `Setup array values for each particles holding the positional data
  rain(i-1,xPos)=(int(rnd(40)+1))-20: `X coordinate of particle
  rain(i-1,yPos)=25: `Y coordinate of particle
  rain(i-1,zPos)=(int(rnd(5)+1)): `Z coordinate of particle
  rain(i-1,speed)=(int(rnd(3)+1)): `Speed of particle
  `Position object for start
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return

_moveParticles:
for i=1 to 50
  `Move particle down according to speed value, which makes a random effect
  rain(i-1,yPos)=rain(i-1,yPos)-rain(i-1,speed)
  `If particle goes too low, set new position
  if rain(i-1,yPos)<0
    rain(i-1,xPos)=(int(rnd(40)+1))-20
    rain(i-1,yPos)=25
    rain(i-1,zPos)=(int(rnd(5)+1))
    rain(i-1,speed)=(int(rnd(3)+1))
  endif
  `Position particle
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return
Posted: 20th May 2003 1:51
Ok, a small edit: This code will work better, the rain will not be overlapped by objects and it ignores the camera's movements so it is always on screen, so if you want to use the code in your programs, use this instead:

+ Code Snippet
`Rain particle generator v1.1
`By Kentaree 19/05/03
`http://members.lycos.co.uk/darknessrising/

`Setup program environment.
sync on
sync rate 60
autocam off

`Load media.
load image "rain.jpg",1

`Set up an array holding position and speed of particles
dim rain(50,4)
xPos=0
yPos=1
zPos=2
speed=3

`Make all the particles
gosub _setupParticles

`Main game loop
do
randomize timer()
gosub _moveParticles
sync
loop


_setupParticles:
`Make textured and ghosted plains numbered 1 to 50, and set transparency on
for i=1 to 50
  make object plain i, 1,1
  set object i,1,0,0
  texture object i,1
  ghost object on i
  disable object zdepth i
  lock object on i
  `Setup array values for each particles holding the positional data
  rain(i-1,xPos)=(int(rnd(40)+1))-20: `X coordinate of particle
  rain(i-1,yPos)=25: `Y coordinate of particle
  rain(i-1,zPos)=(int(rnd(5)+1))+20: `Z coordinate of particle
  rain(i-1,speed)=(int(rnd(3)+1)): `Speed of particle
  `Position object for start
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return

_moveParticles:
for i=1 to 50
  `Move particle down according to speed value, which makes a random effect
  rain(i-1,yPos)=rain(i-1,yPos)-rain(i-1,speed)
  `If particle goes too low, set new position
  if rain(i-1,yPos)<-15
    rain(i-1,xPos)=(int(rnd(40)+1))-20
    rain(i-1,yPos)=25
    rain(i-1,zPos)=(int(rnd(5)+1))+20
    rain(i-1,speed)=(int(rnd(3)+1))
  endif
  `Position particle
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return
Posted: 20th May 2003 3:47
Nice Ran at 62 FPS in classic for me That rain does seem a little big though, but might just be me. Great job kent

RPGamer
Posted: 20th May 2003 14:31
You could save on polygons by making triangles instead of planes. just scale the y to stretch them. Would go from 100 polys to 50.

-Kensupen
Posted: 20th May 2003 16:21
If they seem to big, you could try to place them back more, using the zPos() array. Add 30 instead of 20, or any other value. Doing this though, you might also have to adjust the height at which they start and stop. It's very customisable by just changing a few things.
Posted: 20th May 2003 19:37
yeah effect and thx
Posted: 22nd May 2003 17:40
kentaree is there a way to randomsize where then fall in the scene so in a 3d scene it looks like they are in the scene there then just in front. would you increase the zpos randomiser thing to a great number
Posted: 22nd May 2003 19:41
Uhm, could you explain more clearly what you mean?
The zPos variable is for how close or far away it is. If you want the rain closer you'll change the +20 to a smaller number, if you want it further away, you increase it. Also, if you want the rain to be overlapped by objects, i.e, its not in the front, you can remove the line disable object zdepth i.
Posted: 22nd May 2003 21:59
what i want is at the moment it looks like it is just going over the screen. i want it to go in the 3d world. I know this isn't reallybare with me. the first is how it is at the moment the second how i would like it


okay that i kinda a side view of it
Posted: 23rd May 2003 1:59
i think you just need to increase the z variable (eg to 200) and remove the line 'disable object zdepth' (like kentaree already said )

ok, im feeling generous. try this:
+ Code Snippet
`Rain particle generator v1.1
`By Kentaree 19/05/03
`http://members.lycos.co.uk/darknessrising/

`Setup program environment.
sync on
sync rate 60
autocam off

`Load media.
load image "rain.jpg",1

`Set up an array holding position and speed of particles
dim rain(50,4)
xPos=0
yPos=1
zPos=2
speed=3

`Make all the particles
gosub _setupParticles

`Main game loop
do
randomize timer()
gosub _moveParticles
sync
loop


_setupParticles:
`Make textured and ghosted plains numbered 1 to 50, and set transparency on
for i=1 to 50
  make object plain i, 1,1
  set object i,1,0,0
  texture object i,1
  ghost object on i
  lock object on i
  `Setup array values for each particles holding the positional data
  rain(i-1,xPos)=(int(rnd(40)+1))-20: `X coordinate of particle
  rain(i-1,yPos)=25: `Y coordinate of particle
  rain(i-1,zPos)=(int(rnd(5)+1))+200: `Z coordinate of particle
  rain(i-1,speed)=(int(rnd(3)+1)): `Speed of particle
  `Position object for start
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return

_moveParticles:
for i=1 to 50
  `Move particle down according to speed value, which makes a random effect
  rain(i-1,yPos)=rain(i-1,yPos)-rain(i-1,speed)
  `If particle goes too low, set new position
  if rain(i-1,yPos)<-15
    rain(i-1,xPos)=(int(rnd(40)+1))-20
    rain(i-1,yPos)=25
    rain(i-1,zPos)=(int(rnd(5)+1))+20
    rain(i-1,speed)=(int(rnd(3)+1))
  endif
  `Position particle
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return
Posted: 23rd May 2003 3:58
I don't think that would work because you also have to add 200 in the 'move particle' subroutine.

One way i found to add a lot more random depth is to change the random statement for the zpos into a big number like 500 instead of just 5 and then it well spread itself out. This works a lot better than just adding 200 cause that would just move all of the drops back to the same depth. here's the code incase you didn't understand what i said

+ Code Snippet
`Rain particle generator v1.1
`By Kentaree 19/05/03
`http://members.lycos.co.uk/darknessrising/

`Setup program environment.
sync on
sync rate 60
autocam off

`Load media.
load image "C:Program FilesDark Basic SoftwareDark Basic Litemediarain.jpg",1

`Set up an array holding position and speed of particles
dim rain(50,4)
xPos=0
yPos=1
zPos=2
speed=3

`Make all the particles
gosub _setupParticles

`Main game loop
do
randomize timer()
gosub _moveParticles
sync
loop


_setupParticles:
`Make textured and ghosted plains numbered 1 to 50, and set transparency on
for i=1 to 50
  make object plain i, 1,1
  set object i,1,0,0
  texture object i,1
  ghost object on i
  disable object zdepth i
  lock object on i
  `Setup array values for each particles holding the positional data
  rain(i-1,xPos)=(int(rnd(40)+1))-20: `X coordinate of particle
  rain(i-1,yPos)=25: `Y coordinate of particle
  rain(i-1,zPos)=(int(rnd(500)+1))+20: `Z coordinate of particle
  rain(i-1,speed)=(int(rnd(3)+1)): `Speed of particle
  `Position object for start
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return

_moveParticles:
for i=1 to 50
  `Move particle down according to speed value, which makes a random effect
  rain(i-1,yPos)=rain(i-1,yPos)-rain(i-1,speed)
  `If particle goes too low, set new position
  if rain(i-1,yPos)<-15
    rain(i-1,xPos)=(int(rnd(40)+1))-20
    rain(i-1,yPos)=25
    rain(i-1,zPos)=(int(rnd(500)+1))+20
    rain(i-1,speed)=(int(rnd(3)+1))
  endif
  `Position particle
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return


you might have to take the disable object zdepth statement out of there but I haven't tried it in a scene yet so i don't know.

by the way, you will have to make all of the drops fill the screen again because when you put them farther back, they take up less screen so you get a smaller box of rain that doesn't fill the whole entire screen, and you could also always add more rain drops too.

Awesome code by the way
Posted: 25th May 2003 18:30
Sorry for taking so long to reply.


Right on, Duncala . Changing the randomizing factor to get the z position increases the range of the particles' depth. Change the value in both for..next to a greater one, like from 50 to 100, or higher to get more particles. Obviously, because of the increased particle depth, some particles will seem to appear midscreen and they wont range to the sides. I'm currently working on a code to fix this, not sure when it'll be done though.
Posted: 25th May 2003 19:25
already there kentaree just increase the randomize factor for the xpos. personally i think it looks alot better if you increase the amount of drops by increaseing the for next loop but remember o increase the array by the same amount
Posted: 26th May 2003 3:36
K, I think i got it. I basically just work with it and found the proportion of the depth to the x and y max that's on the screen, that's what the .8 and .6 is. Now, it fills the whole screen. Now I need to figure out how to make the drops in the very back move faster cause they go really really slow. Hope this helps. I increased the number of drops to 500 to look more realistic.

+ Code Snippet
`Rain particle generator v1.1
`By Kentaree 19/05/03
`http://members.lycos.co.uk/darknessrising/

`Setup program environment.
sync on
sync rate 60
autocam off

`Load media.
load image "C:\Program Files\Dark Basic Software\Dark Basic Lite\media\rain.jpg",1

`Set up an array holding position and speed of particles
dim rain(500,4)
xPos=0
yPos=1
zPos=2
speed=3

`Make all the particles
gosub _setupParticles

`Main game loop
do
randomize timer()
gosub _moveParticles
sync
loop


_setupParticles:
`Make textured and ghosted plains numbered 1 to 50, and set transparency on
for i=1 to 500
  make object plain i, 1,1
  set object i,1,0,0
  texture object i,1
  ghost object on i
  disable object zdepth i
  lock object on i
  `Setup array values for each particles holding the positional data
  rain(i-1,zPos)=(int(rnd(500)+1))+20: `Z coordinate of particle
  rain(i-1,xPos)=(int(rnd(rain(i-1,zPos)*.8)*2+1))-rain(i-1,zPos)*.8: `X coordinate of particle
  rain(i-1,yPos)=(int(rnd(rain(i-1,zPos)*.6)*2+1)): `Y coordinate of particle
  rain(i-1,speed)=(int(rnd(3)+1)): `Speed of particle
  `Position object for start
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return

_moveParticles:
for i=1 to 500
  `Move particle down according to speed value, which makes a random effect
  rain(i-1,yPos)=rain(i-1,yPos)-rain(i-1,speed)
  `If particle goes too low, set new position
  if rain(i-1,yPos)<-15
    rain(i-1,zPos)=(int(rnd(500)+1))+20
    rain(i-1,xPos)=(int(rnd(rain(i-1,zPos)*.8)*2+1))-rain(i-1,zPos)*.8
    rain(i-1,yPos)=(int(rnd(rain(i-1,zPos)*.6)*2+1))
    rain(i-1,speed)=(int(rnd(3)+1))
  endif
  `Position particle
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return
Posted: 26th May 2003 18:07
To increase the speed of the drops in the back, just use something like:

rain(i-1,speed)=(int(rnd(3)+1))+rain(i-1,zPos)

The further back the drops are the faster they'll fall. Good code for getting the x and y values corresponding to the depth btw. I was working on something like it, but I was wondering just how the heck I'd get it to work properly, thanx
Posted: 29th May 2003 1:40
All right, I think i got it. For the speed i just did the z position times the .6 for the y size, and then i just divided by 50 to slow it all down a little. Here it is:
+ Code Snippet
`Rain particle generator v1.1
`By Kentaree 19/05/03
`http://members.lycos.co.uk/darknessrising/

`Setup program environment.
sync on
sync rate 60
autocam off

`Load media.
load image "C:Program FilesDark Basic SoftwareDark Basic Litemediarain.jpg",1

`Set up an array holding position and speed of particles
dim rain(50,4)
xPos=0
yPos=1
zPos=2
speed=3
`Make all the particles
gosub _setupParticles

`Main game loop
do
randomize timer()
gosub _moveParticles
sync
loop


_setupParticles:
`Make textured and ghosted plains numbered 1 to 50, and set transparency on
for i=1 to 50
  make object plain i, 1,1
  set object i,1,0,0
  texture object i,1
  ghost object on i
  disable object zdepth i
  lock object on i
  `Setup array values for each particles holding the positional data
  rain(i-1,zPos)=(int(rnd(500)+1))+20: `Z coordinate of particle
  rain(i-1,xPos)=(int(rnd(rain(i-1,zPos)*.8)*2+1))-rain(i-1,zPos)*.8: `X coordinate of particle
  rain(i-1,yPos)=(int(rnd(rain(i-1,zPos)*.6)*2+1)): `Y coordinate of particle
  rain(i-1,speed)=(int(rnd(3)+1+(rain(i-1,zPos)*.6/50))): `Speed of particle
  `Position object for start
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return

_moveParticles:
for i=1 to 50
  `Move particle down according to speed value, which makes a random effect
  rain(i-1,yPos)=rain(i-1,yPos)-rain(i-1,speed)
  `If particle goes too low, set new position
  if rain(i-1,yPos)<-15
    rain(i-1,zPos)=(int(rnd(500)+1))+20
    rain(i-1,xPos)=(int(rnd(rain(i-1,zPos)*.8)*2+1))-rain(i-1,zPos)*.8
    rain(i-1,yPos)=(int(rnd(rain(i-1,zPos)*.6)*2+1))
    rain(i-1,speed)=(int(rnd(3)+1+(rain(i-1,zPos)*.6/50)))
  endif
  `Position particle
  position object i, rain(i-1,xPos), rain(i-1,yPos), rain(i-1,zPos)
next i
return


by the way, is it allright if i use this in my code with you in the creds Kentaree?

Anybody up to makin it splash?

Peace
Posted: 30th May 2003 11:53
it looks great !!!

but the rain is a little big, but that just might be my big ass computer
Posted: 31st May 2003 12:54
@Duncala: Sure you can! There'd be no point in releasing this code if people weren't allowed to use it

@CoDeR Of ThE NiGhT: If they are too big, you can adjust the size in the _setupParticles subroutine. For example, if you want them half the current size, you'd use make object plain i, .5,.5 instead of make object plain i, 1, 1.
Posted: 3rd Jun 2003 12:15
I just drew a small vertical line in paint with a black background, coloured it light blue and saved it - the rain now looks incredibly realistic, unlike the original image - try it!
Posted: 3rd Jun 2003 13:07
lol yep, It looks far better this way, thanks for the tip.