Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Particle system for newbies(rain,snow,smoke,etc)
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Homer_simpson




PostPosted: Sat Jan 03, 2009 6:01 pm   Post subject: Particle system for newbies(rain,snow,smoke,etc)

i've seen that a lot of you like to make animations with turing, so here's a little tutorial on how you can incorporate a particle system into your animations. you could use it to make rain, snow,smoke,etc


here's the steps to creating a particle system

set a type for particles with its required attributes in this example we're going to setup a rain system meaning every drop of rain will be a particle.
every drop of rain has a few attributes : x position, y position, falling speed
so we declare:

type Particle_Type :
record
x, y,speed : real
end record

now we need a lot of these particles so we declare an array of this type
so we declare an array of a 100 particles:

var Particles : array 1 .. 100 of Particle_Type

now we have to give these particles their initial values :

for i : 1 .. 100
Particles (i).x := Rand.Int (0, maxx) %randmize the x position of rain droplets from 0 to maxx
Particles (i).y := Rand.Int (0, maxy) %randmize the y position
Particles (i).speed := Rand.Int (3, 5) %randmize the speed of drops rain
end for


after we've setup all of our particles it's time to move them and draw them
so we start our animation loop like so:

loop
move particles
draw particles
check to see if the particles need to be renewed
clear the screen
end loop

in code:

loop %main animation loop
cls %clear the screen

for i : 1 .. MaxParticles %inside this loop we go through all the particles 1 by 1
Particles (i).y -= Particles (i).speed %move down the drop of rain

if Particles (i).y <= 0 then %if drop of rain has fallen to the ground, reset it's values
Particles (i).y := maxy %set it's y location back to the top again
Particles (i).x := Rand.Int (0, maxx) %randmize the x position of rain droplets from 0 to maxx
Particles (i).speed := Rand.Int (3, 5) %randmize the speed of drops rain

end if

Draw.ThickLine (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x), round (Particles (i).y + (Particles (i).speed)), 3, 11) %draw the drop of rain

end for
View.Update%update the screen
end loop






put it all together and you get:
Quote:
View.Set ("offscreenonly")
colorback (blue)
const MaxParticles := 500
const startfirex := 100
const startfirey := 100
const smokelength := 300

type Particle_Type : %declare type
record
x, y, speed : real
end record

var Particles : array 1 .. MaxParticles of Particle_Type %declare array of type that you've made


for i : 1 .. MaxParticles % give particles their initial x,y value
Particles (i).x := Rand.Int (0, maxx) %randmize the x position
Particles (i).y := Rand.Int (0, maxy) %randmize the y position
Particles (i).speed := Rand.Int (3, 5) %randmize the y position


end for


loop %main animation loop
cls %clear the screen

for i : 1 .. MaxParticles %inside this loop we go through all the particles 1 by 1
Particles (i).y -= Particles (i).speed %move down the drop of rain

if Particles (i).y <= 0 then %if drop of rain has fallen to the ground, reset it's values
Particles (i).y := maxy %set it's y location back to the top again
Particles (i).x := Rand.Int (0, maxx) %randmize the x position of rain droplets from 0 to maxx
Particles (i).speed := Rand.Int (3, 5) %randmize the speed of drops rain

end if

Draw.ThickLine (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x), round (Particles (i).y + (Particles (i).speed)), 3, 11) %draw the drop of rain

end for
View.Update
end loop

Sponsor
Sponsor
Sponsor
sponsor
Homer_simpson




PostPosted: Sat Jan 03, 2009 6:28 pm   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

here's a slight variation for motivation:
Quote:
View.Set ("offscreenonly")
colorback (blue)
const MaxParticles := 1500
const startfirex := 100
const startfirey := 100
const smokelength := 300

type Particle_Type : %declare type
record
x, y, vx, vy : real
end record

var Particles : array 1 .. MaxParticles of Particle_Type %declare array of type that you've made

proc renewp (i : int, x, y, vx, vy : real)
Particles (i).x := x %randmize the x position
Particles (i).y := y %randmize the y position
Particles (i).vx := vx %randmize the y position
Particles (i).vy := vy %randmize the y position
end renewp

