re-org bike mod, optimize external deps for fast startup
This commit is contained in:
parent
6031eeab5c
commit
e4c491768c
2 changed files with 98 additions and 61 deletions
|
@ -10,3 +10,6 @@ bevy-inspector-egui = "0.25.1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
no-mesh = []
|
no-mesh = []
|
||||||
|
|
||||||
|
[profile.dev.package."*"]
|
||||||
|
opt-level = 3
|
||||||
|
|
140
src/bike.rs
140
src/bike.rs
|
@ -8,6 +8,21 @@ use crate::physics::CatControllerState;
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CyberBikeBody;
|
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(
|
fn spawn_bike(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
@ -53,27 +68,53 @@ fn spawn_bike(
|
||||||
spawn_wheels(commands, meshes, materials, xform, bike);
|
spawn_wheels(commands, meshes, materials, xform, bike);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
fn spawn_wheels(
|
||||||
pub struct CyberWheel;
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
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
|
let (mesh, collider) = gen_tire();
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Steering;
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct FrontHub;
|
|
||||||
|
|
||||||
// marker for rear suspension joint
|
let front_hub = wheels_helper(
|
||||||
#[derive(Component)]
|
&mut commands,
|
||||||
pub struct Rearing;
|
&mut meshes,
|
||||||
#[derive(Component)]
|
&mut materials,
|
||||||
pub struct RearHub;
|
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(
|
let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize();
|
||||||
commands: &mut Commands,
|
let rear_hub = wheels_helper(
|
||||||
meshes: &mut ResMut<Assets<Mesh>>,
|
&mut commands,
|
||||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
&mut meshes,
|
||||||
position: Vec3,
|
&mut materials,
|
||||||
) -> Entity {
|
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 mut tire_mesh: Mesh = Torus::new(0.30, 0.40).into();
|
||||||
let tire_verts = tire_mesh
|
let tire_verts = tire_mesh
|
||||||
.attribute(Mesh::ATTRIBUTE_POSITION)
|
.attribute(Mesh::ATTRIBUTE_POSITION)
|
||||||
|
@ -91,15 +132,26 @@ fn helper(
|
||||||
.collect::<Vec<[f32; 3]>>();
|
.collect::<Vec<[f32; 3]>>();
|
||||||
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
|
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
|
||||||
tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts);
|
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<Assets<Mesh>>,
|
||||||
|
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||||
|
position: Vec3,
|
||||||
|
tire_mesh: Mesh,
|
||||||
|
collider: Collider,
|
||||||
|
) -> Entity {
|
||||||
|
let wheel_material = &StandardMaterial {
|
||||||
base_color: Color::srgb(0.01, 0.01, 0.01),
|
base_color: Color::srgb(0.01, 0.01, 0.01),
|
||||||
alpha_mode: AlphaMode::Opaque,
|
alpha_mode: AlphaMode::Opaque,
|
||||||
perceptual_roughness: 0.5,
|
perceptual_roughness: 0.5,
|
||||||
..Default::default()
|
..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);
|
let xform = Transform::from_translation(position);
|
||||||
|
|
||||||
|
@ -109,15 +161,19 @@ fn helper(
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01),
|
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01),
|
||||||
))
|
))
|
||||||
.with_children(move |parent| {
|
.with_children(|parent| {
|
||||||
let mesh: Mesh = Sphere::new(0.1).into();
|
let mesh: Mesh = Sphere::new(0.1).into();
|
||||||
parent.spawn(PbrBundle {
|
parent.spawn(PbrBundle {
|
||||||
mesh: meshes.add(mesh),
|
mesh: meshes.add(mesh),
|
||||||
material: materials.add(wheel_material.clone()),
|
material: materials.add(wheel_material.clone()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
let tire_id = parent
|
})
|
||||||
|
.id();
|
||||||
|
|
||||||
|
let tire = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
SpatialBundle::from_transform(xform),
|
||||||
CyberWheel,
|
CyberWheel,
|
||||||
RigidBody::Dynamic,
|
RigidBody::Dynamic,
|
||||||
collider.clone(),
|
collider.clone(),
|
||||||
|
@ -125,42 +181,20 @@ fn helper(
|
||||||
CollisionLayers::from_bits(2, 2),
|
CollisionLayers::from_bits(2, 2),
|
||||||
ExternalTorque::ZERO.with_persistence(false),
|
ExternalTorque::ZERO.with_persistence(false),
|
||||||
))
|
))
|
||||||
.id();
|
.with_children(|p| {
|
||||||
let _ = tref.insert(tire_id);
|
p.spawn(PbrBundle {
|
||||||
|
mesh: meshes.add(tire_mesh),
|
||||||
|
material: materials.add(wheel_material.clone()),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.id();
|
.id();
|
||||||
|
|
||||||
// connect hubs and tires to make wheels
|
// 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
|
hub
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_wheels(
|
|
||||||
mut commands: Commands,
|
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
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;
|
pub struct CyberBikePlugin;
|
||||||
|
|
||||||
impl Plugin for CyberBikePlugin {
|
impl Plugin for CyberBikePlugin {
|
||||||
|
|
Loading…
Reference in a new issue