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

Username:   Password: 
 RegisterRegister   
 Problem with particle system initialization
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Danjen




PostPosted: Tue Nov 04, 2008 6:52 pm   Post subject: Problem with particle system initialization

I'm trying to make my own particle system, and the particle class works okay. The emitter class gets an error on running saying the variable has no value. Not sure why that's happening, since it's part of an initialize procedure. Sad

Turing:

module math
    export RandReal
    fcn RandReal (num1, num2 : real) : real
        result Rand.Int (round (num1 * 100000), round (num2 * 100000)) / 100000
    end RandReal
end math


class particle
    import math
    export initialize, clear, update, draw, initcolor
    var x, y :
        record
            loc, vel, frc : real
        end record
    var angle : real
    var life, lifeMax : int

    var col :
        record
            r, g, b :
                record
                    s, f : real
                end record
        end record

    proc clear ()
        %Set null values for particle.
        x.loc := 0
        y.loc := 0
        x.vel := 0
        y.vel := 0
        x.frc := 0
        y.frc := 0
        angle := 0
        life := 0
        lifeMax := 0
    end clear
    proc initialize (xloc1, xloc2, yloc1, yloc2, xvel1, xvel2, yvel1, yvel2, xfrc1, xfrc2, yfrc1, yfrc2, angle1, angle2 : real, life1, life2 : int)
        %Set initial values for particle.
        x.loc := math.RandReal (xloc1, xloc2)
        y.loc := math.RandReal (yloc1, yloc2)
        x.vel := math.RandReal (xvel1, xvel2)
        y.vel := math.RandReal (yvel1, yvel2)
        x.frc := math.RandReal (xfrc1, xfrc2)
        y.frc := math.RandReal (yfrc1, yfrc2)
        angle := math.RandReal (angle1, angle2)
        lifeMax := Rand.Int (life1, life2)
        life := lifeMax
    end initialize
    proc initcolor (rs1, rs2, gs1, gs2, bs1, bs2, rf1, rf2, gf1, gf2, bf1, bf2 : real)
        %Generates an initial RGB value and it will gradually fade to second RGB value.
        %12 values needed for randomization, rather than 6. This makes a larger spread
        %in the colors, making it appear more realistic. Random values can be set to
        %the same min/max to make it not random.
        col.r.s := math.RandReal (rs1, rs2)
        col.g.s := math.RandReal (gs1, gs2)
        col.b.s := math.RandReal (bs1, bs2)
        col.r.f := math.RandReal (rf1, rf2)
        col.g.f := math.RandReal (gf1, gf2)
        col.b.f := math.RandReal (bf1, bf2)
    end initcolor

    proc update ()
        %Moves the particle and makes it gradually "die".
        x.loc += x.vel * cosd (angle)             %Moves particle at a horizontal speed of x.vel, and a direction of angle.
        y.loc += y.vel * sind (angle)             %Moves particle at a vertical speed of y.vel, and a direction of angle.
        x.vel -= x.frc             %Adjusts velocity by the amount of friction.
        y.vel -= y.frc             %Adjusts velocity by the amount of friction.

        if life >= 1 then
            life -= 1
        else
            clear ()
        end if
    end update

    proc draw ()
        %Draws the particle.
        if life >= 1 then
            Draw.Dot (
                round (x.loc), round (y.loc),
                RGB.AddColor (
                col.r.f - ((life / lifeMax) * (col.r.f - col.r.s)),
                col.g.f - ((life / lifeMax) * (col.g.f - col.g.s)),
                col.b.f - ((life / lifeMax) * (col.b.f - col.b.s)))
                )
        end if
    end draw
end particle

class emitter
    import particle
    export initialize, initcolor, setMaxParticles
    var maxParticles : int := 0
    var particle_ : flexible array 1 .. maxParticles of pointer to particle

    proc initialize (num : int, xloc1, xloc2, yloc1, yloc2, xvel1, xvel2, yvel1, yvel2, xfrc1, xfrc2, yfrc1, yfrc2, angle1, angle2 : real, life1, life2 : int)
        particle (particle_ (num)).initialize (xloc1, xloc2, yloc1, yloc2, xvel1, xvel2, yvel1, yvel2, xfrc1, xfrc2, yfrc1, yfrc2, angle1, angle2, life1, life2)
    end initialize

    proc initcolor (num : int, rs1, rs2, gs1, gs2, bs1, bs2, rf1, rf2, gf1, gf2, bf1, bf2 : real)
        particle (particle_ (num)).initcolor (rs1, rs2, gs1, gs2, bs1, bs2, rf1, rf2, gf1, gf2, bf1, bf2)
    end initcolor

    proc setMaxParticles (num : int)
        maxParticles := num
        new particle_, maxParticles
    end setMaxParticles

    proc drawParticle (num : int)
        particle (particle_ (num)).draw
        particle (particle_ (num)).update
    end drawParticle
end emitter


View.Set ("offscreenonly")

var spray : pointer to emitter
new emitter, spray

emitter (spray).setMaxParticles (1)
emitter (spray).initialize
    (1,
    200, 200, 210, 210,
    10, 10, 5, 5,
    0, 0, 0, 0,
    - 50, -40, 20, 20
    )
emitter (spray).initcolor
    (1,
    1.0, 1.0, 0.0, 0.0, 0.0, 0.0,
    1.0, 1.0, 0.0, 0.0, 0.0, 0.0
    )
loop
    Draw.Fill (1, 1, 7, 1)

    View.Update
    delay (40)
    cls
end loop
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Nov 05, 2008 12:05 pm   Post subject: RE:Problem with particle system initialization

I don't have Turing on me, but this strikes me as wrong:

Turing:

    var maxParticles : int := 0
    var particle_ : flexible array 1 .. maxParticles of pointer to particle

....

    proc initialize (num : int, xloc1, xloc2, yloc1, yloc2, xvel1, xvel2, yvel1, yvel2, xfrc1, xfrc2, yfrc1, yfrc2, angle1, angle2 : real, life1, life2 : int)
        particle (particle_ (num)).initialize (xloc1, xloc2, yloc1, yloc2, xvel1, xvel2, yvel1, yvel2, xfrc1, xfrc2, yfrc1, yfrc2, angle1, angle2, life1, life2)
    end initialize


Since you never resize the particles array, so it always has 0 entries.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: