djeedai / bevy_hanabi

🎆 Hanabi — a GPU particle system plugin for the Bevy game engine.
Apache License 2.0
907 stars 72 forks source link

Inherit initial velocity from moving emitters #64

Open malleusinferni opened 1 year ago

malleusinferni commented 1 year ago

Particles currently have an initial velocity determined by the InitModifiers on an EffectAsset and the GlobalTransform of the emitter entity. Certain classes of visual effects also need to account for the velocity of a moving emitter, and add this value to the initial velocity of all its spawned particles.

To maintain open-ended interoperability with physics engines, the API for this feature could expose an initial velocity field on the ParticleEffect component, and make the API consumer responsible for copying the appropriate value from any physics components.

djeedai commented 1 year ago

I think the way I'd like to handle that is that we calculate the emitter's velocity, defined as the delta position between 2 frames of the GlobalTransform::position of the emitter divided by the frame delta time. And then we pass that emitter velocity to the GPU alongside the spawn count for each frame, and the compute shader adds it to the particle's own velocity. That's reasonably straightforward.

Note that we're assuming here a world-space simulation. But that's the only mode available today so that's fine.

malleusinferni commented 1 year ago

The previous and current GlobalTransform may need to be compared regardless for the purposes of #65, so deducing velocity that way makes plenty of sense, as does interpolating it between frames. In that case, the user-facing API for this feature should expose other ways to manipulate the emitter's calculated velocity before adding it to the velocity of each particle — for example, a scaling factor.