-
-
Notifications
You must be signed in to change notification settings - Fork 208
Description
Hey there 👋 Encountered this after updating my version of avian to the latest main (For Bevy 0.15 compatibility):
thread 'main' panicked at contrib/bevy/crates/bevy_math/src/direction.rs:63:9:
Error: The vector given to `Dir3::new_unchecked` is not normalized. The length is 1.0144635.
stack backtrace:
0: rust_begin_unwind
at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
1: core::panicking::panic_fmt
at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:74:14
2: bevy_math::direction::assert_is_normalized
at ./contrib/bevy/crates/bevy_math/src/direction.rs:63:9
3: bevy_math::direction::Dir3::new_unchecked
at ./contrib/bevy/crates/bevy_math/src/direction.rs:402:9
4: <avian3d::position::Rotation as core::ops::arith::Mul<bevy_math::direction::Dir3>>::mul
at ./contrib/avian/crates/avian3d/../../src/position.rs:742:9
5: avian3d::spatial_query::update_shape_caster_positions
at ./contrib/avian/crates/avian3d/../../src/spatial_query/mod.rs:319:36
6: core::ops::function::FnMut::call_mut
at /Users/coreh/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:166:5
7: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
at /Users/coreh/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:294:13
8: <Func as bevy_ecs::system::function_system::SystemParamFunction<fn(F0,F1) .> Out>>::run::call_inner
at ./contrib/bevy/crates/bevy_ecs/src/system/function_system.rs:939:21
9: <Func as bevy_ecs::system::function_system::SystemParamFunction<fn(F0,F1) .> Out>>::run
at ./contrib/bevy/crates/bevy_ecs/src/system/function_system.rs:942:17
10: <bevy_ecs::system::function_system::FunctionSystem<Marker,F> as bevy_ecs::system::system::System>::run_unsafe
at ./contrib/bevy/crates/bevy_ecs/src/system/function_system.rs:737:19
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Encountered a panic in system `avian3d::spatial_query::update_shape_caster_positions`!
Encountered a panic in system `avian3d::schedule::run_physics_schedule`!
Encountered a panic in system `bevy_app::main_schedule::FixedMain::run_fixed_main`!
Encountered a panic in system `bevy_time::fixed::run_fixed_main_schedule`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
This happens very spuriously/inconsistently, but generally when shooting enemies in my game.
The issue seems to arise from a Quat * Dir3 multiplication in this function: https://github.com/Jondolf/avian/blob/56d7c59d4f63945e27db7e6d024ad1d73c371b29/src/position.rs#L727-L729
I modified it to check for normalization of the result and print the values of both operands and result:
fn mul(self, direction: Dir) -> Self::Output {
let result = (self * direction.adjust_precision()).f32();
if !result.is_normalized() {
println!("Quat: {:?}, (length = {})", self.0, self.0.length());
println!("Dir3: {:?} (length = {})", direction, direction.length());
println!("Result: {:?} (length = {})", result, result.length());
}
Dir::new_unchecked(result)
}Then, right before the assertion failure, I get this output:
Quat: Quat(-0.06689565, -0.38996884, 0.5608767, 0.7361675) (length = 1.0065168)
Dir3: Dir3(Vec3(0.04367252, -0.9936813, -0.10339359)) (length = 1)
Result: Vec3(0.8393426, -0.29920217, 0.48198038) (length = 1.013076)
It looks like for whatever reason, the quaternion being used for global_rotation here is non-normalized.
This might be caused by an internal change in glam, since bevy did update from 0.27.x to 0.29.x, and especially in 0.29.0 there seems to be a few changes related to quaternions that could cause this?
Edit: Looking around at recent PRs, I suspect the most likely culprit is this: #520
Edit 2: Just added the missing .normalize() call to transform_to_position that was removed in #520, and the crash is gone.
Edit 3: I think I might have hit #522 as well, check the footage below, this weird rotation glitch does not happen when reverting to the old addition-based math, from before #520.
[CW: Videogame Violence]