Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "drag" field to particle systems ? #42

Closed
Rayoule opened this issue Mar 8, 2023 · 12 comments
Closed

Add "drag" field to particle systems ? #42

Rayoule opened this issue Mar 8, 2023 · 12 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Rayoule
Copy link
Contributor

Rayoule commented Mar 8, 2023

I am enjoying this crate very much, and I think adding a drag field would be awesome to have some smokey/lightweight effect.
It would work as a constant (or interpolated over time?) velocity scaling.
I used a lot of particle systems, and the drag is almost always on, even with little impact, so it gives the feeling that the particles are moving in the air and not in empty space.

For now, I have to use a negative acceleration to achieve this. The problem is that if I modify my initial speed, acceleration will also need a tweak so it matches the speed, and don't make the particles go too quick, or go in the opposite direction.

Also, the acceleration is a value and not a vector, so we can't deviate particles from their original direction. This makes impossible to simulate gravity or other custom forces we would like to apply (vector fields gives awesome results with particles)

Well I hope this will help :)

@abnormalbrain abnormalbrain added enhancement New feature or request help wanted Extra attention is needed labels Mar 11, 2023
@abnormalbrain
Copy link
Owner

I have previously considered moving toward a more correct velocity usage. Adding drag would be a nice touch as well.

I'm not super familiar with the actual physics involved, so I think it'll take me some time to digest some of the info. I'm very open to pointers in how to correctly (or at least approximately) calculate this if you have any, assuming we already have a current velocity and acceleration.

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 13, 2023

Maybe put a simplified equation from the wiki you mentionned.

It says that: F = 1/2 * p * v^2 * C * A

F being the resulting drag force
p being the density of the fluid, so its a constant in our case
v is the speed of the particle
A is the cross section of the area, so a constant in our case
C the drag coefficient. This will be the given "drag" field of the particle system. 0 by default, or if drag Option is None

it gives: F = 1/2 * v^2 * C

I think this would make it work, with a little tweak of "p" to make it nice out of the box

What do you think ?

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 13, 2023

I could try to implement that, but I never made a pull/merge requests out of the studio I worked before :)

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 13, 2023

I made a branch with modifications that changes Direction/Speed to Velocity, but I have no idea how to submit it to this repo.
works fine, but breaks the basic.rs example due to the nature of the current acceleration use

@abnormalbrain
Copy link
Owner

@Rayoule nice!

To open the change here, you'll want to fork this repo by hitting the Fork button in the top right. That'll create a copy of the repo in your account. Push your branch to that fork. And then you can open a PR from that branch on your forked copy to this repo.

We'll want to get the examples and docs updated so they work before we can merge though.

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 14, 2023

Thanks !

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 14, 2023

It works without any modifications of the examples !
https://github.com/Rayoule/bevy_particle_systems

Another thing is that the acceleration is still a f32. It should be a Vec3 since an acceleration is an external force that can have any direction. Do you agree ?
But the acceleration is a ValueOverTime. Is making a VectorOverTime seems like a good idea to you ?

In the meantime I will add the drag and make an example about it.

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 14, 2023

Drag has been added and works fine.
It can't be an Option<ValueOverTime> since that type doesn't implement Reflect (and I have no idea what Reflect does)
A new example/drag.rs is also there to show how it works.

Now that its done, I would like to turn the acceleration into a vector so it can be more flexible, and not direction-based (for example, to simulate gravity).
I am tempted to use ColorOverTime since it allows us to modify a vector over time, but its clearly not clean...
Maybe create a new VectorOverTime struct ?

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 15, 2023

Hello again !

I've just implemented a new way to handle acceleration/velocity/drag that allows more flexibility and unlocks new possibilities !
I made a branch to my fork that shows how it is implemented (with an example velocity_modifiers.rs that replaces drag.rs): https://github.com/Rayoule/bevy_particle_systems/tree/velocity_modifiers

How it works:
For Particle and ParticleSystem, a new field velocity_modifiers replaces acceleration and drag.
This field is a Vec of VelocityModifier, an enum that can be:

  1. a Value(ValueOverTime) (which works the same way as the acceleration in the current version of bevy_particle_systems).
  2. a ConstantVector(Vec3) that contains a constant acceleration (ex: Vec3::new(0.0, -500, 0.0) to simulate gravity).
  3. a Drag(f32) that add the drag effects.

This way, we have only one field in the Particle / ParticleSystem structs, but it still possible to add other effects to velocity (like vector fields, attraction fields, wind, gameplay driven effects, etc...) if needed later, just by adding new variants to VelocityModifier.

And now, we can stack these modifiers so its possible to have a drag AND a constant acceleration (see the example velocity_modifiers.rs) !

I fixed the example basic.rs (the only one that was using acceleration). Its a little more verbose but its worth it in my opinion.

@abnormalbrain
I hope this modification fits to your vision of the project !

@abnormalbrain
Copy link
Owner

@Rayoule Interesting!

Sorry for the lack of reply here, got busy with my day job but I really like this approach. It feels clean and extensible.

@Rayoule
Copy link
Contributor Author

Rayoule commented Mar 25, 2023

No problem !
I will submit the PR once the drag is merged.

@abnormalbrain
Copy link
Owner

Closed by #44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants