Jump to content

A particle code.


Message added by TopicLocker3000

This topic was automatically locked after 6 months of inactivity. If you are the topic owner, please contact a moderator to have it unlocked.

Recommended Posts

//This is only intened for educational purposes feel free to use.
//Just give credit to zeroyalbros.
{
ps = part_system_create();
pt = part_type_create();
pe = part_emitter_create(ps)
 
ps2 = part_system_create();
pt2 = part_type_create();
pe2 = part_emitter_create(ps2)
 
part_system_depth(ps, 20);
part_type_shape(pt, 7);
part_type_blend(pt, 1);
part_type_alpha3(pt, 1, 0.50, 0);
part_type_color3(pt, 16711680, 33023, 255);
part_type_direction(pt, 90, 90, 0, 0);
part_type_speed(pt, 1, 2, 0, 0);
part_type_gravity(pt, 0, 0)
part_type_size(pt, 0.20, 0.30, 0, 0.20); 
part_type_life(pt, 35, 65);
part_type_orientation(pt, 0, 0, 0, 0, 0);
part_type_scale(pt, 1, 1);  
part_emitter_region(ps, pe, x, x+239, y, y-10, ps_shape_rectangle, ps_distr_linear);
part_emitter_burst(ps, pe, pt, 40);
 
part_system_depth(ps2, 20);
part_type_shape(pt2, 8);
part_type_blend(pt2, 1);
part_type_alpha3(pt2, 1, 0.60, 0.30);
part_type_color3(pt2, 16711680, 33023, 255);
part_type_direction(pt2, 60, 120, 0, 0);
part_type_speed(pt2, 1, 2, 0, 0);
part_type_gravity(pt2, 0, 0);
part_type_size(pt2, 0.30, 0.30, -0.01, 0.05); 
part_type_life(pt2, 45, 96);
part_type_orientation(pt2, 0, 0, 0, 0, 0);
part_type_scale(pt2, 1, 1);  
part_emitter_region(ps2, pe2, x, x+239, y, y-10, ps_shape_rectangle, ps_distr_linear);
part_emitter_burst(ps2, pe2, pt2, 10);
}
Sorry the david I made this in like 30 minuets it will probably look like a blob I don't have time right now!!!
Link to post
Share on other sites

David already made a Particle system.

Plus his code completly overwhelms this one.
David released a Snippet of code A while back for what he was gonna do for his particle system.

He had 114 lines...Of just custom Variables. Just Variables.

I don't wanna begin to even comprenhend the length of his actual 3-D particle system code that does stuff.

This is Davids Particle preview:

 

Link to post
Share on other sites

I don't wanna begin to even comprenhend the length of his actual 3-D particle system code that does stuff.

Spawning:

/// tl_particles_spawn(type)

var t,p,a;
t=argument0
p=new(obj_particle)
p.type=t
p.spawntime=current_step
p.pos[XPOS]=pos[XPOS]
p.pos[YPOS]=pos[YPOS]
p.pos[ZPOS]=pos[ZPOS]
p.frame=0
p.time=0
p.timetolive=randomval(lib.pc_destroy_at_time_seconds,lib.pc_destroy_at_time_israndom,lib.pc_destroy_at_time_random_min,lib.pc_destroy_at_time_random_max)*room_speed
p.xoff=0
p.yoff=0
p.zoff=0

// Position
if (lib.pc_spawn_region_use) {
    switch (lib.pc_spawn_region_type) {
        case "sphere":
            var xyang,zang,dis;
            xyang=random(360)
            zang=random_range(-180,180)
            dis=random_range(-lib.pc_spawn_region_sphere_radius,lib.pc_spawn_region_sphere_radius)
            p.pos[XPOS]+=lengthdir_x(dis,xyang)*lengthdir_x(1,zang)
            p.pos[YPOS]+=lengthdir_y(dis,xyang)*lengthdir_x(1,zang)
            p.pos[ZPOS]+=lengthdir_z(dis,zang)
            break
        case "cube":
            p.pos[XPOS]+=random_range(-lib.pc_spawn_region_cube_size/2,lib.pc_spawn_region_cube_size/2)
            p.pos[YPOS]+=random_range(-lib.pc_spawn_region_cube_size/2,lib.pc_spawn_region_cube_size/2)
            p.pos[ZPOS]+=random_range(-lib.pc_spawn_region_cube_size/2,lib.pc_spawn_region_cube_size/2)
            break
        case "box":
            p.pos[XPOS]+=random_range(-lib.pc_spawn_region_box_size[XPOS]/2,lib.pc_spawn_region_box_size[XPOS]/2)
            p.pos[YPOS]+=random_range(-lib.pc_spawn_region_box_size[YPOS]/2,lib.pc_spawn_region_box_size[YPOS]/2)
            p.pos[ZPOS]+=random_range(-lib.pc_spawn_region_box_size[ZPOS]/2,lib.pc_spawn_region_box_size[ZPOS]/2)
            break
    }
}

// Animation
p.anispeed=randomval(t.sprite_animation_speed,t.sprite_animation_speed_israndom,t.sprite_animation_speed_random_min,t.sprite_animation_speed_random_max)

for (a=XPOS a<=ZPOS a++) {
    // Speed
    p.spd[a]=randomval(t.spd[a*t.spd_extend],t.spd_israndom[a*t.spd_extend],t.spd_random_min[a*t.spd_extend],t.spd_random_max[a*t.spd_extend])/room_speed
    p.spd_add[a]=randomval(t.spd_add[a*t.spd_extend],t.spd_add_israndom[a*t.spd_extend],t.spd_add_random_min[a*t.spd_extend],t.spd_add_random_max[a*t.spd_extend])/room_speed
    p.spd_mul[a]=randomval(t.spd_mul[a*t.spd_extend],t.spd_mul_israndom[a*t.spd_extend],t.spd_mul_random_min[a*t.spd_extend],t.spd_mul_random_max[a*t.spd_extend])
    if (p.spd_mul[a]!=1) repeat (5) p.spd_mul[a]=sqrt(p.spd_mul[a])
    
    // Rotation
    p.rot[a]=randomval(t.rot[a*t.rot_extend],t.rot_israndom[a*t.rot_extend],t.rot_random_min[a*t.rot_extend],t.rot_random_max[a*t.rot_extend])
    p.rot_spd[a]=randomval(t.rot_spd[a*t.rot_spd_extend],t.rot_spd_israndom[a*t.rot_spd_extend],t.rot_spd_random_min[a*t.rot_spd_extend],t.rot_spd_random_max[a*t.rot_spd_extend])/room_speed
    p.rot_spd_add[a]=randomval(t.rot_spd_add[a*t.rot_spd_extend],t.rot_spd_add_israndom[a*t.rot_spd_extend],t.rot_spd_add_random_min[a*t.rot_spd_extend],t.rot_spd_add_random_max[a*t.rot_spd_extend])/room_speed
    p.rot_spd_mul[a]=randomval(t.rot_spd_mul[a*t.rot_spd_extend],t.rot_spd_mul_israndom[a*t.rot_spd_extend],t.rot_spd_mul_random_min[a*t.rot_spd_extend],t.rot_spd_mul_random_max[a*t.rot_spd_extend])
    if (p.rot_spd_mul[a]!=1) repeat (5) p.rot_spd_mul[a]=sqrt(p.rot_spd_mul[a])
}

// Scale
p.scale=randomval(t.scale,t.scale_israndom,t.scale_random_min,t.scale_random_max)
p.scale_add=randomval(t.scale_add,t.scale_add_israndom,t.scale_add_random_min,t.scale_add_random_max)/room_speed

// Alpha
p.alpha=randomval(t.alpha,t.alpha_israndom,t.alpha_random_min,t.alpha_random_max)
p.alpha_add=randomval(t.alpha_add,t.alpha_add_israndom,t.alpha_add_random_min,t.alpha_add_random_max)/room_speed

// Color
if (t.color_israndom) p.color=merge_colour(t.color_random_start,t.color_random_end,random(1))
else p.color=t.color

// Color mix
if (t.color_mix_enabled) {
    if (t.color_mix_israndom) p.color_mix=merge_colour(t.color_mix_random_start,t.color_mix_random_end,random(1))
    else p.color_mix=t.color_mix
    p.color_mix_start=p.color
    p.color_mix_time=max(1,randomval(t.color_mix_time,t.color_mix_time_israndom,t.color_mix_time_random_min,t.color_mix_time_random_max)*room_speed)
} else {
    p.color_mix=-1
    p.color_mix_start=-1
    p.color_mix_time=0
}

// Rotation point
if (t.lib>-1) {
    var repx,repy,repz;
    repx=lib.repeat_x*lib.repeat_toggle+1
    repy=lib.repeat_y*lib.repeat_toggle+1
    repz=lib.repeat_z*lib.repeat_toggle+1
    switch (t.lib.type) {
        case "char": case "spblock":
            p.zoff=lib.char_model.height/2
            break
        case "scenery":
            if (lib.scenery=-1) break
            p.xoff=lib.scenery.schxsize*repx*8
            p.yoff=lib.scenery.schysize*repy*8
            p.zoff=lib.scenery.schzsize*repz*8
            break
        case "block":
            p.xoff=repx*8
            p.yoff=repy*8
            p.zoff=repz*8
            break
        case "item":
            p.xoff=8
            p.yoff=0.5
            p.zoff=8
            break
    }
}

ds_list_add(particles,p)

Drawing particles:

/// tl_particle_draw()
/// Draws the particle.

var tex,lib;
lib=type.lib

shader_alpha=alpha
shader_mulrgb=color
shader_colors=(alpha<1 || color<c_white)
shader(c_white)

if (lib=-1) {
    var dir,spr,swid,shei,fwid,fhei,framesx,tx,ty;
    spr=type.sprite_tex.particlessprite[type.sprite_tex_image]
    swid=sprite_get_width(spr)
    shei=sprite_get_height(spr)
    fwid=min(swid,type.sprite_frame_width)
    fhei=type.sprite_frame_height
    framesx=swid div fwid
    tx=((frame mod framesx)*fwid)/swid
    ty=((frame div framesx)*fhei)/shei
    
    tex=sprite_get_texture(spr,0)
    dir=90+point_direction(pos[XPOS],pos[YPOS],app.cam_xfrom,app.cam_yfrom);
    
    matrix_set(matrix_world,matrix_build(pos[XPOS],pos[YPOS],pos[ZPOS],0,0,dir,scale,scale,scale))
    d3d_primitive_begin_texture(pr_trianglelist,tex)
    d3d_vertex_normal_texture(-fwid/2,0,fhei/2,0,0,1,tx,ty)
    d3d_vertex_normal_texture(fwid/2,0,fhei/2,0,0,1,tx+fwid/swid,ty)
    d3d_vertex_normal_texture(fwid/2,0,-fhei/2,0,0,1,tx+fwid/swid,ty+fhei/shei)
    d3d_vertex_normal_texture(-fwid/2,0,fhei/2,0,0,1,tx,ty)
    d3d_vertex_normal_texture(fwid/2,0,-fhei/2,0,0,1,tx+fwid/swid,ty+fhei/shei)
    d3d_vertex_normal_texture(-fwid/2,0,-fhei/2,0,0,1,tx,ty+fhei/shei)
    d3d_primitive_end()
} else {
    var mat=matrix_multiply(matrix_build(-xoff,-yoff,-zoff,0,0,0,1,1,1),matrix_build(pos[XPOS],pos[YPOS],pos[ZPOS],rot[XPOS],rot[YPOS],rot[ZPOS],scale,scale,scale));
    matrix_set(matrix_world,mat)
    switch (lib.type) {
        case "char": case "spblock":
            var char,part;
            char=lib.char_model
            tex=sprite_get_texture(lib.char_skin.mobsprite[char.texsize],char.image)
            for (part=0 part<char.parts part++) {
                if (char.part_model[part]=-1) continue
                matrix_set(matrix_world,matrix_multiply(char.part_matrix[part],mat))
                model_draw(char.part_model[part],tex)
                if (char.part_hasbend[part]) { // Draw bend half
                    matrix_set(matrix_world,matrix_multiply(char.part_matrix_bend[part],mat))
                    model_draw(char.part_bendmodel[part],tex)
                }
            }
            break
        case "scenery":
            lib_schematic_draw(lib,lib.block_tex)
            break
        case "item":
            model_draw(lib.item_model,sprite_get_texture(lib.item_tex.itemsprite,0))
            break
        case "block":
            lib_block_draw(lib.block_model,lib.block_tex)
            break
    }
}

Updating:

/// tl_update_particles()
/// Runs once per step and updates the particle spawning and their positions, scale, alpha, color etc.

var minute,l;
minute=room_speed*60

if (lib.pc_spawn_constant) {
    var t,slot;
    // Fill spawn array
    if (spawn_queue_start=-1 || current_step=spawn_queue_start+minute) {
        for (t=0 t<minute t++) spawn_queue_amount[t]=0
        for (t=0 t<lib.pc_types t++) {
            repeat (floor(lib.pc_type[t].spawn_rate*lib.pc_spawn_amount)) {
                var i=irandom(minute-1);
                spawn_queue[i,spawn_queue_amount[i]++]=lib.pc_type[t]
            }
        }
        spawn_queue_start=current_step
    }
    
    // Check current queue slot
    slot=current_step-spawn_queue_start
    for (t=0 t<spawn_queue_amount[slot] t++) tl_particles_spawn(spawn_queue[slot,t])
}

// Update particles
for (l=0 l<ds_list_size(particles) l++) {
    var a,p,ani;
    p=ds_list_find_value(particles,l);
    
    // Delete at amount
    if (lib.pc_destroy_at_amount && ds_list_size(particles)>lib.pc_destroy_at_amount_val) {tl_particle_delete(l) continue}
    
    // Delete after an amount of time
    p.time++
    if (lib.pc_destroy_at_time && p.time>=p.timetolive) {tl_particle_delete(l) continue}
    
    // Calculate animation frame
    if (p.type.lib=-1) {
        ani=particle_get_animation_percent(p.spawntime,p.type.sprite_frame_start,p.type.sprite_frame_end,p.anispeed,p.type.sprite_animation_onend)
        if (ani=1 && lib.pc_destroy_at_animation_finish && p.type.sprite_animation_onend=0) {tl_particle_delete(l) continue} // Animation end
        p.frame=round(p.type.sprite_frame_start+(p.type.sprite_frame_end-p.type.sprite_frame_start)*ani)
    }
    
    // Speed
    for (a=XPOS a<=ZPOS a++) {
        p.pos[a]+=p.spd[a]
        p.spd[a]+=p.spd_add[a]
        p.spd[a]*=p.spd_mul[a]
        if (p.type.lib>-1) {
            p.rot[a]+=p.rot_spd[a]
            p.rot_spd[a]+=p.rot_spd_add[a]
            p.rot_spd[a]*=p.rot_spd_mul[a]
        }
        
        // Attractor
        if (par[ATTRACTOR]>0) {
            if (p.type.orbit) p.spd[a]+=clamp(par[ATTRACTOR].pos[a]-p.pos[a],-(par[FORCE]+1),(par[FORCE]+1))/room_speed
            else p.spd[a]+=clamp(par[ATTRACTOR].pos[a]-pos[a],-(par[FORCE]+1),(par[FORCE]+1))/room_speed
        }
    }
    
    
    // Scale
    p.scale+=p.scale_add
    if (p.scale<=0) {tl_particle_delete(l) continue} // Scale 0
    
    // Alpha
    p.alpha+=p.alpha_add
    if (p.alpha<=0) {tl_particle_delete(l) continue} // Alpha 0
    
    // Color mix
    if (p.color_mix>-1) p.color=merge_colour(p.color_mix_start,p.color_mix,min(1,(current_step-p.spawntime)/p.color_mix_time))
    
    // Bounding box
    if (lib.pc_bounding_box_type>0 && p.type.bounding_box) {
        if (lib.pc_bounding_box_type=1) {
            if (p.pos[ZPOS]<0 && p.type.bounce) {
                p.spd[XPOS]=p.spd[XPOS]*0.75 // Slow down XY
                p.spd[YPOS]=p.spd[YPOS]*0.75
                p.spd[ZPOS]=round(p.spd[ZPOS]*-p.type.bounce_factor) // Flip Z speed
                for (a=XPOS a<=ZPOS a++) p.rot_spd[a]*=choose(-1,1)*0.5 // Flip rotation
            }
            p.pos[ZPOS]=max(p.pos[ZPOS],0) // Keep above ground
        } else {
            for (a=XPOS a<=ZPOS a++) {
                if (p.type.bounce) {
                    if (p.pos[a]<=pos[a]*lib.pc_bounding_box_relative+lib.pc_bounding_box_custom_start[a] ||
                        p.pos[a]>=pos[a]*lib.pc_bounding_box_relative+lib.pc_bounding_box_custom_end[a]) {
                        var b;
                        p.spd[a]=round(p.spd[a]*-p.type.bounce_factor) // Invert speed
                        for (b=XPOS b<=ZPOS b++) p.rot_spd[a]*=-0.5 // Flip rotation
                    }
                }
                 // Keep within box
                p.pos[a]=clamp(p.pos[a],
                    pos[a]*lib.pc_bounding_box_relative+lib.pc_bounding_box_custom_start[a],
                    pos[a]*lib.pc_bounding_box_relative+lib.pc_bounding_box_custom_end[a])
            }
        }
    }
}

And the interface is another 400 lines. I like to make clever and compact code. Less scrolling that way.

Link to post
Share on other sites

                         I am limited with what I can do in gamemaker because I only have lite edition. And when I made this I had to try to guess how you set up all of the game vars with the other particle system and I was just trying to make a little bit of a rounder particle effect.

Also can I use the script you used for the particle system?

Edited by x33perking
Link to post
Share on other sites

I am limited with what I can do in gamemaker because I only have lite edition. And when I made this I had to try to guess how you set up all of the game vars with the other particle system and I was just trying to make a little bit of a rounder particle effect.

Also can I use the script you used for the particle system?

You can't do 3D in lite. :/ And this code is only compatible with GM:Studio and depends on hundreds of other scripts.
Link to post
Share on other sites

Yah that's what I was saying btw I use gamemaker studio also I'm just not very comfortable with the new one yet. And when I said can I use your script it was a joke like I'm going to steal your work.

P.S I was very tired when I replied. I had helped someone move all day./:

Here I'll give you a download of what I'm getting at:  http://www.mediafire.com/download/8l8bms05mmb0up3/Part_System_Creator.exe

Edited by x33perking
Link to post
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...