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

Cone distribution not uniform in all cases #95

Open
NiseVoid opened this issue Nov 30, 2022 · 7 comments
Open

Cone distribution not uniform in all cases #95

NiseVoid opened this issue Nov 30, 2022 · 7 comments
Labels
A - modifiers Change related to modifiers C - bug Something isn't working D - complex Complex change

Comments

@NiseVoid
Copy link
Contributor

When creating a cone, particle distribution is not uniform. I think the problem comes from alpha_h being set to pow(rand(), 1.0/3.0). This is not correct and performs even worse than setting it directly to rand(), which would at least behave correctly on a cylinder (top_radius == bottom_radius) and looks somewhat uniform on any cone without a sharp point.

@djeedai
Copy link
Owner

djeedai commented Nov 30, 2022

Could you back those claims with resources or mathematical proofs? The 1/3 factor is derived mathematically from a half-cone. Maybe it doesn't extend to a truncated cone, I can't remember the details to be honest, but I find it hard to believe a simple rand() would perform any better in the general case.

@NiseVoid
Copy link
Contributor Author

After I changed some code so top_radius is the side facing Y+ it works fine for perfect cones. However, any cone without top_radius: 0 will not have uniform distribution, be it reversed (top_radius > base_radius), non-perfect (top_radius != 0), or a cylinder (top_radius == base_radius). alpha_h would need to be calculated using these radii, to handle all these cases correctly.

@NiseVoid NiseVoid changed the title Cone distribution not random Cone distribution not uniform in all cases Nov 30, 2022
@djeedai
Copy link
Owner

djeedai commented Nov 30, 2022

This is the distribution for a cone of height 10. with a top of 0. and base of 10., so a perfect (half) cone (truncated at the base). The white cube indicates the origin of the effect, at the top/apex of the cone.

image

Distribution looks fairly uniform.

Now with top radius 5.:

image

We can start to guess some issue.

With top radius 10., forming a cylinder, we finally see the issue clearly:

image

Note that there's no "change the facing" involved here. The origin is at the top by design, which is the apex when r = 0, and the base is always the larger side. This goes hand in hand with the face particles are emitted at the base toward +Y, which we can infer from the colors when setting a non-zero speed:

image

So yes there's a bug in the distribution in case the cone is truncated at the top. But everything else is by design. In particular, there's no inversion of the top/base; the base is where particle are emitted, and is generally larger than the top as you most often want some kind of radial spread of particles rather than the particles converging toward the cone axis. And the "base" semantic comes from the mathematical definition of a cone, while the "top" one is just copied from Unity, and is arguable but is better than nothing. I'd maybe rename to "origin" if that makes it clearer. I do appreciate that Unity uses the opposite convention, but I don't really like it because what they end up calling the "top" is the mathematical base of the cone, while their "base" is the apex when r = 0 (cone not truncated at origin), which is fairly confusing. We can discuss argument against the current convention, and changing it, but given it's already the existing one and people are using it as is, and I see no argument in #96 against it or for a change, I don't see a compelling argument to merge #96 (which would be a breaking change).

@djeedai djeedai added the C - bug Something isn't working label Nov 30, 2022
@djeedai
Copy link
Owner

djeedai commented Nov 30, 2022

By the way, the cone uniform distribution formula is from https://stackoverflow.com/a/41752481

@NiseVoid
Copy link
Contributor Author

Ah, if the design is supposed to have base always be bigger than top that eliminates the most obvious problem. Perhaps the library should somehow disallow setting it like that.

I agree that flipping it would be a breaking change, and one that might be undesirable for particles as you'd want to emit them away from your origin and spread out. I also think hanabi doesn't currently rotate the effects, so users wouldn't be able to just flip it back either.

As for the names, top_radius is as mentioned very confusing since it implies directionality. origin_radius wouldn't be great either since base and origin can be interpreted to have basically the same meaning. The best name I could think of would be cap_radius, since partial cones are sometimes referred to as capped cones but truncated cone seems to be far more common (truncated_radius sounds a bit weird). Another option is just to bypass the fieldnames entirely and just let the user specify 2 radii at once and sort them the right way.

@djeedai
Copy link
Owner

djeedai commented Mar 13, 2023

Note that in addition of the formula getting wronger and wronger as the base and top radii get closer to each other (which isn't fixed yet; I didn't find the proper formula so far), there was also another simpler bug where base and top were inverted in the distribution. The new init.rs shows an example of distribution of particles at spawn, and looks a lot more uniform than it used to be before v0.6.1 and in the screenshots above. There's still a discrepancy though when converging toward a cylinder.

@djeedai
Copy link
Owner

djeedai commented Oct 7, 2023

To be clear on why this is not fixed yet: I have been unable so far too find a reliable formula to calculate the distribution over the surface or volume of a truncated cone. This is slightly tricky to derive mathematically.

@djeedai djeedai added the D - complex Complex change label May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A - modifiers Change related to modifiers C - bug Something isn't working D - complex Complex change
Projects
None yet
Development

No branches or pull requests

2 participants