#Mesh, rigid body, and collider won't spawn for bundle nested in struct?

5 messages · Page 1 of 1 (latest)

kind drift
#

Hello, I have written the below code:

#[derive(Component)]
pub struct Robot {
    name: String,
    
    // The root component/head of the robot
    root_body: (
        PbrBundle,
        RigidBody,
        AsyncCollider,
    ),

}

pub fn spawn_robot_from_urdf(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    assset_server: Res<AssetServer>,

    model_query: Query<Entity, With<Robot>>,
) {

    let diff_bot = commands.spawn(
        (
            Robot {
                name: "diff_bot".to_owned(),
                root_body: (
                    PbrBundle {
                        mesh: meshes.add(Mesh::from(shape::Cube { size: 10.0 })),
                        material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
                        transform: Transform::from_xyz(0.0, 0.0, 0.0),
                        ..default()
                },
                    //physics properties
                    RigidBody::Dynamic, // set this object to be a dynamic rigid body
                    AsyncCollider(ComputedColliderShape::ConvexDecomposition
                        (
                            default()
                        ),
                )),

            },


        )
    ).id();

Without the struct(as below) the cube spawns though?


    let diff_bot = commands.spawn(
        (
        PbrBundle {
            mesh: meshes.add(Mesh::from(shape::Cube { size: 100.0 })),
            material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
            transform: Transform::from_xyz(0.0, 0.0, 0.0),
            ..default()
        },
        //physics properties
        RigidBody::Dynamic, // set this object to be a dynamic rigid body
        AsyncCollider(ComputedColliderShape::ConvexDecomposition
            (
                default()
            ),
    ))).id();
    

What is wrong with the first code block?

split jacinth
#

I think what you want is a Bundle.
It's a struct with each of its fields being a component (or a nested bundle).
When you spawn an entity with a bundle, all of its components are added to it.

#[derive(Bundle)]
pub struct RobotBundle {
    name: Name,
    pbr: PbrBundle,
    rigid_body: RigidBody,
    async_collider: AsyncCollider
}

pub fn spawn_robot_from_urdf(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    assset_server: Res<AssetServer>,

    model_query: Query<Entity, With<Robot>>,
) {
    let diff_bot = commands.spawn(
        RobotBundle {
            // set up things here, or implement Default on RobotBundle
        }).id();
}
#

What you were trying to do instead is adding a Robot component (not bundle) with stuff inside, but the stuff inside wasn't actually directly added to the entity it was only inside the Robot component, and only the Robot component was added to the entity

kind drift
split jacinth
#

"Bevy doesn't recursively add bundles inside components to the entity?"
No, bevy recursively adds bundles/components inside bundles, not components