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

Unexpected crash from the function Dir3::new_unchecked when launching the App #17238

Closed
craftyneil opened this issue Jan 8, 2025 · 2 comments
Labels
A-Transform Translations, rotations and scales C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples P-Crash A sudden unexpected crash S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

Comments

@craftyneil
Copy link

Bevy version

0.15.1

[Optional] Relevant system information

cargo 1.83.0 (5ffbef321 2024-10-29)

Ubuntu 22.04.5 LTS

`AdapterInfo { name: "NVIDIA GeForce RTX 2070", vendor: 4318, device: 7938, device_type: DiscreteGpu, backend: Vulkan }`

What you did

My game hadn't crash before I add the Gun component to the player as a child component like this

pub fn spawn_gun(
    mut commands: Commands,
    player_asset: Res<PlayerAsset>,
    player: Single<(&Transform, Entity), With<Player>>,
) {
    let (player_transform, player_id) = player.into_inner();
    let gun_translation = player_transform
        .translation
        .xy()
        // extend the Vec3 coordinates with the offset
        // by adding or substracting the offset depending on the sign of the Vec3 coordinates
        //                           /*           false => -1 and true => 1            */
        .map(|el| el + (GUN_OFFSET * (el.is_sign_positive() as i32 as f32 - 0.5) * 2f32))
        .extend(0f32);
    info!("player_translation = {:#?}", player_transform.translation);
    info!("gun_translation = {:#?}", gun_translation);

    commands
        .entity(player_id)
        .with_child((
            Gun::new(player_transform.translation, gun_translation), // here
            Sprite::from_image(player_asset.gun_image.clone()),
            Transform::from_translation(gun_translation),
        ))
}

And this is the Gun Component

#[derive(Debug, Component)]
#[require(Sprite)]
pub struct Gun {
    /// Vec3 that point where the player's gun should shoot
    pub shooting_direction: Dir3,
    /// reprsent the time to wait before the gun can shoot again
    pub shooting_speed: Duration,
}

impl Gun {
    pub fn new(player_translation: Vec3, gun_translation: Vec3) -> Self {
        let shooting_direction = Dir3::new(gun_translation - player_translation);
        info!("shooting_direction = {:#?}", shooting_direction);
        Self {
            shooting_direction: shooting_direction.unwrap(),
            shooting_speed: Duration::ZERO,
        }
    }
}

What went wrong

I was expecting the Gun to spawn next to the player.

But I received this panic message

