From 3d0d86f24c3cf3e8ebb0e42abaec5b3fb161f4f6 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 15 Jul 2024 17:29:53 -0700 Subject: [PATCH] compiles and runs, but crashes --- Cargo.lock | 23 ++++++------ src/action/components.rs | 17 +-------- src/action/mod.rs | 14 +++---- src/action/systems.rs | 79 ++++++++++++++++++++++++++++++---------- src/bike/body.rs | 20 ++++------ src/bike/components.rs | 13 +++++-- src/bike/mod.rs | 4 +- src/bike/wheels.rs | 29 ++++++++++++--- src/camera.rs | 23 ++++++------ src/lights.rs | 33 ++++++----------- src/main.rs | 38 ++++++++++++++----- 11 files changed, 172 insertions(+), 121 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 225511b..bfb9348 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -284,9 +284,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "avian3d" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a8da53296a1bc166b84f99aa7c6b9d8c10c71f0c022bc4fc72dd1fc49323b29" +checksum = "54325576b6efa46576db611624db749b553d0c66e439862bdc60c43e01f012f9" dependencies = [ "avian_derive", "bevy", @@ -1223,9 +1223,9 @@ dependencies = [ [[package]] name = "blake3" -version = "1.5.2" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d08263faac5cde2a4d52b513dadb80846023aade56fcd8fc99ba73ba8050e92" +checksum = "e9ec96fe9a81b5e365f9db71fe00edc4fe4ca2cc7dcb7861f0603012a7caa210" dependencies = [ "arrayref", "arrayvec", @@ -1296,9 +1296,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" [[package]] name = "calloop" @@ -1316,13 +1316,12 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.2" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47de7e88bbbd467951ae7f5a6f34f70d1b4d9cfce53d5fd70f74ebe118b3db56" +checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052" dependencies = [ "jobserver", "libc", - "once_cell", ] [[package]] @@ -3067,7 +3066,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall 0.5.2", + "redox_syscall 0.5.3", "smallvec", "windows-targets 0.52.6", ] @@ -3367,9 +3366,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" dependencies = [ "bitflags 2.6.0", ] diff --git a/src/action/components.rs b/src/action/components.rs index 7a6587b..fc38312 100644 --- a/src/action/components.rs +++ b/src/action/components.rs @@ -1,7 +1,7 @@ use std::time::{Duration, Instant}; use bevy::{ - prelude::{Component, ReflectResource, Resource, Vec3}, + prelude::{Component, ReflectResource, Resource}, reflect::Reflect, }; @@ -26,21 +26,6 @@ impl ActionDebugInstant { } } -#[derive(Debug, Component)] -pub(super) struct Tunneling { - pub frames: usize, - pub dir: Vec3, -} - -impl Default for Tunneling { - fn default() -> Self { - Tunneling { - frames: 15, - dir: Vec3::ZERO, - } - } -} - #[derive(Debug, Resource, Reflect)] #[reflect(Resource)] pub struct MovementSettings { diff --git a/src/action/mod.rs b/src/action/mod.rs index 233d6a2..fe08eeb 100644 --- a/src/action/mod.rs +++ b/src/action/mod.rs @@ -1,6 +1,6 @@ -use avian3d::prelude::*; +use avian3d::prelude::{PhysicsPlugins, SubstepCount}; use bevy::{ - app::{Startup, Update}, + app::Update, diagnostic::FrameTimeDiagnosticsPlugin, ecs::reflect::ReflectResource, prelude::{App, IntoSystemConfigs, Plugin, Resource}, @@ -29,14 +29,14 @@ impl Plugin for CyberActionPlugin { .init_resource::() .register_type::() .register_type::() - .add_plugins(( - PhysicsPlugins::default(), - FrameTimeDiagnosticsPlugin::default(), - )) + .add_plugins((PhysicsPlugins::default(), FrameTimeDiagnosticsPlugin)) .insert_resource(SubstepCount(12)) .add_systems( Update, - (gravity, cyber_lean, falling_cat, input_forces).chain(), + ( + gravity, + (cyber_lean, suspension, falling_cat, input_forces).after(clear_forces), + ), ); } } diff --git a/src/action/systems.rs b/src/action/systems.rs index e1d9048..f0a39c1 100644 --- a/src/action/systems.rs +++ b/src/action/systems.rs @@ -1,17 +1,17 @@ use std::f32::consts::{FRAC_PI_3, FRAC_PI_4}; use avian3d::{ - dynamics::solver::xpbd::AngularConstraint, parry::mass_properties::MassProperties, prelude::*, -}; -use bevy::prelude::{ - Commands, Entity, Quat, Query, Res, ResMut, Time, Transform, Vec3, With, Without, + dynamics::solver::xpbd::AngularConstraint, + prelude::{ + ColliderMassProperties, ExternalForce, ExternalTorque, Gravity, LinearVelocity, + PrismaticJoint, RigidBodyQuery, + }, }; +use bevy::prelude::{Quat, Query, Res, ResMut, Time, Transform, Vec3, With, Without}; -//#[cfg(feature = "inspector")] -//use super::ActionDebugInstant; -use super::{CatControllerSettings, CatControllerState, CyberLean, MovementSettings, Tunneling}; +use super::{CatControllerSettings, CatControllerState, CyberLean, MovementSettings}; use crate::{ - bike::{CyberBikeBody, CyberSteering, CyberWheel, WheelConfig, BIKE_WHEEL_COLLISION_GROUP}, + bike::{CyberBikeBody, CyberFork, CyberSpring, CyberSteering, CyberWheel, WheelConfig}, input::InputState, }; @@ -37,14 +37,42 @@ fn rotate_point(pt: &Vec3, rot: &Quat) -> Vec3 { /// The gravity vector points from the cyberbike to the center of the planet. pub(super) fn gravity( - mut query: Query<(&Transform, &mut ExternalForce), With>, + xform: Query<&Transform, With>, + settings: Res, mut gravity: ResMut, - #[cfg(feature = "inspector")] mut debug_instant: ResMut, ) { - let (xform, mut forces) = query.single_mut(); + let xform = xform.single(); *gravity = Gravity(xform.translation.normalize() * -settings.gravity); - forces.clear(); + + // clear all external forces +} + +pub(super) fn clear_forces(mut forces: Query<(&mut ExternalForce, &mut ExternalTorque)>) { + for (mut force, mut torque) in forces.iter_mut() { + force.clear(); + torque.clear(); + } +} + +pub(super) fn suspension( + movment_settings: Res, + wheel_config: Res, + mass: Query<&ColliderMassProperties, With>, + mut axels: Query<(&mut ExternalForce, &CyberSpring, &Transform)>, +) { + let mass = if let Ok(mass) = mass.get_single() { + mass.mass.0 + } else { + 1.0 + }; + let gravity = movment_settings.gravity; + let mag = -wheel_config.stiffness * mass * gravity; + for (mut force, spring, xform) in axels.iter_mut() { + let spring = mag * spring.0; + let spring = xform.translation + spring; + let _ = force.apply_force(spring); + } } /// The desired lean angle, given steering input and speed. @@ -106,26 +134,30 @@ pub(super) fn input_forces( settings: Res, input: Res, time: Res