merge from rolling2

This commit is contained in:
Joe Ardent 2023-02-17 19:02:17 -08:00
commit adc6be7c7b
9 changed files with 135 additions and 125 deletions

68
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "ab_glyph" name = "ab_glyph"
version = "0.2.19" version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5568a4aa5ba8adf5175c5c460b030e27d8893412976cc37bef0e4fbc16cfbba" checksum = "fe21446ad43aa56417a767f3e2f3d7c4ca522904de1dd640529a76e9c5c3b33c"
dependencies = [ dependencies = [
"ab_glyph_rasterizer", "ab_glyph_rasterizer",
"owned_ttf_parser", "owned_ttf_parser",
@ -84,9 +84,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.68" version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
[[package]] [[package]]
name = "approx" name = "approx"
@ -1417,9 +1417,9 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.8.0" version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
dependencies = [ dependencies = [
"instant", "instant",
] ]
@ -2012,14 +2012,14 @@ dependencies = [
[[package]] [[package]]
name = "mio" name = "mio"
version = "0.8.5" version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
"wasi", "wasi",
"windows-sys 0.42.0", "windows-sys 0.45.0",
] ]
[[package]] [[package]]
@ -2164,7 +2164,7 @@ checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]] [[package]]
name = "noise" name = "noise"
version = "0.8.2" version = "0.8.2"
source = "git+https://github.com/Razaekel/noise-rs#a029579c8c1fc4a18f7c84a2f5298721e7f56f2a" source = "git+https://github.com/Razaekel/noise-rs#2ac0431bafd8016d6705f30cbee367bb7018c171"
dependencies = [ dependencies = [
"num-traits", "num-traits",
"rand", "rand",
@ -2243,18 +2243,18 @@ dependencies = [
[[package]] [[package]]
name = "num_enum" name = "num_enum"
version = "0.5.9" version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d829733185c1ca374f17e52b762f24f535ec625d2cc1f070e34c8a9068f341b" checksum = "3e0072973714303aa6e3631c7e8e777970cf4bdd25dc4932e41031027b8bcc4e"
dependencies = [ dependencies = [
"num_enum_derive", "num_enum_derive",
] ]
[[package]] [[package]]
name = "num_enum_derive" name = "num_enum_derive"
version = "0.5.9" version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2be1598bf1c313dcdd12092e3f1920f463462525a21b7b4e11b4168353d0123e" checksum = "0629cbd6b897944899b1f10496d9c4a7ac5878d45fd61bc22e9e79bfbbc29597"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2303,9 +2303,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.17.0" version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]] [[package]]
name = "optional" name = "optional"
@ -2392,9 +2392,9 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]] [[package]]
name = "petgraph" name = "petgraph"
version = "0.6.2" version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143" checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4"
dependencies = [ dependencies = [
"fixedbitset", "fixedbitset",
"indexmap", "indexmap",
@ -2457,9 +2457,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.50" version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -2526,9 +2526,9 @@ dependencies = [
[[package]] [[package]]
name = "range-alloc" name = "range-alloc"
version = "0.1.2" version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e935c45e09cc6dcf00d2f0b2d630a58f4095320223d47fc68918722f0538b6" checksum = "9c8a99fddc9f0ba0a85884b8d14e3592853e787d581ca1816c91349b10e4eeab"
[[package]] [[package]]
name = "rapier3d" name = "rapier3d"
@ -2713,9 +2713,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.91" version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@ -2872,10 +2872,11 @@ dependencies = [
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.4" version = "1.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152"
dependencies = [ dependencies = [
"cfg-if",
"once_cell", "once_cell",
] ]
@ -3388,21 +3389,6 @@ dependencies = [
"windows_x86_64_msvc 0.36.1", "windows_x86_64_msvc 0.36.1",
] ]
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc 0.42.1",
"windows_i686_gnu 0.42.1",
"windows_i686_msvc 0.42.1",
"windows_x86_64_gnu 0.42.1",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc 0.42.1",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.45.0" version = "0.45.0"

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: 20.0, kp: 40.0,
kd: 4.5, kd: 5.0,
ki: 0.07, ki: 0.1,
} }
} }
} }
@ -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.0, pitch_limit: 0.8,
} }
} }
} }

View file

@ -1,9 +1,10 @@
use std::f32::consts::PI;
#[cfg(feature = "inspector")] #[cfg(feature = "inspector")]
use std::time::Instant; use std::time::Instant;
use bevy::prelude::{Commands, Entity, Query, Res, ResMut, Time, Transform, Vec3, With, Without}; use bevy::prelude::{Commands, Entity, Query, Res, ResMut, Time, Transform, Vec3, With, Without};
use bevy_rapier3d::prelude::{ use bevy_rapier3d::prelude::{
CollisionGroups, ExternalForce, Friction, Group, QueryFilter, RapierConfiguration, CollisionGroups, ExternalForce, Group, MultibodyJoint, QueryFilter, RapierConfiguration,
RapierContext, ReadMassProperties, Velocity, RapierContext, ReadMassProperties, Velocity,
}; };
@ -11,7 +12,7 @@ use bevy_rapier3d::prelude::{
use super::ActionDebugInstant; use super::ActionDebugInstant;
use super::{CatControllerSettings, CatControllerState, MovementSettings, Tunneling}; use super::{CatControllerSettings, CatControllerState, MovementSettings, Tunneling};
use crate::{ use crate::{
bike::{CyberBikeBody, CyberWheel, BIKE_WHEEL_COLLISION_GROUP}, bike::{CyberBikeBody, CyberSteering, CyberWheel, BIKE_WHEEL_COLLISION_GROUP},
input::InputState, input::InputState,
}; };
@ -61,7 +62,7 @@ pub(super) fn falling_cat(
let (derivative, integral) = control_vars.update_pitch(pitch_error, time.delta_seconds()); let (derivative, integral) = control_vars.update_pitch(pitch_error, time.delta_seconds());
let mag = (settings.kp * pitch_error) + (settings.ki * integral) + (settings.kd * derivative); let mag = (settings.kp * pitch_error) + (settings.ki * integral) + (settings.kd * derivative);
if mag.is_finite() { if mag.is_finite() {
forces.torque += wright * mag; forces.torque += wright * mag * 0.25;
} }
#[cfg(feature = "inspector")] #[cfg(feature = "inspector")]
@ -79,25 +80,42 @@ pub(super) fn falling_cat(
pub(super) fn input_forces( pub(super) fn input_forces(
settings: Res<MovementSettings>, settings: Res<MovementSettings>,
input: Res<InputState>, input: Res<InputState>,
mut cquery: Query<&mut Friction, With<CyberWheel>>, mut braking_query: Query<&mut MultibodyJoint, (Without<CyberSteering>, With<CyberWheel>)>,
mut bquery: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>, mut body_query: Query<
(&Transform, &mut ExternalForce),
(With<CyberBikeBody>, Without<CyberSteering>),
>,
mut steering_query: Query<&mut MultibodyJoint, With<CyberSteering>>,
) { ) {
let (xform, mut forces) = bquery.single_mut(); let (xform, mut forces) = body_query.single_mut();
// thrust // thrust
let thrust = xform.forward() * input.throttle * settings.accel; let thrust = xform.forward() * input.throttle * settings.accel;
forces.force += thrust; let point = xform.translation + xform.back() * 0.5;
let force = ExternalForce::at_point(thrust, point, xform.translation);
*forces += force;
// brake // brake
for mut friction in cquery.iter_mut() { for mut motor in braking_query.iter_mut() {
friction.coefficient = if input.brake { 2.0 } else { 0.0 }; let factor = if input.brake { 100.00 } else { 0.0 };
motor.data = (*motor
.data
.as_revolute_mut()
.unwrap()
.set_motor_max_force(factor)
.set_motor_velocity(0.0, factor))
.into();
} }
// steering // steering
let force = xform.right() * input.yaw * settings.sensitivity; let angle = input.yaw.powf(3.0) * (PI / 4.0);
let point = xform.translation + xform.forward(); let mut steering = steering_query.single_mut();
let force = ExternalForce::at_point(force, point, xform.translation); steering.data = (*steering
*forces += force; .data
.as_revolute_mut()
.unwrap()
.set_motor_position(-angle, 100.0, 0.5))
.into();
} }
/// Don't let the wheels get stuck underneat the planet /// Don't let the wheels get stuck underneat the planet
@ -122,7 +140,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: 6, frames: 10,
dir: -hit.1.normal, dir: -hit.1.normal,
}); });
} }
@ -153,8 +171,8 @@ pub(super) fn tunnel_out(
} }
tunneling.frames -= 1; tunneling.frames -= 1;
force.force = tunneling.dir * settings.gravity * 1.5 * mprops.0.mass; force.force = tunneling.dir * settings.gravity * 1.5 * mprops.0.mass;
#[cfg(feature = "inspector")] //#[cfg(feature = "inspector")]
dbg!(&tunneling); //dbg!(&tunneling);
} }
} }

View file

@ -56,7 +56,7 @@ pub(super) fn spawn_cyberbike(
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(spatialbundle) .insert(spatialbundle)
.insert(( .insert((
Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.50), Collider::capsule(Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8), 0.50),
bike_collision_group, bike_collision_group,
mass_properties, mass_properties,
damping, damping,

View file

@ -6,6 +6,9 @@ use bevy::{
#[derive(Component)] #[derive(Component)]
pub struct CyberBikeBody; pub struct CyberBikeBody;
#[derive(Component)]
pub struct CyberSteering;
#[derive(Debug, Component)] #[derive(Debug, Component)]
pub struct CyberWheel; pub struct CyberWheel;
@ -28,7 +31,7 @@ impl Default for WheelConfig {
front_forward: 0.9, front_forward: 0.9,
front_stance: 0.65, front_stance: 0.65,
rear_back: 1.1, rear_back: 1.1,
y: -0.45, y: -0.1,
limits: [-0.7, 0.1], limits: [-0.7, 0.1],
stiffness: 90.0, stiffness: 90.0,
damping: 8.0, damping: 8.0,

View file

@ -1,21 +1,19 @@
use bevy::prelude::{shape::UVSphere as Tire, *}; use bevy::prelude::{shape::UVSphere as Tire, *};
use bevy_rapier3d::prelude::{ use bevy_rapier3d::prelude::{
Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction, Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping,
MultibodyJoint, PrismaticJointBuilder, Restitution, RigidBody, Sleeping, ExternalForce, Friction, MultibodyJoint, PrismaticJointBuilder, Restitution,
TransformInterpolation, RevoluteJointBuilder, RigidBody, Sleeping, TransformInterpolation,
}; };
use super::{CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP}; use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
pub fn spawn_tires( pub fn spawn_tires(
commands: &mut Commands, commands: &mut Commands,
xform: &Transform, _xform: &Transform,
bike: Entity, bike: Entity,
conf: &WheelConfig, conf: &WheelConfig,
meshterials: &mut Meshterial, meshterials: &mut Meshterial,
) { ) {
//return;
// re-set the collision group
let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP; let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP;
let wheels_collision_group = CollisionGroups::new(membership, filter); let wheels_collision_group = CollisionGroups::new(membership, filter);
let wheel_y = conf.y; let wheel_y = conf.y;
@ -49,8 +47,8 @@ pub fn spawn_tires(
}; };
let friction = Friction { let friction = Friction {
coefficient: 0.0, coefficient: 0.8,
..Default::default() combine_rule: CoefficientCombineRule::Min,
}; };
let mut wheel_poses = Vec::with_capacity(2); let mut wheel_poses = Vec::with_capacity(2);
@ -60,7 +58,7 @@ pub fn spawn_tires(
let wheel_x = 0.0; let wheel_x = 0.0;
let wheel_z = -conf.front_forward; let wheel_z = -conf.front_forward;
let offset = Vec3::new(wheel_x, wheel_y, wheel_z); let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
wheel_poses.push(offset); wheel_poses.push((offset, Some(CyberSteering)));
} }
// rear // rear
@ -68,58 +66,60 @@ pub fn spawn_tires(
let wheel_x = 0.0; let wheel_x = 0.0;
let wheel_z = conf.rear_back; let wheel_z = conf.rear_back;
let offset = Vec3::new(wheel_x, wheel_y, wheel_z); let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
wheel_poses.push(offset); wheel_poses.push((offset, None));
} }
for offset in wheel_poses { for (offset, steering) in wheel_poses {
let trans = xform.translation + offset;
let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans);
let wheel_damping = Damping { let wheel_damping = Damping {
linear_damping: 0.8, linear_damping: 0.8,
..Default::default() ..Default::default()
}; };
let wheel_collider = Collider::ball(wheel_rad); let wheel_collider = Collider::ball(wheel_rad);
let mass_props = ColliderMassProperties::Density(0.1); let mass_props = ColliderMassProperties::Density(0.1);
let damping = conf.damping; let damping = conf.damping;
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
let prismatic_builder = PrismaticJointBuilder::new(Vec3::Y)
.local_anchor1(offset) .local_anchor1(offset)
.limits(limits) .limits(limits)
.motor_position(-0.1, stiffness, damping); .motor_position(limits[0], stiffness, damping);
let joint = MultibodyJoint::new(bike, prismatic); let prismatic_joint = MultibodyJoint::new(bike, prismatic_builder);
let fork_rb_entity = commands
let spatial_bundle = SpatialBundle {
transform: wheel_pos_in_world,
..Default::default()
};
let tire_spundle = SpatialBundle {
transform: Transform::IDENTITY,
..Default::default()
};
commands
.spawn(RigidBody::Dynamic) .spawn(RigidBody::Dynamic)
.insert(spatial_bundle) .insert(prismatic_joint)
.insert(( .id();
let axel_parent = if let Some(steering) = steering {
let neck_builder = RevoluteJointBuilder::new(Vec3::Y);
let neck_joint = MultibodyJoint::new(fork_rb_entity, neck_builder);
let hub = commands
.spawn(RigidBody::Dynamic)
.insert(neck_joint)
.insert(steering)
.id();
dbg!(hub);
hub
} else {
fork_rb_entity
};
let revolute_builder = RevoluteJointBuilder::new(Vec3::X);
let axel_joint = MultibodyJoint::new(axel_parent, revolute_builder);
commands.spawn(pbr_bundle.clone()).insert((
wheel_collider, wheel_collider,
mass_props, mass_props,
wheel_damping, wheel_damping,
ccd, ccd,
not_sleeping, not_sleeping,
joint, axel_joint,
wheels_collision_group, wheels_collision_group,
friction, friction,
CyberWheel, CyberWheel,
ExternalForce::default(), ExternalForce::default(),
)) Restitution::new(0.2),
.with_children(|wheel| { SpatialBundle::default(),
wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert( TransformInterpolation::default(),
TransformInterpolation { RigidBody::Dynamic,
start: None, ));
end: None,
},
);
});
} }
} }

View file

@ -120,12 +120,15 @@ pub struct CyberCamPlugin;
impl Plugin for CyberCamPlugin { impl Plugin for CyberCamPlugin {
fn build(&self, app: &mut bevy::prelude::App) { fn build(&self, app: &mut bevy::prelude::App) {
// common stuff common(app);
}
}
fn common(app: &mut bevy::prelude::App) {
app.insert_resource(DebugCamOffset::default()) app.insert_resource(DebugCamOffset::default())
.add_startup_system(setup_cybercams) .add_startup_system(setup_cybercams)
.add_state(CyberCameras::Hero) .add_state(CyberCameras::Hero)
.add_system(cycle_cam_state) .add_system(cycle_cam_state)
.add_system(update_active_camera) .add_system(update_active_camera)
.add_system(follow_cyberbike); .add_system(follow_cyberbike);
}
} }

View file

@ -16,8 +16,8 @@ fn update_debug_cam(mut offset: ResMut<DebugCamOffset>, mut keys: ResMut<Input<K
for key in keyset { for key in keyset {
match key { match key {
KeyCode::Left => offset.rot += 5.0, KeyCode::Left => offset.rot -= 5.0,
KeyCode::Right => offset.rot -= 5.0, KeyCode::Right => offset.rot += 5.0,
KeyCode::Up => { KeyCode::Up => {
if shifted { if shifted {
offset.alt += 0.5; offset.alt += 0.5;

View file

@ -32,10 +32,10 @@ fn spawn_planet(
let pcollide = ( let pcollide = (
shape, shape,
Friction { Friction {
coefficient: 0.05, coefficient: 0.8,
..Default::default() ..Default::default()
}, },
Restitution::new(0.0), Restitution::new(0.8),
); );
commands commands