Update to Bevy 0.8. Not showing the cyberbike for some reason.

Physics is still probably fucked.
This commit is contained in:
Joe Ardent 2022-08-13 15:30:37 -07:00
parent 1579c3d972
commit bd96c52f3a
9 changed files with 609 additions and 582 deletions

1050
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,16 +5,17 @@ edition = "2021"
[dependencies] [dependencies]
rand = "0.8" rand = "0.8"
bevy_polyline = "0.2" bevy_polyline = "0.3"
noise = { git = "https://github.com/Razaekel/noise-rs" } noise = { git = "https://github.com/Razaekel/noise-rs" }
hexasphere = "7" hexasphere = "7"
wgpu = "0.12" wgpu = "0.13"
# wgpu = "0.12"
[features] [features]
debug_render = [] debug_render = []
[dependencies.bevy] [dependencies.bevy]
version = "0.7" version = "0.8"
default-features = false default-features = false
features = [ features = [
"bevy_gilrs", "bevy_gilrs",
@ -27,7 +28,7 @@ features = [
[dependencies.bevy_rapier3d] [dependencies.bevy_rapier3d]
features = ["simd-nightly"] features = ["simd-nightly"]
version = "0.13" version = "0.16"
# Maybe also enable only a small amount of optimization for our code: # Maybe also enable only a small amount of optimization for our code:
[profile.dev] [profile.dev]

View file

@ -1,4 +1,6 @@
use bevy::prelude::*; use bevy::prelude::{
App, ParallelSystemDescriptorCoercion, Plugin, Query, Res, ResMut, Transform, Vec3, With,
};
use bevy_rapier3d::prelude::{ use bevy_rapier3d::prelude::{
ExternalForce, Friction, NoUserData, RapierConfiguration, RapierPhysicsPlugin, Velocity, ExternalForce, Friction, NoUserData, RapierConfiguration, RapierPhysicsPlugin, Velocity,
}; };
@ -9,9 +11,9 @@ use crate::{
}; };
pub struct MovementSettings { pub struct MovementSettings {
pub sensitivity: f32,
pub accel: f32, pub accel: f32,
pub gravity: f32, pub gravity: f32,
pub sensitivity: f32,
} }
impl Default for MovementSettings { impl Default for MovementSettings {
@ -24,13 +26,17 @@ impl Default for MovementSettings {
} }
} }
fn zero_gravity(mut config: ResMut<RapierConfiguration>) {
config.gravity = Vec3::ZERO;
}
fn gravity( fn gravity(
xform: Query<&Transform, With<CyberBikeBody>>, mut query: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>,
settings: Res<MovementSettings>, settings: Res<MovementSettings>,
mut config: ResMut<RapierConfiguration>,
) { ) {
let gravity = xform.single().translation.normalize() * -settings.gravity; let (xform, mut forces) = query.single_mut();
config.gravity = gravity; let grav = xform.translation.normalize() * -settings.gravity;
forces.force = grav;
} }
fn falling_cat_pid( fn falling_cat_pid(
@ -70,7 +76,7 @@ fn falling_cat_pid(
let angvel = vels.angvel.as_ref(); let angvel = vels.angvel.as_ref();
let roll_rate = cam_forward.dot(Vec3::from_slice(angvel)).abs(); let roll_rate = cam_forward.dot(Vec3::from_slice(angvel)).abs();
dbg!(roll_rate); //dbg!(roll_rate);
if pitch_cos.abs() < 0.85 && roll_rate < 1.0 { if pitch_cos.abs() < 0.85 && roll_rate < 1.0 {
torque += xform.back() * mag; torque += xform.back() * mag;
} }
@ -90,8 +96,6 @@ fn falling_cat_pid(
let integral = (control_vars.pitch_sum * 0.7) + pitch_error; let integral = (control_vars.pitch_sum * 0.7) + pitch_error;
control_vars.pitch_sum = integral; control_vars.pitch_sum = integral;
dbg!(integral);
let proportional = pitch_error; let proportional = pitch_error;
let kp = 7.1; let kp = 7.1;
@ -101,7 +105,7 @@ fn falling_cat_pid(
let angvel = vels.angvel.as_ref(); let angvel = vels.angvel.as_ref();
let pitch_rate = cam_right.dot(Vec3::from_slice(angvel)).abs(); let pitch_rate = cam_right.dot(Vec3::from_slice(angvel)).abs();
dbg!(pitch_rate);
if roll_cos.abs() < 0.85 && pitch_rate < 2.0 { if roll_cos.abs() < 0.85 && pitch_rate < 2.0 {
torque += xform.left() * mag; torque += xform.left() * mag;
} }
@ -110,15 +114,6 @@ fn falling_cat_pid(
forces.torque = torque; forces.torque = torque;
} }
fn drag(mut query: Query<(&Velocity, &mut ExternalForce), With<CyberBikeBody>>) {
let (vels, mut forces) = query.single_mut();
if let Some(vel) = vels.linvel.try_normalize() {
let v2 = vels.linvel.length_squared();
forces.force -= vel * v2 * 0.02;
}
}
fn input_forces( fn input_forces(
settings: Res<MovementSettings>, settings: Res<MovementSettings>,
input: Res<InputState>, input: Res<InputState>,
@ -138,6 +133,19 @@ fn input_forces(
// steering // steering
let torque = xform.up() * input.yaw * settings.sensitivity; let torque = xform.up() * input.yaw * settings.sensitivity;
forces.torque += torque; forces.torque += torque;
//dbg!(&input);
}
fn drag(mut query: Query<(&Velocity, &mut ExternalForce), With<CyberBikeBody>>) {
let (vels, mut forces) = query.single_mut();
if let Some(vel) = vels.linvel.try_normalize() {
let v2 = vels.linvel.length_squared();
forces.force -= vel * v2 * 0.02;
}
//dbg!(&forces);
} }
pub struct CyberActionPlugin; pub struct CyberActionPlugin;
@ -145,9 +153,10 @@ impl Plugin for CyberActionPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_resource::<MovementSettings>() app.init_resource::<MovementSettings>()
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default()) .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_system(gravity) .add_startup_system(zero_gravity)
.add_system(gravity.before("cat"))
.add_system(falling_cat_pid.label("cat")) .add_system(falling_cat_pid.label("cat"))
.add_system(drag.label("drag").after("iforces")) .add_system(input_forces.label("iforces").after("cat"))
.add_system(input_forces.label("iforces").after("cat")); .add_system(drag.label("drag").after("iforces"));
} }
} }

View file

@ -77,7 +77,10 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(ExternalForce::default()) .insert(ExternalForce::default())
.insert(CyberBikeCollider) .insert(CyberBikeCollider)
.with_children(|rider| { .with_children(|rider| {
rider.spawn_scene(asset_server.load("cyber-bike_no_y_up.glb#Scene0")); rider.spawn_bundle(SceneBundle {
scene: asset_server.load("cyber-bike_no_y_up.glb#Scene0"),
..Default::default()
});
}) })
.insert(CyberBikeModel) .insert(CyberBikeModel)
.insert(CyberBikeBody) .insert(CyberBikeBody)

View file

@ -1,9 +1,6 @@
use bevy::{ use bevy::prelude::{
prelude::{ info, Camera, Camera3dBundle, Commands, Component, Input, KeyCode, ParamSet,
info, Commands, Component, Entity, Input, KeyCode, ParamSet, PerspectiveCameraBundle,
PerspectiveProjection, Plugin, Quat, Query, Res, ResMut, State, Transform, With, PerspectiveProjection, Plugin, Quat, Query, Res, ResMut, State, Transform, With,
},
render::camera::{ActiveCamera, Camera3d},
}; };
use crate::{bike::CyberBikeModel, input::InputState}; use crate::{bike::CyberBikeModel, input::InputState};
@ -32,14 +29,14 @@ fn setup_cybercams(mut commands: Commands) {
..Default::default() ..Default::default()
}; };
commands commands
.spawn_bundle(PerspectiveCameraBundle { .spawn_bundle(Camera3dBundle {
perspective_projection: hero_projection, projection: bevy::render::camera::Projection::Perspective(hero_projection),
..Default::default() ..Default::default()
}) })
.insert(CyberCameras::Hero); .insert(CyberCameras::Hero);
commands commands
.spawn_bundle(PerspectiveCameraBundle::default()) .spawn_bundle(Camera3dBundle::default())
.insert(CyberCameras::Debug); .insert(CyberCameras::Debug);
} }
@ -82,16 +79,16 @@ fn follow_cyberbike(
} }
fn update_active_camera( fn update_active_camera(
mut active_cams: ResMut<ActiveCamera<Camera3d>>,
state: Res<State<CyberCameras>>, state: Res<State<CyberCameras>>,
mut query: Query<(&CyberCameras, Entity)>, mut query: Query<(&mut Camera, &CyberCameras)>,
) { ) {
// find the camera with the current state, set it as the ActiveCamera // find the camera with the current state, set it as the ActiveCamera
query query.iter_mut().for_each(|(mut cam, cyber)| {
.iter_mut() if cyber.eq(state.current()) {
.filter(|(cybercam, _)| state.current().eq(cybercam)) cam.is_active = true;
.for_each(|(_, entity)| { } else {
active_cams.set(entity); cam.is_active = false;
}
}); });
} }

View file

@ -51,6 +51,7 @@ fn wireframe_planet(
width: 101.0, width: 101.0,
color: BISEXY_COLOR, color: BISEXY_COLOR,
perspective: true, perspective: true,
depth_bias: -0.001,
}), }),
..Default::default() ..Default::default()
}); });

View file

@ -1,43 +1,42 @@
use bevy::{ use bevy::prelude::{
ecs::event::{Events, ManualEventReader}, info, App, EventReader, GamepadAxisType, GamepadButtonType, GamepadEvent, GamepadEventType,
prelude::*, Plugin, ResMut,
}; };
#[derive(Default)] #[derive(Default, Debug)]
pub(crate) struct InputState { pub(crate) struct InputState {
event_reader: ManualEventReader<GamepadEvent>,
pub yaw: f32, pub yaw: f32,
pub throttle: f32, pub throttle: f32,
pub brake: bool, pub brake: bool,
pub pitch: f32, pub pitch: f32,
} }
fn update_input(events: Res<Events<GamepadEvent>>, mut istate: ResMut<InputState>) { fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) {
let mut throttle = 0.0; for GamepadEvent {
let mut brake = false; gamepad: _,
let mut pitch = 0.0; event_type: ev,
let mut yaw = 0.0; } in events.iter()
for GamepadEvent(_, ev) in istate.event_reader.iter(&events) { {
match *ev { match *ev {
GamepadEventType::ButtonChanged(GamepadButtonType::RightTrigger2, val) => { GamepadEventType::ButtonChanged(GamepadButtonType::RightTrigger2, val) => {
throttle += val; istate.throttle = val;
} }
GamepadEventType::ButtonChanged(GamepadButtonType::LeftTrigger2, val) => { GamepadEventType::ButtonChanged(GamepadButtonType::LeftTrigger2, val) => {
throttle -= val; istate.throttle = -val;
} }
GamepadEventType::ButtonChanged(GamepadButtonType::East, val) => { GamepadEventType::ButtonChanged(GamepadButtonType::East, val) => {
if val > 0.5 { if val > 0.5 {
brake = true; istate.brake = true;
} else { } else {
brake = false; istate.brake = false;
} }
} }
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickX, val) => { GamepadEventType::AxisChanged(GamepadAxisType::LeftStickX, val) => {
yaw -= val; istate.yaw = val;
} }
// ignore spurious vertical movement for now // ignore spurious vertical movement for now
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, val) => { GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, val) => {
pitch += val; istate.pitch = val;
} }
_ => { _ => {
info!("unhandled gamepad event: {:?}", ev); info!("unhandled gamepad event: {:?}", ev);
@ -45,10 +44,7 @@ fn update_input(events: Res<Events<GamepadEvent>>, mut istate: ResMut<InputState
} }
} }
istate.throttle = throttle; dbg!(&istate);
istate.brake = brake;
istate.pitch = pitch;
istate.yaw = yaw;
} }
pub struct CyberInputPlugin; pub struct CyberInputPlugin;

View file

@ -37,7 +37,7 @@ fn main() {
.add_plugin(CyberUIPlugin) .add_plugin(CyberUIPlugin)
.add_plugin(CyberBikePlugin) .add_plugin(CyberBikePlugin)
.add_startup_system(disable_mouse_trap) .add_startup_system(disable_mouse_trap)
.add_system(bevy::input::system::exit_on_esc_system); .add_system(bevy::window::close_on_esc);
app.run(); app.run();
} }

View file

@ -7,7 +7,7 @@ use crate::bike::CyberBikeBody;
struct UpText; struct UpText;
fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) { fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(UiCameraBundle::default()); //commands.spawn_bundle(UiCameraBundle::default());
commands commands
.spawn_bundle(TextBundle { .spawn_bundle(TextBundle {