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

Username:   Password: 
 RegisterRegister   
 Particle Engine problems
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Mon Sep 18, 2006 6:43 pm   Post subject: Particle Engine problems

I'm making a particle spray, but I'm having problems with the appearance of the particles, whenever a larger amount is on the screen, the "production" of more particles is basically non-existant for a short period of time, heres my code, if anyone can figure out whats going on it would be appreciated Razz

Turing:

class Particle
    export create_new, move, draw, eliminate
    const gravity : real := -0.1
    var x, y, clr, xv, yv : real

    procedure create_new (x_, y_, xv_, yv_, clr_ : int)
        x := x_
        y := y_
        xv := xv_ + Rand.Real
        yv := yv_ + Rand.Real
        clr := clr_
    end create_new

    procedure move
        y += yv
        x += xv
        yv += gravity
    end move

    procedure draw
        Draw.Dot (round (x), round (y), round (clr))
    end draw

    function eliminate : boolean
        if y < 0 then
            result true
        else
            result false
        end if
    end eliminate
end Particle

var particle : flexible array 1 .. 0 of ^Particle
var elements_to_be_removed : flexible array 1 .. 0 of int
var x, y, b : int

procedure push (element_to_be_removed : int)
    var temp : ^Particle
    for i : element_to_be_removed .. upper (particle) - 1
        temp := particle (i)
        particle (i) := particle (i + 1)
        particle (i + 1) := temp
    end for
    new particle, upper (particle) - 1
end push

View.Set ("offscreenonly,graphics:max;max")
loop
    colorback (black)
    Draw.Cls
    Draw.FillBox (0, 0, maxx, maxy, black)
    Mouse.Where (x, y, b)
    if b >= 1 then
        for i : 1 .. 12
            new particle, upper (particle) + 1
            new Particle, particle (upper (particle))
            particle (upper (particle)) -> create_new (x, y, Rand.Int (-2, 2), Rand.Int (0, 8), yellow)
        end for
    end if
    new elements_to_be_removed, 0
    for i : 1 .. upper (particle)
        particle (i) -> move
        particle (i) -> draw
        if particle (i) -> eliminate then
            new elements_to_be_removed, upper (elements_to_be_removed) + 1
            elements_to_be_removed (upper (elements_to_be_removed)) := i
        end if
    end for
    for i : 1 .. upper (elements_to_be_removed)
        push (elements_to_be_removed (i))
        free particle (upper (particle))
        if upper (particle) - 1 < 0 then
            new particle, 0
        else
            new particle, upper (particle) - 1
        end if
    end for
    View.Update
    Time.Delay (15)
end loop
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Mon Sep 18, 2006 7:50 pm   Post subject: (No subject)

Here As Requested Freakman, The Way I Decided to fix.

code:

class Particle
    export create_new, move, draw, eliminate
    const gravity : real := -0.1
    var x, y, clr, xv, yv : real

    procedure create_new (x_, y_, xv_, yv_, clr_ : int)
        x := x_
        y := y_
        xv := xv_ + Rand.Real
        yv := yv_ + Rand.Real
        clr := clr_
    end create_new

    procedure move
        y += yv
        x += xv
        yv += gravity
    end move

    procedure draw
        Draw.Dot (round (x), round (y), round (clr))
    end draw

    function eliminate : boolean
        if y < 0 then
            result true
        else
            result false
        end if
    end eliminate
end Particle

View.Set ("offscreenonly,graphics:400;400")

var particle : flexible array 1 .. 0 of ^Particle
var mx, my, mb : int
var current, high : int

loop
    Draw.Cls
    Draw.FillBox (0, 0, maxx, maxy, black)

    Mouse.Where (mx, my, mb)

    %Create Particles If Mouse Button Is Being Pressed
    if mb >= 1 then
        for i : 1 .. 12
            new particle, upper (particle) + 1
            new Particle, particle (upper (particle))
            particle (upper (particle)) -> create_new (mx, my, Rand.Int (-2, 2), Rand.Int (0, 8), yellow)
        end for
    end if

    %For Each Particle
    current := 1
    high := upper (particle)
    loop
        if (current > high) then
            exit
        end if
        if (particle (current) -> eliminate) then
            free particle (current)
            particle (current) := particle (upper (particle))
            new particle, upper (particle) - 1
            high -= 1
        else
            particle (current) -> move ()
            particle (current) -> draw ()
            current += 1
        end if
    end loop

    View.Update
    Time.Delay (15)
end loop
ericfourfour




PostPosted: Tue Sep 19, 2006 9:39 pm   Post subject: (No subject)

I'd recommend making a particle engine following this tutorial (credits go to Martin for finding it). I use it every time.

Anyway, Ultrahex fixed it so I thought I'd give you a few pointers (no pun intended).

The particles should not be 'freed' until you are absolutely done with them or you need more memory. The particles should have a flag saying there dead instead. This lowers the amount of times you allocate/deallocate memory already. Next, when you want to create another particle, find a particle with the dead flag. Then all you have to do is reinitialize it. Once again, you have lowered the amount of times you allocate/deallocate memory.

I have a question for Ultrahex. Why did you cls then draw a black box over the screen?
code:

Draw.Cls
Draw.FillBox (0, 0, maxx, maxy, black)


Incase you didn't already know, you can set the background colour.
code:

colourback (7) %Black background

What this really does, is it sets the colour that will show up behind text. It doesn't really set the screen background.

What cls does, is it outputs a full screen of blank text.
Kind of like this:
code:

put repeat ("\n", maxrow)

What happens when you output a full screen of blank text with black as the text background? The whole screen goes black.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: