particles.c
This include file contains often used, small particle effect related functions, such as emulating a particle effect with sprites. The functions are simple enough to understand them at a glance. If you want to modify them, please edit a particles.c copy in your work folder, and not the original file in the include folder.
effect_sprite(STRING* image, EVENT func, var number, VECTOR* pos, VECTOR* vel)
Emulates a particle effect similar to effect_local, but with sprites (or models) instead of particles.
Parameters:
image |
STRING* or char*; sprite or model filename to be used as particle image, or NULL for a default particle. |
func |
Particle function; runs every frame. |
number |
Number of particles to create. |
pos |
Position of the particle emitter. |
vel |
Initial speed vector. |
Remarks:
- Advantage of particle emulation: sprites or models can have any size and rotation, are distance sorted when transparent, can perform collision detection when desired,
and can use shaders
. Disadvantage: rendering and creating sprites or models is much slower than rendering and creating particles.
- The emulated particle function (func) looks almost like a normal particle function, but some particle-specific parameters and flags are replaced by entity skills
skill60..69 and FLAG7
: vel_x/y/z => _VEL_X/Y/Z; gravity => _GRAVITY; size => _SIZE; lifespan => _LIFESPAN; you => _CREATOR;
MOVE => _MOVE
. BEAM and STREAK are not supported.
- Every emulated particle adds a function to the scheduler that runs during its lifespan. Adjust max_entities and nexus if necessary.
Example:
#include <acknex.h>
#include <default.c>
#include <particles.c>
// original particle function
function p_fountain(PARTICLE* p)
{
VECTOR vTemp;
vec_randomize(vTemp,2);
vec_add(p.vel_x,vTemp);
vec_set(p.blue,vector(random(255),random(255),255));
set(p, MOVE | BRIGHT | TRANSLUCENT);
p.alpha = 100;
p.size = 2;
p.gravity = 0.2;
p.skill_a = 3; // fade factor
p.event = p_fade;
}
// sprite-emulated particle function
function p_fountain_sprite(ENTITY* p)
{
VECTOR vTemp;
vec_randomize(vTemp,2);
vec_add(p._VEL_X,vTemp);
vec_set(p.blue,vector(random(255),random(255),255));
set(p, _MOVE | BRIGHT | TRANSLUCENT);
p.alpha = 100;
p._SIZE = 2;
p._GRAVITY = 0.2;
p._FADE = 3; // fade factor
p.event = p_fade_sprite;
}
function main()
{
max_entities = max_particles;
level_load(NULL);
vec_set(camera.x,vector(-150,0,50));
while(1)
{
if (key_s) // emulated particles
effect_sprite(NULL,p_fountain_sprite,maxv(1,20*time_step),vector(0,0,0),vector(0,0,5));
else // original particles
effect(p_fountain,maxv(1,20*time_step),vector(0,0,0),vector(0,0,5));
wait(1);
}
}
vec_randomize (VECTOR* vec, var range)
Set a vector to a random length and orientation; useful for particle explosions (see example above).
Parameters:
vec - vector to be set to random values.
range - maximum vector length.
Modifies:
vec
p_fade (PARTICLE* p)
p_fade_sprite (ENTITY* p)
Fade a translucent particle resp. emulated particle out and removes it when it becomes fully transparent. The fade speed can be given by the skill_a resp. _FADE parameter of the particle (see example above).
p_follow (PARTICLE* p)
p_follow_sprite (ENTITY* p)
Let a particle follow the creator entity. Uses particle skill_x/_y/_z resp. entity skill70..72.
p_snow (PARTICLE* p)
Let a particle fall slowly to the ground like a snowflake. The wind speed vector is given by vel_x/_y/_z. The particle is placed at a random position at the upper boundary of the camera frustum, and removed when it reaches the lower boundary of the frustum. Uses skill_z.
Example: See samples\weather.c
See also:
effect
► latest
version online