Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Thor] Setting up a dynamically changing Emitter in the game loop  (Read 2171 times)

0 Members and 1 Guest are viewing this topic.

Yee7i

  • Newbie
  • *
  • Posts: 13
    • View Profile
I want to enable particles on a fireball, but the fireball's position is dynamically changing via a Vector2f position, to which the Sprite position is adjusted. Basically I've got a Vector2f fireball.position and then I'm using sprFireball.setPosition(fireball.position). It's for convenience only.

But to the point. I've got a ParticleSystem and most basic Emitter properties declared outside the game loop, but as the fireball's position is changing in the game loop, I need to somehow move the Emitter's particle position to that of the fireball. I've looked up Bromeon's tutorial on Particles and Connections, but that didn't help in my case. I was toying around with it for the past 2 hours or so and can't find a solution.

I'll try to minimize the code and put it below

        thor::ParticleSystem fireballParticleSystem;
        //adding textures

        thor::UniversalEmitter fireballEmitter;
        //setting properties

        fireballParticleSystem.addEmitter(fireballEmitter);
        // adding the emitter for the first time

        sf::Clock fireballFrameTime;
        // for emitter updates      

        // window and isOpen,

        { // game loop

       

        // setting the position of the fireball, its rotation, moving it and using the fireball.position to store the
           position
       
           

        }

     
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Thor] Setting up a dynamically changing Emitter in the game loop
« Reply #1 on: October 02, 2019, 10:31:28 pm »
thor::UniversalEmitter has several attributes, each of which can be set to a Distribution. You can read the docs to understand exactly what this is in Thor's context.

 In your case, thor::UniversalEmitter::setParticlePosition() should do the job. It can be assigned either a constant, or a function that dynamically fetches the position.

thor::Distribution<sf::Vector2f> positionQuery = [...] () -> sf::Vector2f
{
   // compute position of your fireball here
   return fireball.GetPosition();
};

thor::UniversalEmitter emitter;
emitter.setParticlePosition(positionQuery);

It may be necessary to construct an intermediate std::function<sf::Vector2f()>, I'm not 100% sure the type inference of the thor::Distribution constructor catches this. As far as I remember, the SFINAE black magic is quite advanced there ;)
« Last Edit: October 02, 2019, 10:33:17 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Yee7i

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [Thor] Setting up a dynamically changing Emitter in the game loop
« Reply #2 on: October 03, 2019, 05:08:53 pm »
I don't know if you saw, but I've said that I used setParticlePosition, but in order for that to work, I'd need to add an Emitter again, because, for some reason, it wouldn't update it when I run the function on its own.

I worked it around by adding an emitter every frame for 0.006 seconds with emission rate 100, but it's not a good solution and I know it.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Thor] Setting up a dynamically changing Emitter in the game loop
« Reply #3 on: October 05, 2019, 12:21:31 pm »
You don't mention setParticlePosition() in your post, but probably you implied it ;)

Quote
but in order for that to work, I'd need to add an Emitter again, because, for some reason, it wouldn't update it when I run the function on its own.
Can you explain that more clearly? What wouldn't update what, and which function?

Generally, if you have multiple fireballs in your system, adding one emitter per fireball is perfectly fine. After all, fireballs are independent entities with different positions, velocities, rotations etc. You can either add an emitter for a given time, or explicitly use the thor::Connection to remove it when the fireball dies.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Yee7i

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: [Thor] Setting up a dynamically changing Emitter in the game loop
« Reply #4 on: October 07, 2019, 05:40:04 pm »
Oh, I've just noticed that half the code didn't get through properly. I said later in the part that didn't go through that neither the updating particle system nor setting the particle position works, unless I re-add the emitter.
When I run the .setParticlePosition or .updateParticleSystem I'd need to add the emitter again in order for it to actually update.

As I said, the work-around is to add the emitter again (in my case for 0.006 seconds) and add the emitter each frame, clearing emitters before and then update.

Right now I'm working on rebuilding the whole code, so I'll keep the thread updated if I find a solution, but the workaround is really imperfect.

 

anything