From a5b9e549dbc9d53fb62a7946c7cda8aa1ebc35b0 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 2 Feb 2023 17:26:28 -0800 Subject: [PATCH] fix de-penetrator --- src/action/systems.rs | 14 +++-- src/bike/body.rs | 86 ++++++++++++++++++++++++++++ src/bike/mod.rs | 14 +++-- src/bike/{systems.rs => wheels.rs} | 91 ++---------------------------- 4 files changed, 108 insertions(+), 97 deletions(-) create mode 100644 src/bike/body.rs rename src/bike/{systems.rs => wheels.rs} (57%) diff --git a/src/action/systems.rs b/src/action/systems.rs index 0b02919..5b822bb 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: 5, + frames: 6, dir: -hit.1.normal, }); } @@ -135,22 +135,24 @@ pub(super) fn tunnel_out( ( Entity, &mut Tunneling, - &mut CollisionGroups, &mut ExternalForce, + &mut CollisionGroups, ), With, >, + mprops: Query<&ReadMassProperties, With>, settings: Res, ) { - for (entity, mut tunneling, mut cgroups, mut force) in wheel_query.iter_mut() { + let mprops = mprops.single(); + for (entity, mut tunneling, mut force, mut cgroups) in wheel_query.iter_mut() { if tunneling.frames == 0 { commands.entity(entity).remove::(); - cgroups.memberships = BIKE_WHEEL_COLLISION_GROUP.0; - cgroups.filters = BIKE_WHEEL_COLLISION_GROUP.1; + force.force = Vec3::ZERO; + (cgroups.memberships, cgroups.filters) = BIKE_WHEEL_COLLISION_GROUP; continue; } tunneling.frames -= 1; - force.force += tunneling.dir * settings.gravity * 1.1; + force.force = tunneling.dir * settings.gravity * 1.5 * mprops.0.mass; #[cfg(feature = "inspector")] dbg!(&tunneling); } diff --git a/src/bike/body.rs b/src/bike/body.rs new file mode 100644 index 0000000..765007b --- /dev/null +++ b/src/bike/body.rs @@ -0,0 +1,86 @@ +use bevy::{ + prelude::{AssetServer, BuildChildren, Commands, Quat, Res, SpatialBundle, Transform, Vec3}, + scene::SceneBundle, +}; +use bevy_rapier3d::prelude::{ + Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, + ReadMassProperties, Restitution, RigidBody, Sleeping, TransformInterpolation, Velocity, +}; + +use super::{spawn_tires, CyberBikeBody, Meshterial, WheelConfig, BIKE_BODY_COLLISION_GROUP}; +use crate::{action::CatControllerState, planet::PLANET_RADIUS}; + +pub(super) fn spawn_cyberbike( + mut commands: Commands, + asset_server: Res, + wheel_conf: Res, + mut meshterials: Meshterial, +) { + let altitude = PLANET_RADIUS - 220.0; + + let mut xform = Transform::from_translation(Vec3::X * altitude) + .with_rotation(Quat::from_axis_angle(Vec3::Z, -89.0f32.to_radians())); + //.with_rotation(Quat::from_axis_angle(Vec3::X, 140.0f32.to_radians())); + + let right = xform.right() * 350.0; + xform.translation += right; + + let damping = Damping { + angular_damping: 2.0, + linear_damping: 0.1, + }; + + let friction = Friction { + coefficient: 0.3, + ..Default::default() + }; + + let restitution = Restitution { + coefficient: 0.0, + ..Default::default() + }; + + let mass_properties = ColliderMassProperties::Density(0.9); + + let (membership, filter) = BIKE_BODY_COLLISION_GROUP; + let bike_collision_group = CollisionGroups::new(membership, filter); + + let scene = asset_server.load("cb-no-y_up.glb#Scene0"); + + let spatialbundle = SpatialBundle { + transform: xform, + ..Default::default() + }; + + let bike = commands + .spawn(RigidBody::Dynamic) + .insert(spatialbundle) + .insert(( + 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, + Sleeping::disabled(), + Ccd { enabled: true }, + ReadMassProperties::default(), + )) + .insert(TransformInterpolation { + start: None, + end: None, + }) + .insert(Velocity::zero()) + .insert(ExternalForce::default()) + .with_children(|rider| { + rider.spawn(SceneBundle { + scene, + ..Default::default() + }); + }) + .insert(CyberBikeBody) + .insert(CatControllerState::default()) + .id(); + + spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials); +} diff --git a/src/bike/mod.rs b/src/bike/mod.rs index beed5ff..9765aa2 100644 --- a/src/bike/mod.rs +++ b/src/bike/mod.rs @@ -1,15 +1,21 @@ +mod body; mod components; -mod systems; +mod wheels; -use bevy::prelude::{App, Plugin, StartupStage}; +use bevy::prelude::{App, Assets, Mesh, Plugin, ResMut, StandardMaterial, StartupStage}; use bevy_rapier3d::prelude::Group; -pub use self::components::*; -use self::systems::spawn_cyberbike; +pub(crate) use self::components::*; +use self::{body::spawn_cyberbike, wheels::spawn_tires}; 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); +type Meshterial<'a> = ( + ResMut<'a, Assets>, + ResMut<'a, Assets>, +); + pub struct CyberBikePlugin; impl Plugin for CyberBikePlugin { fn build(&self, app: &mut App) { diff --git a/src/bike/systems.rs b/src/bike/wheels.rs similarity index 57% rename from src/bike/systems.rs rename to src/bike/wheels.rs index a7664b6..bd0f1f5 100644 --- a/src/bike/systems.rs +++ b/src/bike/wheels.rs @@ -1,96 +1,13 @@ use bevy::prelude::{shape::UVSphere as Tire, *}; use bevy_rapier3d::prelude::{ Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, - MultibodyJoint, PrismaticJointBuilder, ReadMassProperties, Restitution, RigidBody, Sleeping, - TransformInterpolation, Velocity, + MultibodyJoint, PrismaticJointBuilder, Restitution, RigidBody, Sleeping, + TransformInterpolation, }; -use super::{ - CyberBikeBody, CyberWheel, WheelConfig, BIKE_BODY_COLLISION_GROUP, BIKE_WHEEL_COLLISION_GROUP, -}; -use crate::{action::CatControllerState, planet::PLANET_RADIUS}; +use super::{CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP}; -type Meshterial<'a> = ( - ResMut<'a, Assets>, - ResMut<'a, Assets>, -); - -pub(super) fn spawn_cyberbike( - mut commands: Commands, - asset_server: Res, - wheel_conf: Res, - mut meshterials: Meshterial, -) { - let altitude = PLANET_RADIUS - 220.0; - - let mut xform = Transform::from_translation(Vec3::X * altitude) - .with_rotation(Quat::from_axis_angle(Vec3::Z, -89.0f32.to_radians())); - //.with_rotation(Quat::from_axis_angle(Vec3::X, 140.0f32.to_radians())); - - let right = xform.right() * 350.0; - xform.translation += right; - - let damping = Damping { - angular_damping: 2.0, - linear_damping: 0.1, - }; - - let friction = Friction { - coefficient: 0.0, - ..Default::default() - }; - - let restitution = Restitution { - coefficient: 0.0, - ..Default::default() - }; - - let mass_properties = ColliderMassProperties::Density(0.9); - - let (membership, filter) = BIKE_BODY_COLLISION_GROUP; - let bike_collision_group = CollisionGroups::new(membership, filter); - - let scene = asset_server.load("cb-no-y_up.glb#Scene0"); - - let spatialbundle = SpatialBundle { - transform: xform, - ..Default::default() - }; - - let bike = commands - .spawn(RigidBody::Dynamic) - .insert(spatialbundle) - .insert(( - 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, - Sleeping::disabled(), - Ccd { enabled: true }, - ReadMassProperties::default(), - )) - .insert(TransformInterpolation { - start: None, - end: None, - }) - .insert(Velocity::zero()) - .insert(ExternalForce::default()) - .with_children(|rider| { - rider.spawn(SceneBundle { - scene, - ..Default::default() - }); - }) - .insert(CyberBikeBody) - .insert(CatControllerState::default()) - .id(); - - spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials); -} - -fn spawn_tires( +pub fn spawn_tires( commands: &mut Commands, xform: &Transform, bike: Entity,