Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.51k stars 119 forks source link

Panic with headless build #505

Open glklimmer opened 2 months ago

glklimmer commented 2 months ago

Description:

Bevy provides a MinimalPlugins plugin bundle for headless bevy applications. Headless applications can be used to implement a game server for example. When trying to add the default Avian PhysicsPlugins Plugin Bundle Bevy a panics.

Minimal Example:

Here is a minimal example for a simple headless bevy application.

use avian3d::prelude::*;
use bevy::{app::ScheduleRunnerPlugin, prelude::*};
fn main() {
    App::new()
        .add_plugins(MinimalPlugins.set(ScheduleRunnerPlugin::run_once()))
        .add_systems(Update, hello_world_system)
        .add_plugins(PhysicsPlugins::default())
        .run();
}

fn hello_world_system() {
    println!("hello world");
}

Which results in the following error.

Resource requested by avian3d::collision::collider::backend::init_collider_constructor_hie
rarchies does not exist: bevy_asset::assets::Assets<bevy_render::mesh::mesh::Mesh>
Encountered a panic in system `hello world
avian3d::collision::collider::backend::init_collider_constructors`!
Encountered a panic in system `avian3d::collision::collider::backend::init_collider_constr
uctor_hierarchies`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

Is this a bug?

Does PhysicsPlugins::default() include plugins that can not be used without bevy_window::WindowPlugin maybe? If yes, would it make sense to also provide a MinimalPlugins bundle for avian physics? What plugins would I need to exclude?

Thanks!

datael commented 2 months ago

The panics do not seem to be related to WindowPlugin.

Info for you and anyone else who lands here trying to do the same in the future:

There's a few things missing when launching with MinimalPlugins that cause a panic:

Also missing but doesn't cause a panic:

These requirements are implicitly codified in tests.rs::create_app here, but this information isn't easy to find.

With the following changes I was able to get your code above running:

  fn main() {
      App::new()
          .add_plugins(MinimalPlugins.set(ScheduleRunnerPlugin::run_once()))
+         .add_plugins((
+             TransformPlugin,
+             bevy::asset::AssetPlugin::default(),
+             bevy::scene::ScenePlugin,
+         ))
+         .init_resource::<Assets<Mesh>>()
          .add_systems(Update, hello_world_system)
          .add_plugins(PhysicsPlugins::default())
          .run();
  }

If you don't need mesh colliders, you can avoid the need for Assets<Mesh> resource and ScenePlugin if you disable the bevy_scene and collider-from-mesh features.

I don't see a way that it could be possible to remove the reliance on TransformPlugin since so much of Avian works with GlobalTransform.