From 79ddc3f951c0aa4deabe49dfed926aa84260cb4d Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 24 Feb 2022 23:20:29 -0800 Subject: [PATCH] Add drag, re-enable CCD for bike. --- src/action.rs | 38 ++++++++++++++++++++------------------ src/geometry.rs | 8 ++++---- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/action.rs b/src/action.rs index 266f82f..6db971a 100644 --- a/src/action.rs +++ b/src/action.rs @@ -55,13 +55,6 @@ fn setup_colliders( let pbody = RigidBodyBundle { body_type: RigidBodyType::Static.into(), - // ccd: RigidBodyCcd { - // ccd_enabled: true, - // ccd_max_dist: PLANET_RADIUS * 1.1, - // ccd_thickness: 0.1, - // ..Default::default() - // } - // .into(), ..Default::default() }; let pcollide = ColliderBundle { @@ -87,21 +80,18 @@ fn setup_colliders( fn setup_bike_collider(bike: Entity, xform: &Transform, commands: &mut Commands) { let mut bbody = RigidBodyBundle::default(); + bbody.damping.angular_damping = 0.8; - bbody.damping.linear_damping = 0.4; - bbody.forces = RigidBodyForces { - torque: Vec3::ZERO.into(), + bbody.damping.linear_damping = 0.1; + + bbody.ccd = RigidBodyCcd { + ccd_enabled: true, + ccd_thickness: 0.7, + ccd_max_dist: 1.5, ..Default::default() } .into(); - // bbody.ccd = RigidBodyCcd { - // ccd_enabled: false, - // ccd_max_dist: 2.4, - // ..Default::default() - // } - // .into(); - let isometry = Isometry::from_parts(xform.translation.into(), xform.rotation.into()); bbody.position = isometry.into(); // collider @@ -174,6 +164,17 @@ fn update_forces( forces.torque += torque; } +fn drag( + mut query: Query<(&RigidBodyVelocityComponent, &mut RigidBodyForcesComponent), With>, +) { + let (vels, mut forces) = query.single_mut(); + + if let Some(vel) = vels.linvel.try_normalize(0.001) { + let v2 = vels.linvel.magnitude_squared(); + forces.force -= vel * v2 * 0.002; + } +} + pub struct CyberActionPlugin; impl Plugin for CyberActionPlugin { fn build(&self, app: &mut App) { @@ -185,6 +186,7 @@ impl Plugin for CyberActionPlugin { ) .add_system(gravity) .add_system(falling_cat.label("cat")) - .add_system(update_forces.after("cat")); + .add_system(drag.label("drag").after("uforces")) + .add_system(update_forces.label("uforces").after("cat")); } } diff --git a/src/geometry.rs b/src/geometry.rs index 5413516..2c81d31 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -3,13 +3,13 @@ use bevy::{ render::mesh::Indices, }; use hexasphere::shapes::IcoSphere; -use noise::{HybridMulti, NoiseFn, Perlin}; +use noise::{HybridMulti, NoiseFn, Simplex}; use wgpu::PrimitiveTopology; use crate::Label; pub const PLANET_RADIUS: f32 = 5000.0; -pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.095; +pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 1.015; #[derive(Component, Debug)] pub struct CyberBike; @@ -25,7 +25,7 @@ fn spawn_giant_sphere( let color = Color::DARK_GRAY; let isphere = shape::Icosphere { radius: PLANET_RADIUS, - subdivisions: 40, + subdivisions: 64, }; let pmesh = gen_planet(isphere); @@ -89,7 +89,7 @@ fn gen_planet(sphere: Icosphere) -> Mesh { // TODO: use displaced points for normals by replacing raw_points with // noise-displaced points. - let noise = HybridMulti::::default(); + let noise = HybridMulti::::default(); let raw_points = generated .raw_points()