bitshifter / glam-rs

A simple and fast linear algebra library for games and graphics
Apache License 2.0
1.46k stars 145 forks source link

Make random quaternions uniformly random (via `Standard`) #534

Closed mweatherley closed 2 weeks ago

mweatherley commented 3 weeks ago

Previously, randomly sampled Quats had bias because they were constructed using Euler rotation sequences. This PR makes them follow a uniform distribution. The algorithm used is the one described here.

This is essentially a follow-up from this bevy_math PR, which affords users the ability to ergonomically obtain random directions and quaternions.

I believe that this implementation also conforms more closely with the philosophy of Standard: since the space of quaternions is compact, it is quite natural for the "default" distribution over them to be uniform.

Furthermore, if a user wants the old distribution, it is easily implemented using just Rng::gen_range and Quat's constructors; the present implementation actually bundles relatively specialized knowledge into an interface, which abstracts difficulties away from users.

NthTensor commented 3 weeks ago

Over at bevy we've had several users be confused when they encounter the current nonuniform version. We can't really fix this in our side, and to me this seems like a better default.

bitshifter commented 3 weeks ago

You need to use math::sin_cos which wraps libm and std implementations.

bitshifter commented 3 weeks ago

I don't have a strong feeling about performance characteristics of this. It's something I added at the very beginning of glam to generate some data to run benchmarks on, it's not something I put any thought into at the time. It seems like there's still a bit of discussion happening on bevy discord, let me know what you settle on.

mweatherley commented 2 weeks ago

Cool. I ended up switching to axis-angle randomization since I think it's simple to grok and its performance seems pretty good.

bitshifter commented 2 weeks ago

Thanks for the PR!