Using particle engine in c++

Hi,

I tried to use particle system in c++. I have read example python programs, but it seems that there is a lot of python dependences in it.

I have this, edited output of particle panel:

ParticleSystem *s;
ParticleSystemManager m;

....

   PointParticleFactory *f = new PointParticleFactory();
    f->set_lifespan_base(1.0000);
    f->set_lifespan_spread(0.2500);
    f->set_mass_base(1.0000);
    f->set_mass_spread(0.0000);
    f->set_terminal_velocity_base(400.0000);
    f->set_terminal_velocity_spread(0.0000);

    SparkleParticleRenderer *r = new SparkleParticleRenderer();
    r->set_alpha_mode(BaseParticleRenderer::PR_ALPHA_NONE);
 //   r->set_user_alpha(1.00);
    r->set_center_color(Colorf(1.00, 1.00, 1.00, 1.00));
    r->set_edge_color(Colorf(1.00, 1.00, 0.00, 1.00));
    r->set_birth_radius(0.0500);
    r->set_death_radius(15.0000);
    r->set_life_scale(SparkleParticleRenderer::SP_NO_SCALE);

    SphereVolumeEmitter *em = new SphereVolumeEmitter();
    em->set_emission_type(BaseParticleEmitter::ET_RADIATE);
    em->set_amplitude(1.0000);
    em->set_amplitude_spread(0.0000);
    em->set_offset_force(LVector3f(0.0000, 0.0000, 0.0000));
    em->set_explicit_launch_vector(LVector3f(1.0000, 0.0000, 0.0000));
    em->set_radiate_origin(LVector3f(0.0000, 0.0000, 0.0000));
    em->set_radius(1.0000);

    s = new ParticleSystem();
    s->set_factory(f);
    s->set_emitter(em);
    s->set_renderer(r);
    s->set_pool_size(128);
    s->set_birth_rate(0.0200);
    s->set_litter_size(10);
    s->set_litter_spread(0);
    s->set_system_lifespan(0.0000);
    s->set_local_velocity_flag(1);
    s->set_system_grows_older_flag(0);

And my attempts to see particles:

    PhysicalNode *phn = new PhysicalNode("starts");
    phn->add_physical(s);

    NodePath phnp = window->
     get_render().attach_new_node(s->
     get_physical_node()
    );

    phnp.set_pos(0, 0, 0);
    phnp.set_hpr(0.000, 0.000, 0.000);
    phnp.set_scale(0.25, 0.25, 0.25);

    m.attach_particlesystem(s);

and inside of the AsyncTask:

     m.do_particles(task->get_dt(),s,true);

But there are no particles on the screen. (it is test program, so particles are only objects in scene).

Do you know how it should be done in c++?

Btw. what about “C++ Snippets” thread?

I found three small mistakes:
(1) You need to attach the “render node” into the scene graph somewhere. You’re attaching the “physical node”, which defines the frame of reference (and the transform) on the particle system itself, but the “render node” is where the particles actually get drawn. Add something like this:

r->get_render_node_path().reparent_to(window->get_render());

(2) You’re passing the wrong dt value into m.do_particles(). The task dt value is only the amount of time spent within the task itself, but the particle system wants to know the amount of time elapsed globally since the previous frame. For this, use the global ClockObject. Also, you should use the one-parameter version of do_particles(), so it should look like:

m.do_particles(ClockObject::get_global_clock()->get_dt());

(3) Many of these objects inherit from ReferenceCount, and thus you should store them in a PT() pointer instead of a plain C-style pointer, or there is a danger of them being deleted out from under you. Use PT(PointParticleFactory) f instead of PointParticleFactory *f, and similar for SparkleParticleRenderer, SphereVolumeEmitter, ParticleSystem, and PhysicalNode.

David