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

Username:   Password: 
 RegisterRegister   
 TheOneTrueGod's Particle System
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TheOneTrueGod




PostPosted: Mon May 01, 2006 5:02 pm   Post subject: TheOneTrueGod's Particle System

Well, since everyone else made particle systems, I decided "Why not me too?" so here's my spiffy particle system (Morphed to resemble fireworks)

code:

const Wind := 0 %Affects motion of particles
const FireWorkRate := 150 %Less is more.

class Partical
    import Wind
    export Create, Manage
    const xdecay := 1.01
    const ydecay := 0.03
    var P : flexible array 1 .. 0 of
        record
            x, y, xs, ys, r, g, b : real
        end record
    var theta, rad : real

    procedure Create (x_, y_, xs_, ys_, r_, g_, b_ : real, amount : int)
        for i : 1 .. amount
            new P, upper (P) + 1

            theta := Rand.Int (0, 360)
            rad := Rand.Int (0, 200) / 100

            P (i).x := x_
            P (i).y := y_
            P (i).xs := xs_ / 2 + cosd (theta) * rad
            P (i).ys := ys_ / 2 + sind (theta) * rad
            P (i).r := r_
            P (i).g := g_
            P (i).b := b_
        end for
    end Create

    procedure ReduceColour (var C : real)
        if C > 0 then
            C -= Rand.Int (3, 10) / 10
        else
            C := 0
        end if
    end ReduceColour

    procedure Manage
        for i : 1 .. upper (P)
            drawdot (round (P (i).x), round (P (i).y), RGB.AddColour (P (i).r / 255, P (i).g / 255, P (i).b / 255))
            P (i).x += P (i).xs
            P (i).y += P (i).ys

            P (i).xs /= xdecay
            P (i).ys -= ydecay
            P (i).xs += Wind

            if P (i).ys < -1 then
                P (i).ys += ydecay * 2
            end if

            ReduceColour (P (i).r)
            ReduceColour (P (i).g)
            ReduceColour (P (i).b)

            if P (i).y < 0 or P (i).r = 0 and P (i).g = 0 and P (i).b = 0 then
                P (i) := P (upper (P))
                new P, upper (P) - 1
                exit
            end if
        end for
    end Manage

end Partical

View.Set ('offscreenonly')

Text.ColourBack (black)
cls
var P : flexible array 1 .. 0 of ^Partical
%new
%new Partical, P
%Partical (P).Create (maxx div 2, maxy div 2, 0, 0, 250)
var FireWork : flexible array 1 .. 0 of
    record
        x, y, xs, ys, r, g, b : real
    end record
var c : int := FireWorkRate - 1

loop
    c += 1
    if c mod FireWorkRate = 0 then
        new FireWork, upper (FireWork) + 1
        FireWork (upper (FireWork)).x := Rand.Int (0, maxx)
        FireWork (upper (FireWork)).y := 0
        FireWork (upper (FireWork)).xs := 0
        FireWork (upper (FireWork)).ys := Rand.Int (20, 55) / 10
        FireWork (upper (FireWork)).r := Rand.Int (0, 1) * 255
        FireWork (upper (FireWork)).g := Rand.Int (0, 1) * 255
        FireWork (upper (FireWork)).b := Rand.Int (0, 1) * 255
        c := 0
    end if

    for i : 1 .. upper (FireWork)
        FireWork (i).x += FireWork (i).xs
        FireWork (i).y += FireWork (i).ys
        FireWork (i).xs += Wind / 2
        FireWork (i).ys -= 0.03
        if FireWork (i).ys <= 0 or FireWork (i).y > 230 then
            new P, upper (P) + 1
            %put FireWork (i).x, FireWork (i).y, FireWork (i).xs, FireWork (i).ys
            new Partical, P (upper (P))
            Partical (P (upper (P))).Create (FireWork (i).x, FireWork (i).y, FireWork (i).xs, FireWork (i).ys, FireWork (i).r, FireWork (i).g, FireWork (i).b, 200)
            FireWork (i) := FireWork (upper (FireWork))
            new FireWork, upper (FireWork) - 1
            exit
        end if
        drawfilloval (round (FireWork (i).x), round (FireWork (i).y), 3, 3, RGB.AddColour (FireWork (i).r / 255, FireWork (i).g / 255, FireWork (i).b / 255))
    end for
    for i : 1 .. upper (P)
        Partical (P (i)).Manage
    end for
    View.Update
    Time.DelaySinceLast (30)
    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
upthescale




PostPosted: Mon May 01, 2006 5:31 pm   Post subject: (No subject)

wow u are a god!
Clayton




PostPosted: Tue May 02, 2006 9:17 pm   Post subject: (No subject)

nice, im not exactly sure whats going on in your code (although i have a faint idea, im not quite at your level yet), but i was playing around with it a bit (specifically the wind constant) i think you should either lessen its effect or something (just so it can actually be used more effectively) b/c i gave it a value and when i ran the program the "fire work" just kind of blew across my screen and right across, nice job though. another thing, could i ask you why you chose to do this (im not sure if this is the better way or not, im just asking)
Turing:

var FireWork: flexible array 1..0 of
    record
        x,y, %etc.. :int
    end record

as opposed to
Turing:

type firework
    record
        x,y, %etc.. :int
    end record
var FireWork : flexible array 1..0 of firework

im just wondering about that b/c i didnt know you could do that

@upthescale: what is it with you and spamming on this site, its really annoying! use the PM system to say things like that, maybe you should read the rules to find out whats allowed and what isnt
TheOneTrueGod




PostPosted: Tue May 02, 2006 9:30 pm   Post subject: (No subject)

I don't believe theres one thats better or not (You'd have to check with a mod / more knowledgable person).

I chose to do it one way because it reduces the amount of lines of code, and it makes it so I don't have to have the (IMO) useless variable of "firework" If i was going to create multiple instances of "firework", then I might want to use that method.

A very bad example of the above:
code:

type firework :
    record
        x,y :int
    end record

var FireWork : flexible array 1..0 of firework
var TheLastPieceOfPie : firework
var Last_Weeks_Dinner : array 1..3 of firework

type FireWork2 :
   record
       f : firework %I don't know if this would actually work, and i'm feeling
                        %kinda lazy right now so I won't test it.
       duration : int
    end record


Anyone more knowledgable want to add something?

Edit: After reading some of catalysts old posts, I discovered another implementation of it. You can return a record from a function. For example,

code:

type CoOrd :
   record
      x,y : int
   end record

function Location (x_,y_ : int) : CoOrd
    var C : CoOrd
    C.x := x_
    C.y := y_
    result C
end Location


This may seem useless, but it has quite powerful applications in things like, say, the mandlebrot set.
The mandlebrot set has probably (almost definetly) been posted here before, so you can just view it there. If you cannot find it, just send me a request and i'll try and program it and post it.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: