merge of physics from flatlander

This commit is contained in:
Joe Ardent 2023-02-17 18:03:17 -08:00
commit 6fd4f7c2e0
9 changed files with 145 additions and 127 deletions

68
Cargo.lock generated
View file

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

View file

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

View file

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

View file

@ -56,7 +56,7 @@ pub(super) fn spawn_cyberbike(
.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),
Collider::capsule(Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8), 0.50),
bike_collision_group,
mass_properties,
damping,

View file

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

View file

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

View file

@ -120,7 +120,11 @@ pub struct CyberCamPlugin;
impl Plugin for CyberCamPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
// common stuff
common(app);
}
}
fn common(app: &mut bevy::prelude::App) {
app.insert_resource(DebugCamOffset::default())
.add_startup_system(setup_cybercams)
.add_state(CyberCameras::Hero)
@ -128,4 +132,3 @@ impl Plugin for CyberCamPlugin {
.add_system(update_active_camera)
.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 {
match key {
KeyCode::Left => offset.rot += 5.0,
KeyCode::Right => offset.rot -= 5.0,
KeyCode::Left => offset.rot -= 5.0,
KeyCode::Right => offset.rot += 5.0,
KeyCode::Up => {
if shifted {
offset.alt += 0.5;

View file

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