Update to Bevy 0.8. Not showing the cyberbike for some reason.
Physics is still probably fucked.
This commit is contained in:
parent
1579c3d972
commit
bd96c52f3a
9 changed files with 609 additions and 582 deletions
1050
Cargo.lock
generated
1050
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -5,16 +5,17 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
rand = "0.8"
|
||||
bevy_polyline = "0.2"
|
||||
bevy_polyline = "0.3"
|
||||
noise = { git = "https://github.com/Razaekel/noise-rs" }
|
||||
hexasphere = "7"
|
||||
wgpu = "0.12"
|
||||
wgpu = "0.13"
|
||||
# wgpu = "0.12"
|
||||
|
||||
[features]
|
||||
debug_render = []
|
||||
|
||||
[dependencies.bevy]
|
||||
version = "0.7"
|
||||
version = "0.8"
|
||||
default-features = false
|
||||
features = [
|
||||
"bevy_gilrs",
|
||||
|
@ -27,7 +28,7 @@ features = [
|
|||
|
||||
[dependencies.bevy_rapier3d]
|
||||
features = ["simd-nightly"]
|
||||
version = "0.13"
|
||||
version = "0.16"
|
||||
|
||||
# Maybe also enable only a small amount of optimization for our code:
|
||||
[profile.dev]
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy::prelude::{
|
||||
App, ParallelSystemDescriptorCoercion, Plugin, Query, Res, ResMut, Transform, Vec3, With,
|
||||
};
|
||||
use bevy_rapier3d::prelude::{
|
||||
ExternalForce, Friction, NoUserData, RapierConfiguration, RapierPhysicsPlugin, Velocity,
|
||||
};
|
||||
|
@ -9,9 +11,9 @@ use crate::{
|
|||
};
|
||||
|
||||
pub struct MovementSettings {
|
||||
pub sensitivity: f32,
|
||||
pub accel: f32,
|
||||
pub gravity: f32,
|
||||
pub sensitivity: f32,
|
||||
}
|
||||
|
||||
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(
|
||||
xform: Query<&Transform, With<CyberBikeBody>>,
|
||||
mut query: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>,
|
||||
settings: Res<MovementSettings>,
|
||||
mut config: ResMut<RapierConfiguration>,
|
||||
) {
|
||||
let gravity = xform.single().translation.normalize() * -settings.gravity;
|
||||
config.gravity = gravity;
|
||||
let (xform, mut forces) = query.single_mut();
|
||||
let grav = xform.translation.normalize() * -settings.gravity;
|
||||
forces.force = grav;
|
||||
}
|
||||
|
||||
fn falling_cat_pid(
|
||||
|
@ -70,7 +76,7 @@ fn falling_cat_pid(
|
|||
|
||||
let angvel = vels.angvel.as_ref();
|
||||
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 {
|
||||
torque += xform.back() * mag;
|
||||
}
|
||||
|
@ -90,8 +96,6 @@ fn falling_cat_pid(
|
|||
let integral = (control_vars.pitch_sum * 0.7) + pitch_error;
|
||||
control_vars.pitch_sum = integral;
|
||||
|
||||
dbg!(integral);
|
||||
|
||||
let proportional = pitch_error;
|
||||
|
||||
let kp = 7.1;
|
||||
|
@ -101,7 +105,7 @@ fn falling_cat_pid(
|
|||
|
||||
let angvel = vels.angvel.as_ref();
|
||||
let pitch_rate = cam_right.dot(Vec3::from_slice(angvel)).abs();
|
||||
dbg!(pitch_rate);
|
||||
|
||||
if roll_cos.abs() < 0.85 && pitch_rate < 2.0 {
|
||||
torque += xform.left() * mag;
|
||||
}
|
||||
|
@ -110,15 +114,6 @@ fn falling_cat_pid(
|
|||
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(
|
||||
settings: Res<MovementSettings>,
|
||||
input: Res<InputState>,
|
||||
|
@ -138,6 +133,19 @@ fn input_forces(
|
|||
// steering
|
||||
let torque = xform.up() * input.yaw * settings.sensitivity;
|
||||
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;
|
||||
|
@ -145,9 +153,10 @@ impl Plugin for CyberActionPlugin {
|
|||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<MovementSettings>()
|
||||
.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(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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,7 +77,10 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
.insert(ExternalForce::default())
|
||||
.insert(CyberBikeCollider)
|
||||
.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(CyberBikeBody)
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
use bevy::{
|
||||
prelude::{
|
||||
info, Commands, Component, Entity, Input, KeyCode, ParamSet, PerspectiveCameraBundle,
|
||||
use bevy::prelude::{
|
||||
info, Camera, Camera3dBundle, Commands, Component, Input, KeyCode, ParamSet,
|
||||
PerspectiveProjection, Plugin, Quat, Query, Res, ResMut, State, Transform, With,
|
||||
},
|
||||
render::camera::{ActiveCamera, Camera3d},
|
||||
};
|
||||
|
||||
use crate::{bike::CyberBikeModel, input::InputState};
|
||||
|
@ -32,14 +29,14 @@ fn setup_cybercams(mut commands: Commands) {
|
|||
..Default::default()
|
||||
};
|
||||
commands
|
||||
.spawn_bundle(PerspectiveCameraBundle {
|
||||
perspective_projection: hero_projection,
|
||||
.spawn_bundle(Camera3dBundle {
|
||||
projection: bevy::render::camera::Projection::Perspective(hero_projection),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(CyberCameras::Hero);
|
||||
|
||||
commands
|
||||
.spawn_bundle(PerspectiveCameraBundle::default())
|
||||
.spawn_bundle(Camera3dBundle::default())
|
||||
.insert(CyberCameras::Debug);
|
||||
}
|
||||
|
||||
|
@ -82,16 +79,16 @@ fn follow_cyberbike(
|
|||
}
|
||||
|
||||
fn update_active_camera(
|
||||
mut active_cams: ResMut<ActiveCamera<Camera3d>>,
|
||||
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
|
||||
query
|
||||
.iter_mut()
|
||||
.filter(|(cybercam, _)| state.current().eq(cybercam))
|
||||
.for_each(|(_, entity)| {
|
||||
active_cams.set(entity);
|
||||
query.iter_mut().for_each(|(mut cam, cyber)| {
|
||||
if cyber.eq(state.current()) {
|
||||
cam.is_active = true;
|
||||
} else {
|
||||
cam.is_active = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ fn wireframe_planet(
|
|||
width: 101.0,
|
||||
color: BISEXY_COLOR,
|
||||
perspective: true,
|
||||
depth_bias: -0.001,
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
|
|
38
src/input.rs
38
src/input.rs
|
@ -1,43 +1,42 @@
|
|||
use bevy::{
|
||||
ecs::event::{Events, ManualEventReader},
|
||||
prelude::*,
|
||||
use bevy::prelude::{
|
||||
info, App, EventReader, GamepadAxisType, GamepadButtonType, GamepadEvent, GamepadEventType,
|
||||
Plugin, ResMut,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Debug)]
|
||||
pub(crate) struct InputState {
|
||||
event_reader: ManualEventReader<GamepadEvent>,
|
||||
pub yaw: f32,
|
||||
pub throttle: f32,
|
||||
pub brake: bool,
|
||||
pub pitch: f32,
|
||||
}
|
||||
|
||||
fn update_input(events: Res<Events<GamepadEvent>>, mut istate: ResMut<InputState>) {
|
||||
let mut throttle = 0.0;
|
||||
let mut brake = false;
|
||||
let mut pitch = 0.0;
|
||||
let mut yaw = 0.0;
|
||||
for GamepadEvent(_, ev) in istate.event_reader.iter(&events) {
|
||||
fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputState>) {
|
||||
for GamepadEvent {
|
||||
gamepad: _,
|
||||
event_type: ev,
|
||||
} in events.iter()
|
||||
{
|
||||
match *ev {
|
||||
GamepadEventType::ButtonChanged(GamepadButtonType::RightTrigger2, val) => {
|
||||
throttle += val;
|
||||
istate.throttle = val;
|
||||
}
|
||||
GamepadEventType::ButtonChanged(GamepadButtonType::LeftTrigger2, val) => {
|
||||
throttle -= val;
|
||||
istate.throttle = -val;
|
||||
}
|
||||
GamepadEventType::ButtonChanged(GamepadButtonType::East, val) => {
|
||||
if val > 0.5 {
|
||||
brake = true;
|
||||
istate.brake = true;
|
||||
} else {
|
||||
brake = false;
|
||||
istate.brake = false;
|
||||
}
|
||||
}
|
||||
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickX, val) => {
|
||||
yaw -= val;
|
||||
istate.yaw = val;
|
||||
}
|
||||
// ignore spurious vertical movement for now
|
||||
GamepadEventType::AxisChanged(GamepadAxisType::LeftStickY, val) => {
|
||||
pitch += val;
|
||||
istate.pitch = val;
|
||||
}
|
||||
_ => {
|
||||
info!("unhandled gamepad event: {:?}", ev);
|
||||
|
@ -45,10 +44,7 @@ fn update_input(events: Res<Events<GamepadEvent>>, mut istate: ResMut<InputState
|
|||
}
|
||||
}
|
||||
|
||||
istate.throttle = throttle;
|
||||
istate.brake = brake;
|
||||
istate.pitch = pitch;
|
||||
istate.yaw = yaw;
|
||||
dbg!(&istate);
|
||||
}
|
||||
|
||||
pub struct CyberInputPlugin;
|
||||
|
|
|
@ -37,7 +37,7 @@ fn main() {
|
|||
.add_plugin(CyberUIPlugin)
|
||||
.add_plugin(CyberBikePlugin)
|
||||
.add_startup_system(disable_mouse_trap)
|
||||
.add_system(bevy::input::system::exit_on_esc_system);
|
||||
.add_system(bevy::window::close_on_esc);
|
||||
|
||||
app.run();
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::bike::CyberBikeBody;
|
|||
struct UpText;
|
||||
|
||||
fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn_bundle(UiCameraBundle::default());
|
||||
//commands.spawn_bundle(UiCameraBundle::default());
|
||||
|
||||
commands
|
||||
.spawn_bundle(TextBundle {
|
||||
|
|
Loading…
Reference in a new issue