Finish splitting up bike module, fix de-penetrator.

This commit is contained in:
Joe Ardent 2023-02-01 16:32:20 -08:00
parent 71a91da77a
commit c7680227e3
6 changed files with 79 additions and 98 deletions

View file

@ -58,9 +58,9 @@ pub struct CatControllerSettings {
impl Default for CatControllerSettings { impl Default for CatControllerSettings {
fn default() -> Self { fn default() -> Self {
Self { Self {
kp: 17.0, kp: 20.0,
kd: 4.0, kd: 4.5,
ki: 0.05, ki: 0.07,
} }
} }
} }
@ -85,7 +85,7 @@ impl Default for CatControllerState {
pitch_prev: Default::default(), pitch_prev: Default::default(),
decay_factor: 0.99, decay_factor: 0.99,
roll_limit: 1.5, roll_limit: 1.5,
pitch_limit: 1.2, pitch_limit: 1.0,
} }
} }
} }

View file

@ -21,7 +21,7 @@ impl Plugin for CyberActionPlugin {
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default()) .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default()) .add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_startup_system(zero_gravity) .add_startup_system(zero_gravity)
.add_system(surface_fix) .add_system(surface_fix.label("surface_fix"))
.add_system(gravity.before("cat")) .add_system(gravity.before("cat"))
.add_system(falling_cat.label("cat")) .add_system(falling_cat.label("cat"))
.add_system(input_forces.label("iforces").after("cat")) .add_system(input_forces.label("iforces").after("cat"))

View file

@ -122,7 +122,7 @@ pub(super) fn surface_fix(
cgroups.memberships = Group::NONE; cgroups.memberships = Group::NONE;
cgroups.filters = Group::NONE; cgroups.filters = Group::NONE;
commands.entity(entity).insert(Tunneling { commands.entity(entity).insert(Tunneling {
frames: 15, frames: 5,
dir: -hit.1.normal, dir: -hit.1.normal,
}); });
} }
@ -140,6 +140,7 @@ pub(super) fn tunnel_out(
), ),
With<CyberWheel>, With<CyberWheel>,
>, >,
settings: Res<MovementSettings>,
) { ) {
for (entity, mut tunneling, mut cgroups, mut force) in wheel_query.iter_mut() { for (entity, mut tunneling, mut cgroups, mut force) in wheel_query.iter_mut() {
if tunneling.frames == 0 { if tunneling.frames == 0 {
@ -149,7 +150,8 @@ pub(super) fn tunnel_out(
continue; continue;
} }
tunneling.frames -= 1; tunneling.frames -= 1;
force.force += tunneling.dir * tunneling.frames as f32; force.force += tunneling.dir * settings.gravity * 1.1;
#[cfg(feature = "inspector")]
dbg!(&tunneling); dbg!(&tunneling);
} }
} }

View file

@ -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,
}
}
}

View file

@ -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::<WheelConfig>()
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike);
}
}

View file

@ -1,15 +1,13 @@
use std::fmt::Debug;
use bevy::prelude::{shape::UVSphere as Tire, *}; use bevy::prelude::{shape::UVSphere as Tire, *};
use bevy_rapier3d::{ use bevy_rapier3d::prelude::{
geometry::Group, Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction,
prelude::{ MultibodyJoint, PrismaticJointBuilder, ReadMassProperties, Restitution, RigidBody, Sleeping,
Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, TransformInterpolation, Velocity,
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}; use crate::{action::CatControllerState, planet::PLANET_RADIUS};
type Meshterial<'a> = ( type Meshterial<'a> = (
@ -17,44 +15,7 @@ type Meshterial<'a> = (
ResMut<'a, Assets<StandardMaterial>>, ResMut<'a, Assets<StandardMaterial>>,
); );
#[derive(Component)] pub(super) fn spawn_cyberbike(
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(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
wheel_conf: Res<WheelConfig>, wheel_conf: Res<WheelConfig>,
@ -73,11 +34,6 @@ fn spawn_cyberbike(
angular_damping: 2.0, angular_damping: 2.0,
linear_damping: 0.1, 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 { let friction = Friction {
coefficient: 0.0, coefficient: 0.0,
@ -105,14 +61,14 @@ fn spawn_cyberbike(
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(spatialbundle) .insert(spatialbundle)
.insert(( .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, bike_collision_group,
mass_properties, mass_properties,
damping, damping,
restitution, restitution,
friction, friction,
not_sleeping, Sleeping::disabled(),
ccd, Ccd { enabled: true },
ReadMassProperties::default(), ReadMassProperties::default(),
)) ))
.insert(TransformInterpolation { .insert(TransformInterpolation {
@ -134,24 +90,6 @@ fn spawn_cyberbike(
spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials); spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials);
} }
#[cfg(feature = "inspector")]
fn re_tire(
mut commands: Commands,
wheel_conf: ResMut<WheelConfig>,
mut meshterials: Meshterial,
bquery: Query<(Entity, &Transform), With<CyberBikeBody>>,
wheels: Query<Entity, With<CyberWheel>>,
) {
// 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( fn spawn_tires(
commands: &mut Commands, commands: &mut Commands,
xform: &Transform, xform: &Transform,
@ -256,6 +194,7 @@ fn spawn_tires(
wheels_collision_group, wheels_collision_group,
friction, friction,
CyberWheel, CyberWheel,
ExternalForce::default(),
)) ))
.with_children(|wheel| { .with_children(|wheel| {
wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert( wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert(
@ -264,24 +203,6 @@ fn spawn_tires(
end: None, 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::<WheelConfig>()
.add_startup_system_to_stage(StartupStage::PostStartup, spawn_cyberbike);
#[cfg(feature = "inspector")]
{
app.add_system(re_tire);
}
}
}