From e4c491768cbcdd18143a64b4e54d9765e98a0a7c Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 24 Jul 2024 16:02:54 -0700 Subject: [PATCH] re-org bike mod, optimize external deps for fast startup --- Cargo.toml | 3 + src/bike.rs | 156 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 98 insertions(+), 61 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0bcffb8..7516e03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,6 @@ bevy-inspector-egui = "0.25.1" [features] no-mesh = [] + +[profile.dev.package."*"] +opt-level = 3 diff --git a/src/bike.rs b/src/bike.rs index 645eed5..4318574 100644 --- a/src/bike.rs +++ b/src/bike.rs @@ -8,6 +8,21 @@ use crate::physics::CatControllerState; #[derive(Component)] pub struct CyberBikeBody; +#[derive(Component)] +pub struct CyberWheel; + +// marker for front suspension joint +#[derive(Component)] +pub struct Steering; +#[derive(Component)] +pub struct FrontHub; + +// marker for rear suspension joint +#[derive(Component)] +pub struct Rearing; +#[derive(Component)] +pub struct RearHub; + fn spawn_bike( mut commands: Commands, mut meshes: ResMut>, @@ -53,27 +68,53 @@ fn spawn_bike( spawn_wheels(commands, meshes, materials, xform, bike); } -#[derive(Component)] -pub struct CyberWheel; +fn spawn_wheels( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, + xform: Transform, + body: Entity, +) { + let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees + let front_pos = xform.translation + rake_vec; -// marker for front suspension joint -#[derive(Component)] -pub struct Steering; -#[derive(Component)] -pub struct FrontHub; + let (mesh, collider) = gen_tire(); -// marker for rear suspension joint -#[derive(Component)] -pub struct Rearing; -#[derive(Component)] -pub struct RearHub; + let front_hub = wheels_helper( + &mut commands, + &mut meshes, + &mut materials, + front_pos, + mesh.clone(), + collider.clone(), + ); + commands.entity(front_hub).insert(FrontHub); + commands.spawn(( + Steering, + FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec), + )); -fn helper( - commands: &mut Commands, - meshes: &mut ResMut>, - materials: &mut ResMut>, - position: Vec3, -) -> Entity { + let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize(); + let rear_hub = wheels_helper( + &mut commands, + &mut meshes, + &mut materials, + rear_pos, + mesh, + collider, + ); + commands.entity(rear_hub).insert(RearHub); + commands.spawn(( + Rearing, + FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y), + )); +} + +//-************************************************************************ +// helper fns for the wheels +//-************************************************************************ + +fn gen_tire() -> (Mesh, Collider) { let mut tire_mesh: Mesh = Torus::new(0.30, 0.40).into(); let tire_verts = tire_mesh .attribute(Mesh::ATTRIBUTE_POSITION) @@ -91,15 +132,26 @@ fn helper( .collect::>(); tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION); tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts); - let wheel_material = StandardMaterial { + + let collider = Collider::convex_decomposition_from_mesh(&tire_mesh).unwrap(); + + (tire_mesh, collider) +} + +fn wheels_helper( + commands: &mut Commands, + meshes: &mut ResMut>, + materials: &mut ResMut>, + position: Vec3, + tire_mesh: Mesh, + collider: Collider, +) -> Entity { + let wheel_material = &StandardMaterial { base_color: Color::srgb(0.01, 0.01, 0.01), alpha_mode: AlphaMode::Opaque, perceptual_roughness: 0.5, ..Default::default() }; - let collider = Collider::convex_decomposition_from_mesh(&tire_mesh).unwrap(); - let mut tire = None; - let tref = &mut tire; let xform = Transform::from_translation(position); @@ -109,58 +161,40 @@ fn helper( RigidBody::Dynamic, MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01), )) - .with_children(move |parent| { + .with_children(|parent| { let mesh: Mesh = Sphere::new(0.1).into(); parent.spawn(PbrBundle { mesh: meshes.add(mesh), material: materials.add(wheel_material.clone()), ..Default::default() }); - let tire_id = parent - .spawn(( - CyberWheel, - RigidBody::Dynamic, - collider.clone(), - ColliderDensity(0.05), - CollisionLayers::from_bits(2, 2), - ExternalTorque::ZERO.with_persistence(false), - )) - .id(); - let _ = tref.insert(tire_id); + }) + .id(); + + let tire = commands + .spawn(( + SpatialBundle::from_transform(xform), + CyberWheel, + RigidBody::Dynamic, + collider.clone(), + ColliderDensity(0.05), + CollisionLayers::from_bits(2, 2), + ExternalTorque::ZERO.with_persistence(false), + )) + .with_children(|p| { + p.spawn(PbrBundle { + mesh: meshes.add(tire_mesh), + material: materials.add(wheel_material.clone()), + ..Default::default() + }); }) .id(); // connect hubs and tires to make wheels - commands.spawn(RevoluteJoint::new(hub, tire.unwrap()).with_aligned_axis(Vec3::X)); + commands.spawn(RevoluteJoint::new(hub, tire).with_aligned_axis(Vec3::X)); hub } -fn spawn_wheels( - mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, - xform: Transform, - body: Entity, -) { - let rake_vec = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees - let front_pos = xform.translation + rake_vec; - - let front_hub = helper(&mut commands, &mut meshes, &mut materials, front_pos); - commands.entity(front_hub).insert(FrontHub); - commands.spawn(( - Steering, - FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec), - )); - - let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize(); - let rear_hub = helper(&mut commands, &mut meshes, &mut materials, rear_pos); - commands.entity(rear_hub).insert(RearHub); - commands.spawn(( - Rearing, - FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y), - )); -} - pub struct CyberBikePlugin; impl Plugin for CyberBikePlugin {