thread 'Compute Task Pool (4)' panicked at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_math-0.15.1/src/direction.rs:63:9:
Error: The vector given to `Dir3::new_unchecked` is not normalized. The length is NaN.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_render::view::visibility::update_frusta<bevy_render::camera::projection::OrthographicProjection>`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
zsh: segmentation fault (core dumped)  cargo run

We can see on this error message that there append to be a problem updating the position of a camera, and the only camera that I spawned was the player camera like that

pub fn spawn_player(mut commands: Commands, player_asset: Res<PlayerAsset>) {
    commands.spawn((
        Player,
        Sprite::from_image(player_asset.player_image.clone()),
        //                    normalise a Vec3 with z=0
        Transform::from_scale(Vec2::ONE.extend(0f32).normalize() * PLAYER_SCALE_FACTOR),
    ));
}

Since the problem was related to a Dir3, I verified the Gun::new() function, since it's the only place where I use a Dir3. But the function here

pub fn new(player_translation: Vec3, gun_translation: Vec3) -> Self {
        let shooting_direction = Dir3::new(gun_translation - player_translation);
        // ...
        Self {
            shooting_direction: shooting_direction.unwrap(), //here
            // ...
        }
    }

unwraped a Ok variant with this value

shooting_direction = Ok(
    Dir3(
        Vec3(
            0.70710677,
            0.70710677,
            0.0,
        ),
    ),
)

Then, someone suggested to me in the bevy server that a maybe messed with the camera transform, witch I don't because the program crashed because I couldn't even move the player, and so, the gun, so the problem is not with the camera's Transform modification in the other functions but when I spawn the camera. I even tried to move the place where I spawn the camera from the function spawn_player to the function spawn_gun like this

pub fn spawn_gun(
    mut commands: Commands,
    player_asset: Res<PlayerAsset>,
    player: Single<(&Transform, Entity), With<Player>>,
) {
    // ...

    commands.entity(player_id).with_child((
        Gun::new(player_transform.translation, gun_translation),
        Sprite::from_image(player_asset.gun_image.clone()),
        Transform::from_translation(gun_translation),
        Camera2D  // <---------------------------------------------------------  here 
    ));
}

but it still crashed.
The only way to make it not crash was to not spawn the Camera2D.

Additional information

the full log with RUST_BACKTRACE=1;

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.67s
     Running `target/debug/unexpected_mutation`
2025-01-06T18:03:53.209271Z  INFO bevy_diagnostic::system_information_diagnostics_plugin::internal: SystemInfo { os: "Linux 22.04 Ubuntu", kernel: "6.8.0-49-generic", cpu: "AMD Ryzen 5 3600 6-Core Processor", core_count: "6", memory: "15.5 GiB" }
2025-01-06T18:03:53.331919Z  INFO bevy_render::renderer: AdapterInfo { name: "NVIDIA GeForce GT 1030", vendor: 4318, device: 7425, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "535.183.01", backend: Vulkan }
2025-01-06T18:03:54.138732Z  INFO bevy_winit::system: Creating new window "Unexpected Mutation" (0v1#4294967296)
2025-01-06T18:03:54.138833Z  INFO winit::platform_impl::linux::x11::window: Guessed window scale factor: 1
2025-01-06T18:03:54.550251Z  INFO bevy_asset_loader::loading_state::systems: Loading state 'unexpected_mutation::AppLoadingStates::AssetLoading' is done
2025-01-06T18:03:54.616406Z  INFO unexpected_mutation::player::system: player_translation = Vec3(
    0.0,
    0.0,
    0.0,
)
2025-01-06T18:03:54.616441Z  INFO unexpected_mutation::player::system: gun_translation = Vec3(
    2.0,
    2.0,
    0.0,
)
2025-01-06T18:03:54.616457Z  INFO unexpected_mutation::player::gun::component: shooting_direction = Ok(
    Dir3(
        Vec3(
            0.70710677,
            0.70710677,
            0.0,
        ),
    ),
)
thread '<unnamed>' panicked at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_math-0.15.1/src/direction.rs:63:9:
Error: The vector given to `Dir3::new_unchecked` is not normalized. The length is NaN.
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 /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_math-0.15.1/src/direction.rs:63:9
   3: bevy_math::direction::Dir3::new_unchecked
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_math-0.15.1/src/direction.rs:402:9
   4: bevy_transform::components::global_transform::GlobalTransform::back
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_transform-0.15.1/src/components/global_transform.rs:61:13
   5: bevy_render::camera::projection::CameraProjection::compute_frustum
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_render-0.15.1/src/camera/projection.rs:92:14
   6: bevy_render::view::visibility::update_frusta
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_render-0.15.1/src/view/visibility/mod.rs:388:20
   7: core::ops::function::FnMut::call_mut
             at /home/craftyneil/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:166:5
   8: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
             at /home/craftyneil/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:294:13
   9: <Func as bevy_ecs::system::function_system::SystemParamFunction<fn(F0) .> Out>>::run::call_inner
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.1/src/system/function_system.rs:1002:21
  10: <Func as bevy_ecs::system::function_system::SystemParamFunction<fn(F0) .> Out>>::run
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.1/src/system/function_system.rs:1005:17
  11: <bevy_ecs::system::function_system::FunctionSystem<Marker,F> as bevy_ecs::system::system::System>::run_unsafe
             at /home/craftyneil/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.15.1/src/system/function_system.rs:800:19
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Encountered a panic in system `bevy_render::view::visibility::update_frusta<bevy_render::camera::projection::OrthographicProjection>`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
@craftyneil craftyneil added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jan 8, 2025
@hymm
Copy link
Contributor

hymm commented Jan 8, 2025

The z scale on the player transform being zero might be the problem. What are you trying to do there with the scale?

@BenjaminBrienen BenjaminBrienen added P-Crash A sudden unexpected crash A-Transform Translations, rotations and scales D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Triage This issue needs to be labelled labels Jan 8, 2025
@craftyneil
Copy link
Author

The scale is to help me resize the player in an easy way but I found thanks to you that the problem was that the z scale was set at 0, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Transform Translations, rotations and scales C-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples P-Crash A sudden unexpected crash S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

No branches or pull requests

3 participants