for i : 1 .. MaxParticles % give particles their initial x,y value
renewp (i, 400, Rand.Int (1, 300), Rand.Int (1, 50) / 10, Rand.Int (1, 50) / 10)
end for


loop %main animation loop
cls %clear the screen

for i : 1 .. MaxParticles %inside this loop we go through all the particles 1 by 1
Particles (i).x -= Particles (i).vx %move down the drop of rain
Particles (i).y += Particles (i).vy %move down the drop of rain
Particles (i).vy -= .9
if Particles (i).y <= 0 then %if drop of rain has fallen to the ground, reset it's values
renewp (i, 400, 300, Rand.Int (1, 50) / 10, Rand.Int (1, 50) / 10)

end if

% drawfilloval(round (Particles (i).x), round (Particles (i).y), round ( (Particles (i).vx)), round ( (Particles (i).vy)), 11) %draw the drop of rain
%drawdot(round (Particles (i).x), round (Particles (i).y), 11) %draw the drop of rain
%Draw.ThickLine (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x-Particles (i).vx), round (Particles (i).y+Particles (i).vy), 2,abs(round (Particles (i).vy+90))) %draw the drop of rain
drawline (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x - Particles (i).vx), round (Particles (i).y + Particles (i).vy), (round (Particles (i).vy + 60)))
%draw the drop of rain
end for
View.Update
end loop

revangrey




PostPosted: Sat Jan 03, 2009 10:10 pm   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

umm maybe a mod might want to fix the:


const startfirex := 100
const startfirey := 100

I think you left that in from the smoke program you wrote...

speaking of that how would you move the smoke to a different area of the screen?

or should I have posted this in the Turing Help section?
Insectoid




PostPosted: Sat Jan 03, 2009 10:25 pm   Post subject: RE:Particle system for newbies(rain,snow,smoke,etc)

To make snow move on the horizontal axis as well, you can add in (in code not following yours)

[code]
particle.x += Rand.Int (-1, 1)
[code]

This will make the snow look more like snow.

To turn this into smoke, just make the snow move up, make the particles smaller and limit the X to the chimney/burning tree/whatever. Hmm...I'm going to have to try that, might be fun

Great job. Loved the rainbow waterfall!

EDIT: Just made some smoke. Just modified homer's code.

Turing:

View.Set ("offscreenonly")
colorback (blue)
const MaxParticles := 1000
const startfirex := 100
const startfirey := 100
const smokelength := 300
var smokeX := 50
var smokeWidth := 10
var smokeVel := 1
type Particle_Type : %declare type
    record
        x, y, speed : real
    end record

var Particles : array 1 .. MaxParticles of Particle_Type %declare array of type that you've made


for i : 1 .. MaxParticles % give particles their initial x,y value
    Particles (i).x := Rand.Int (smokeX, smokeX + smokeWidth) %randmize the x position
    Particles (i).y := Rand.Int (0, maxy) %randmize the y position
    Particles (i).speed := 3 %randmize the y position


end for


loop %main animation loop

    for x : 1 .. MaxParticles
        smokeX += smokeVel
        cls %clear the screen
        for i : 1 .. MaxParticles %inside this loop we go through all the particles 1 by 1
            Particles (i).y += Particles (i).speed %move down the drop of rain

            if Particles (i).y >= maxy then %if drop of rain has fallen to the ground, reset it's values
                Particles (i).y := 0 %set it's y location back to the top again
                Particles (i).x := Rand.Int (smokeX, smokeX + smokeWidth) %randmize the x position of rain droplets from 0 to maxx
                Particles (i).speed := 3 %randmize the speed of drops rain
            end if

            Particles (i).x += Rand.Int (-1, 1)
            Draw.Dot (round (Particles (i).x), round (Particles (i).y), grey) %draw the drop of rain

        end for
        View.Update
        if smokeX > smokeWidth or smokeX < 50 then
            smokeVel *= -1
        end if
      delay (10)
    View.Update
         end for

end loop
Homer_simpson




PostPosted: Sun Jan 04, 2009 1:14 am   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

i'm glad u found the code useful Smile
Nick




PostPosted: Wed Jan 07, 2009 1:00 am   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

I did a bit of work I hope you like it Very Happy.


Particle.zip
 Description:

Download
 Filename:  Particle.zip
 Filesize:  1.78 KB
 Downloaded:  756 Time(s)

Homer_simpson




PostPosted: Wed Jan 07, 2009 9:23 pm   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

that's the proper way of doing it, with imported classes and procedures... nice +bits Smile
saltpro15




PostPosted: Fri Jan 16, 2009 9:16 pm   Post subject: RE:Particle system for newbies(rain,snow,smoke,etc)

Thanks a lot for the code Homer!! I've spent a very long time trying to figure out how to make a good particle engine
Sponsor
Sponsor
Sponsor
sponsor
iproballer




PostPosted: Sun Jun 06, 2010 1:13 pm   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

How can you make it go behind something, other objects so that its raining in the background?
Cezna




PostPosted: Sun Jun 06, 2010 1:32 pm   Post subject: RE:Particle system for newbies(rain,snow,smoke,etc)

@ iproballer
You would just draw the particles after you draw whatever is in the foreground.

You might also want to make the colour of the particles darker (assuming the background is black, but they would be lighter if it was white), and the particles size smaller.

For future reference, try not to post to such old topics.
If you have a question, create a new topic.

Source: The Rules, necroposting section
montesser




PostPosted: Wed Nov 10, 2010 2:50 pm   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

Hey, i was wondering if there was a way to change the particles into little yellow or white stars, im trying to do a space travel game and this would be great for it

Homer_simpson @ Sat Jan 03, 2009 6:01 pm wrote:


put it all together and you get:
Quote:
View.Set ("offscreenonly")
colorback (blue)
const MaxParticles := 500
const startfirex := 100
const startfirey := 100
const smokelength := 300

type Particle_Type : %declare type
record
x, y, speed : real
end record

var Particles : array 1 .. MaxParticles of Particle_Type %declare array of type that you've made


for i : 1 .. MaxParticles % give particles their initial x,y value
Particles (i).x := Rand.Int (0, maxx) %randmize the x position
Particles (i).y := Rand.Int (0, maxy) %randmize the y position
Particles (i).speed := Rand.Int (3, 5) %randmize the y position


end for


loop %main animation loop
cls %clear the screen

for i : 1 .. MaxParticles %inside this loop we go through all the particles 1 by 1
Particles (i).y -= Particles (i).speed %move down the drop of rain

if Particles (i).y <= 0 then %if drop of rain has fallen to the ground, reset it's values
Particles (i).y := maxy %set it's y location back to the top again
Particles (i).x := Rand.Int (0, maxx) %randmize the x position of rain droplets from 0 to maxx
Particles (i).speed := Rand.Int (3, 5) %randmize the speed of drops rain

end if

Draw.ThickLine (round (Particles (i).x), round (Particles (i).y), round (Particles (i).x), round (Particles (i).y + (Particles (i).speed)), 3, 11) %draw the drop of rain

end for
View.Update
end loop

Insectoid




PostPosted: Thu Nov 11, 2010 8:12 pm   Post subject: RE:Particle system for newbies(rain,snow,smoke,etc)

of course. Instead of drawing dots, you draw stars. Turing even has a built-in function for it.

If you understand the code you won't have any trouble making it do damn near whatever you want.
montesser




PostPosted: Fri Nov 12, 2010 10:33 am   Post subject: Re: Particle system for newbies(rain,snow,smoke,etc)

well heres the problem, im new to turing and im still learning the program, i can post a code for you to witness what my program is, but other than that. Im still trying to work this code, and ive been able to change the dots to white and the background to black, just not sure what part of the code or how i would have to change it to make them into stars.



PT 2. also, i was wondering how i got it to clear the screen, when i put a delay and cls, it only slows down the animations not stop them
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: