Skip to content
New issue

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

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

Already on GitHub? # to your account

Error: The vector given to Dir3::new_unchecked is not normalized #573

Closed
coreh opened this issue Dec 3, 2024 · 4 comments · Fixed by #620
Closed

Error: The vector given to Dir3::new_unchecked is not normalized #573

coreh opened this issue Dec 3, 2024 · 4 comments · Fixed by #620
Labels
A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests A-Transform Relates to transforms or physics positions P-Crash A sudden unexpected crash

Comments

@coreh
Copy link

coreh commented Dec 3, 2024

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:

avian/src/position.rs

Lines 727 to 729 in 56d7c59

fn mul(self, direction: Dir) -> Self::Output {
Dir::new_unchecked((self * direction.adjust_precision()).f32())
}

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]

rotation.issue.720p.mov
@Jondolf Jondolf added P-Crash A sudden unexpected crash A-Transform Relates to transforms or physics positions A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests labels Dec 5, 2024
@matjlars
Copy link

I'm getting this same panic occasionally when I call transform.forward() on a kinematic body. I can get it to not panic by normalizing the rotation first, but then the rotation itself is off a little bit. And about 5% of the time, it's off by about 90-180-ish degrees.

I had this error with the older version (bevy 0.14) and solved it by getting rid of all usage of the LinearVelocity component. But now with the upgrade to bevy 0.15 and avian 0.2 the bug is back. My hypothesis is somewhere, something is setting the transform.rotation to a non-normalized quaternion.

@Femto-godhand
Copy link

Femto-godhand commented Dec 29, 2024

I got a similar error message by using the "look_at()" and "up()" function in my code. I replaced the usage of the up() with "Vec3::Y" like this:

fn rotate_and_translate_player_mesh(
    mut player_mesh_transform: Single<&mut Transform, With<PelvisBone>>,
    player_transform: Single<&GlobalTransform, With<PlayerBody>>,
    cursor_target_coordinate: Res<CursorTargetCoordinates>
) {
    player_mesh_transform.translation = player_transform.translation();
    player_mesh_transform.translation.y = player_transform.translation().y + 0.94;

    let target = Vec3::new(
        cursor_target_coordinate.global.x,
        player_mesh_transform.translation.y,
        cursor_target_coordinate.global.z,
    );
    let up = Vec3::Y;   //player_mesh_transform.up();    **<- CORRECTION HERE!**
    player_mesh_transform.look_at(target, up);
}

And I never got the error message again. Maybe it can help.

@arseeeeN
Copy link

arseeeeN commented Jan 4, 2025

I got the same issue, makes it currently impossible to work on one of my projects.
I think the desync in normalization in my case could be caused by lightyear, since my code doesn't change any of the rotations in any meaningful way. I hope this gets fixed soon.

@Jondolf
Copy link
Owner

Jondolf commented Jan 10, 2025

#620 appears to fix #618, which is a very similar issue to this one. I'd appreciate if someone who is getting this panic could test that branch to see if it fixes it :) (or if it causes any other issues)

Also FYI, this is a debug_assert, so it should only cause a panic with debug_assertions enabled. When building in release mode, there should be no panic.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests A-Transform Relates to transforms or physics positions P-Crash A sudden unexpected crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants