From c7680227e3bae253d1db5de401d210795e5ce808 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 1 Feb 2023 16:32:20 -0800 Subject: [PATCH] Finish splitting up `bike` module, fix de-penetrator. --- src/action/components.rs | 8 +-- src/action/mod.rs | 2 +- src/action/systems.rs | 6 ++- src/bike/components.rs | 38 +++++++++++++++ src/bike/mod.rs | 20 ++++++++ src/bike/systems.rs | 103 +++++---------------------------------- 6 files changed, 79 insertions(+), 98 deletions(-) diff --git a/src/action/components.rs b/src/action/components.rs index d71bdd4..656d6ef 100644 --- a/src/action/components.rs +++ b/src/action/components.rs @@ -58,9 +58,9 @@ pub struct CatControllerSettings { impl Default for CatControllerSettings { fn default() -> Self { Self { - kp: 17.0, - kd: 4.0, - ki: 0.05, + kp: 20.0, + kd: 4.5, + ki: 0.07, } } } @@ -85,7 +85,7 @@ impl Default for CatControllerState { pitch_prev: Default::default(), decay_factor: 0.99, roll_limit: 1.5, - pitch_limit: 1.2, + pitch_limit: 1.0, } } } diff --git a/src/action/mod.rs b/src/action/mod.rs index 642b1ed..3591d35 100644 --- a/src/action/mod.rs +++ b/src/action/mod.rs @@ -21,7 +21,7 @@ impl Plugin for CyberActionPlugin { .add_plugin(RapierPhysicsPlugin::::default()) .add_plugin(FrameTimeDiagnosticsPlugin::default()) .add_startup_system(zero_gravity) - .add_system(surface_fix) + .add_system(surface_fix.label("surface_fix")) .add_system(gravity.before("cat")) .add_system(falling_cat.label("cat")) .add_system(input_forces.label("iforces").after("cat")) diff --git a/src/action/systems.rs b/src/action/systems.rs index bc81bc8..0b02919 100644 --- a/src/action/systems.rs +++ b/src/action/systems.rs @@ -122,7 +122,7 @@ pub(super) fn surface_fix( cgroups.memberships = Group::NONE; cgroups.filters = Group::NONE; commands.entity(entity).insert(Tunneling { - frames: 15, + frames: 5, dir: -hit.1.normal, }); } @@ -140,6 +140,7 @@ pub(super) fn tunnel_out( ), With, >, + settings: Res, ) { for (entity, mut tunneling, mut cgroups, mut force) in wheel_query.iter_mut() { if tunneling.frames == 0 { @@ -149,7 +150,8 @@ pub(super) fn tunnel_out( continue; } tunneling.frames -= 1; - force.force += tunneling.dir * tunneling.frames as f32; + force.force += tunneling.dir * settings.gravity * 1.1; + #[cfg(feature = "inspector")] dbg!(&tunneling); } } diff --git a/src/bike/components.rs b/src/bike/components.rs index e69de29..bc89870 100644 --- a/src/bike/components.rs +++ b/src/bike/components.rs @@ -0,0 +1,38 @@ +use bevy::{ + prelude::{Component, ReflectResource, Resource}, + reflect::Reflect, +}; + +#[derive(Component)] +pub struct CyberBikeBody; + +#[derive(Debug, Component)] +pub struct CyberWheel; + +#[derive(Resource, Reflect)] +#[reflect(Resource)] +pub struct WheelConfig { + pub front_forward: f32, + pub front_stance: f32, + pub rear_back: f32, + pub y: f32, + pub limits: [f32; 2], + pub stiffness: f32, + pub damping: f32, + pub radius: f32, +} + +impl Default for WheelConfig { + fn default() -> Self { + Self { + front_forward: 0.9, + front_stance: 0.65, + rear_back: 1.1, + y: -0.45, + limits: [-0.7, 0.1], + stiffness: 90.0, + damping: 8.0, + radius: 0.3, + } + } +} diff --git a/src/bike/mod.rs b/src/bike/mod.rs index e69de29..beed5ff 100644 --- a/src/bike/mod.rs +++ b/src/bike/mod.rs @@ -0,0 +1,20 @@ +mod components; +mod systems; + +use bevy::prelude::{App, Plugin, StartupStage}; +use bevy_rapier3d::prelude::Group; + +pub use self::components::*; +use self::systems::spawn_cyberbike; + +pub const BIKE_BODY_COLLISION_GROUP: (Group, Group) = (Group::GROUP_1, Group::GROUP_1); +pub const BIKE_WHEEL_COLLISION_GROUP: (Group, Group) = (Group::GROUP_10, Group::GROUP_10); + +pub struct CyberBikePlugin; +impl Plugin for CyberBikePlugin { + fn build(&self, app: &mut App) { + app.insert_resource(WheelConfig::default()) + .register_type::() + .add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike); + } +} diff --git a/src/bike/systems.rs b/src/bike/systems.rs index a9ac527..a7664b6 100644 --- a/src/bike/systems.rs +++ b/src/bike/systems.rs @@ -1,15 +1,13 @@ -use std::fmt::Debug; - use bevy::prelude::{shape::UVSphere as Tire, *}; -use bevy_rapier3d::{ - geometry::Group, - prelude::{ - Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, - MultibodyJoint, PrismaticJointBuilder, ReadMassProperties, Restitution, RigidBody, - Sleeping, TransformInterpolation, Velocity, - }, +use bevy_rapier3d::prelude::{ + Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, + MultibodyJoint, PrismaticJointBuilder, ReadMassProperties, Restitution, RigidBody, Sleeping, + TransformInterpolation, Velocity, }; +use super::{ + CyberBikeBody, CyberWheel, WheelConfig, BIKE_BODY_COLLISION_GROUP, BIKE_WHEEL_COLLISION_GROUP, +}; use crate::{action::CatControllerState, planet::PLANET_RADIUS}; type Meshterial<'a> = ( @@ -17,44 +15,7 @@ type Meshterial<'a> = ( ResMut<'a, Assets>, ); -#[derive(Component)] -pub struct CyberBikeBody; - -#[derive(Debug, Component)] -pub struct CyberWheel; - -#[derive(Resource, Reflect)] -#[reflect(Resource)] -pub struct WheelConfig { - pub front_forward: f32, - pub front_stance: f32, - pub rear_back: f32, - pub y: f32, - pub limits: [f32; 2], - pub stiffness: f32, - pub damping: f32, - pub radius: f32, -} - -impl Default for WheelConfig { - fn default() -> Self { - Self { - front_forward: 0.9, - front_stance: 0.65, - rear_back: 1.1, - y: -0.45, - limits: [-0.7, 0.1], - stiffness: 90.0, - damping: 8.0, - radius: 0.3, - } - } -} - -pub const BIKE_BODY_COLLISION_GROUP: (Group, Group) = (Group::GROUP_1, Group::GROUP_1); -pub const BIKE_WHEEL_COLLISION_GROUP: (Group, Group) = (Group::GROUP_10, Group::GROUP_10); - -fn spawn_cyberbike( +pub(super) fn spawn_cyberbike( mut commands: Commands, asset_server: Res, wheel_conf: Res, @@ -73,11 +34,6 @@ fn spawn_cyberbike( angular_damping: 2.0, linear_damping: 0.1, }; - let not_sleeping = Sleeping::disabled(); - let ccd = Ccd { enabled: true }; - - let bcollider_shape = - Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.50); let friction = Friction { coefficient: 0.0, @@ -105,14 +61,14 @@ fn spawn_cyberbike( .spawn(RigidBody::Dynamic) .insert(spatialbundle) .insert(( - bcollider_shape, + Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.50), bike_collision_group, mass_properties, damping, restitution, friction, - not_sleeping, - ccd, + Sleeping::disabled(), + Ccd { enabled: true }, ReadMassProperties::default(), )) .insert(TransformInterpolation { @@ -134,24 +90,6 @@ fn spawn_cyberbike( spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials); } -#[cfg(feature = "inspector")] -fn re_tire( - mut commands: Commands, - wheel_conf: ResMut, - mut meshterials: Meshterial, - bquery: Query<(Entity, &Transform), With>, - wheels: Query>, -) { - // we fuck with values in the egui inspector - let (bike, xform) = bquery.single(); - if wheel_conf.is_changed() { - for wheel in wheels.iter() { - commands.entity(wheel).despawn_recursive(); - } - spawn_tires(&mut commands, xform, bike, &wheel_conf, &mut meshterials); - } -} - fn spawn_tires( commands: &mut Commands, xform: &Transform, @@ -256,6 +194,7 @@ fn spawn_tires( wheels_collision_group, friction, CyberWheel, + ExternalForce::default(), )) .with_children(|wheel| { wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert( @@ -264,24 +203,6 @@ fn spawn_tires( end: None, }, ); - }) - .insert(TransformInterpolation { - start: None, - end: None, }); } } - -pub struct CyberBikePlugin; -impl Plugin for CyberBikePlugin { - fn build(&self, app: &mut App) { - app.insert_resource(WheelConfig::default()) - .register_type::() - .add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike); - - #[cfg(feature = "inspector")] - { - app.add_system(re_tire); - } - } -}