update to bevyu 0.15, avian 0.2
This commit is contained in:
parent
cacb322fac
commit
fc81b75dfe
10 changed files with 93 additions and 94 deletions
|
@ -67,7 +67,7 @@ pub(super) fn apply_lean(
|
|||
|
||||
// only try to correct roll if we're not totally vertical
|
||||
if pitch_error.abs() < 0.95 {
|
||||
let (derivative, integral) = control_vars.update_roll(roll_error, time.delta_seconds());
|
||||
let (derivative, integral) = control_vars.update_roll(roll_error, time.delta_secs());
|
||||
let mag =
|
||||
(settings.kp * roll_error) + (settings.ki * integral) + (settings.kd * derivative);
|
||||
if mag.is_normal() {
|
||||
|
|
|
@ -5,8 +5,10 @@ use avian3d::prelude::{
|
|||
};
|
||||
use bevy::{
|
||||
core::Name,
|
||||
prelude::{AssetServer, BuildChildren, Commands, Quat, Res, SpatialBundle, Transform, Vec3},
|
||||
scene::SceneBundle,
|
||||
prelude::{
|
||||
AssetServer, BuildChildren, ChildBuild, Commands, Quat, Res, Transform, Vec3, Visibility,
|
||||
},
|
||||
scene::SceneRoot,
|
||||
};
|
||||
|
||||
use super::{spawn_wheels, CyberBikeBody, Meshterial, WheelConfig};
|
||||
|
@ -44,8 +46,9 @@ pub(super) fn spawn_cyberbike(
|
|||
|
||||
let body_collider =
|
||||
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8));
|
||||
let spatial_bundle = (xform, Visibility::default());
|
||||
let bike = commands
|
||||
.spawn(SpatialBundle::from_transform(xform))
|
||||
.spawn(spatial_bundle)
|
||||
.insert((
|
||||
Name::new("bike body"),
|
||||
RigidBody::Dynamic,
|
||||
|
@ -64,10 +67,7 @@ pub(super) fn spawn_cyberbike(
|
|||
ExternalTorque::ZERO.with_persistence(false),
|
||||
))
|
||||
.with_children(|rider| {
|
||||
rider.spawn(SceneBundle {
|
||||
scene,
|
||||
..Default::default()
|
||||
});
|
||||
rider.spawn(SceneRoot(scene));
|
||||
})
|
||||
.id();
|
||||
|
||||
|
|
|
@ -5,9 +5,12 @@ use avian3d::{
|
|||
Friction, Joint, MassPropertiesBundle, Restitution, RevoluteJoint, RigidBody, SweptCcd,
|
||||
},
|
||||
};
|
||||
use bevy::prelude::{
|
||||
AlphaMode, Assets, Color, Commands, Entity, Mesh, Name, PbrBundle, Quat, ResMut, Sphere,
|
||||
StandardMaterial, Torus, Transform, Vec3,
|
||||
use bevy::{
|
||||
pbr::MeshMaterial3d,
|
||||
prelude::{
|
||||
AlphaMode, Assets, Color, Commands, Entity, Mesh, Mesh3d, Name, Quat, ResMut, Sphere,
|
||||
StandardMaterial, Torus, Transform, Vec3, Visibility,
|
||||
},
|
||||
};
|
||||
|
||||
use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig};
|
||||
|
@ -99,30 +102,32 @@ fn wheels_helper(
|
|||
let xform = Transform::from_translation(position);
|
||||
let hub_mesh: Mesh = Sphere::new(0.1).into();
|
||||
|
||||
let hub_bundle = (
|
||||
Mesh3d(meshes.add(hub_mesh)),
|
||||
MeshMaterial3d(materials.add(wheel_material.clone())),
|
||||
xform,
|
||||
Visibility::Visible,
|
||||
);
|
||||
let hub = commands
|
||||
.spawn((
|
||||
Name::new(format!("{pos_name} hub")),
|
||||
RigidBody::Dynamic,
|
||||
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 1000.0),
|
||||
MassPropertiesBundle::from_shape(&Collider::sphere(0.1), 1000.0),
|
||||
CollisionLayers::NONE,
|
||||
PbrBundle {
|
||||
mesh: meshes.add(hub_mesh),
|
||||
material: materials.add(wheel_material.clone()),
|
||||
transform: xform,
|
||||
..Default::default()
|
||||
},
|
||||
hub_bundle,
|
||||
))
|
||||
.id();
|
||||
|
||||
let tire_bundle = (
|
||||
Mesh3d(meshes.add(tire_mesh)),
|
||||
MeshMaterial3d(materials.add(wheel_material.clone())),
|
||||
xform,
|
||||
Visibility::Visible,
|
||||
);
|
||||
let tire = commands
|
||||
.spawn((
|
||||
Name::new(format!("{pos_name} tire")),
|
||||
PbrBundle {
|
||||
mesh: meshes.add(tire_mesh),
|
||||
material: materials.add(wheel_material.clone()),
|
||||
transform: xform,
|
||||
..Default::default()
|
||||
},
|
||||
tire_bundle,
|
||||
CyberWheel,
|
||||
RigidBody::Dynamic,
|
||||
collider,
|
||||
|
|
|
@ -45,16 +45,16 @@ fn setup_cybercams(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
};
|
||||
|
||||
let id = commands
|
||||
.spawn(Camera3dBundle {
|
||||
projection: bevy::render::camera::Projection::Perspective(hero_projection),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(CyberCameras::Hero)
|
||||
.spawn((
|
||||
Camera3d::default(),
|
||||
bevy::render::camera::Projection::Perspective(hero_projection),
|
||||
))
|
||||
.insert((CyberCameras::Hero, Msaa::Sample4))
|
||||
.id();
|
||||
|
||||
commands
|
||||
.spawn(Camera3dBundle::default())
|
||||
.insert(CyberCameras::Debug);
|
||||
.spawn(Camera3d::default())
|
||||
.insert((CyberCameras::Debug, Msaa::Sample4));
|
||||
|
||||
setup_ui(commands, asset_server, id);
|
||||
}
|
||||
|
|
22
src/input.rs
22
src/input.rs
|
@ -54,13 +54,11 @@ fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputS
|
|||
for pad_event in events.read() {
|
||||
match pad_event {
|
||||
GamepadEvent::Button(button_event) => {
|
||||
let GamepadButtonChangedEvent {
|
||||
button_type, value, ..
|
||||
} = button_event;
|
||||
match button_type {
|
||||
GamepadButtonType::RightTrigger2 => istate.throttle = *value,
|
||||
GamepadButtonType::LeftTrigger2 => istate.throttle = -value,
|
||||
GamepadButtonType::East => {
|
||||
let GamepadButtonChangedEvent { button, value, .. } = button_event;
|
||||
match button {
|
||||
GamepadButton::RightTrigger2 => istate.throttle = *value,
|
||||
GamepadButton::LeftTrigger2 => istate.throttle = -value,
|
||||
GamepadButton::East => {
|
||||
if value > &0.5 {
|
||||
istate.brake = true;
|
||||
} else {
|
||||
|
@ -71,14 +69,12 @@ fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputS
|
|||
}
|
||||
}
|
||||
GamepadEvent::Axis(axis_event) => {
|
||||
let GamepadAxisChangedEvent {
|
||||
axis_type, value, ..
|
||||
} = axis_event;
|
||||
match axis_type {
|
||||
GamepadAxisType::LeftStickX => {
|
||||
let GamepadAxisChangedEvent { axis, value, .. } = axis_event;
|
||||
match axis {
|
||||
GamepadAxis::LeftStickX => {
|
||||
istate.yaw = *value;
|
||||
}
|
||||
GamepadAxisType::RightStickY => {
|
||||
GamepadAxis::RightStickY => {
|
||||
istate.pitch = *value;
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -13,17 +13,18 @@ pub mod lights;
|
|||
pub mod planet;
|
||||
pub mod ui;
|
||||
|
||||
#[derive(PhysicsLayer)]
|
||||
#[derive(PhysicsLayer, Default)]
|
||||
pub enum ColliderGroups {
|
||||
BikeBody,
|
||||
Wheels,
|
||||
#[default]
|
||||
Planet,
|
||||
}
|
||||
|
||||
pub fn disable_mouse_trap(mut window: Query<&mut Window, With<PrimaryWindow>>) {
|
||||
let mut window = window.get_single_mut().unwrap();
|
||||
window.cursor.grab_mode = bevy::window::CursorGrabMode::None;
|
||||
window.cursor.visible = true;
|
||||
window.cursor_options.grab_mode = bevy::window::CursorGrabMode::None;
|
||||
window.cursor_options.visible = true;
|
||||
}
|
||||
|
||||
pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {
|
||||
|
|
|
@ -44,39 +44,41 @@ fn spawn_static_lights(
|
|||
|
||||
// up light
|
||||
commands
|
||||
.spawn(PointLightBundle {
|
||||
transform: Transform::from_xyz(0.0, PLANET_RADIUS + 30.0, 0.0),
|
||||
point_light: pink_light,
|
||||
..Default::default()
|
||||
})
|
||||
.spawn((
|
||||
Transform::from_xyz(0.0, PLANET_RADIUS + 30.0, 0.0),
|
||||
pink_light,
|
||||
Visibility::Visible,
|
||||
))
|
||||
.with_children(|builder| {
|
||||
builder.spawn(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(Sphere::new(10.0))),
|
||||
material: materials.add(StandardMaterial {
|
||||
builder.spawn((
|
||||
Mesh3d(meshes.add(Mesh::from(Sphere::new(10.0)))),
|
||||
MeshMaterial3d(materials.add(StandardMaterial {
|
||||
base_color: BLUE,
|
||||
emissive: PINK.into(),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
})),
|
||||
Transform::default(),
|
||||
Visibility::Inherited,
|
||||
));
|
||||
});
|
||||
// down light
|
||||
commands
|
||||
.spawn(PointLightBundle {
|
||||
transform: Transform::from_xyz(0.0, -PLANET_RADIUS - 30.0, 0.0),
|
||||
point_light: blue_light,
|
||||
..Default::default()
|
||||
})
|
||||
.spawn((
|
||||
Transform::from_xyz(0.0, -PLANET_RADIUS - 30.0, 0.0),
|
||||
blue_light,
|
||||
Visibility::Visible,
|
||||
))
|
||||
.with_children(|builder| {
|
||||
builder.spawn(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(Sphere::new(10.0))),
|
||||
material: materials.add(StandardMaterial {
|
||||
builder.spawn((
|
||||
Mesh3d(meshes.add(Mesh::from(Sphere::new(10.0)))),
|
||||
MeshMaterial3d(materials.add(StandardMaterial {
|
||||
base_color: PINK,
|
||||
emissive: BLUE.into(),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
})),
|
||||
Transform::default(),
|
||||
Visibility::Inherited,
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,7 @@ const CYBER_SKY: Color = Color::srgb(0.64, 0.745, 0.937); // a light blue sky
|
|||
|
||||
fn main() {
|
||||
let mut app = App::new();
|
||||
app.insert_resource(Msaa::Sample4)
|
||||
.insert_resource(ClearColor(CYBER_SKY))
|
||||
app.insert_resource(ClearColor(CYBER_SKY))
|
||||
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
resolution: (2560.0, 1440.0).into(),
|
||||
|
|
|
@ -35,11 +35,12 @@ fn spawn_planet(
|
|||
);
|
||||
|
||||
commands
|
||||
.spawn(PbrBundle {
|
||||
mesh: meshes.add(mesh),
|
||||
material: materials.add(Color::WHITE),
|
||||
..Default::default()
|
||||
})
|
||||
.spawn((
|
||||
Mesh3d(meshes.add(mesh)),
|
||||
MeshMaterial3d(materials.add(Color::WHITE)),
|
||||
Transform::default(),
|
||||
Visibility::Visible,
|
||||
))
|
||||
.insert((
|
||||
RigidBody::Static,
|
||||
pcollide,
|
||||
|
|
31
src/ui.rs
31
src/ui.rs
|
@ -2,10 +2,11 @@ use avian3d::prelude::LinearVelocity;
|
|||
use bevy::{
|
||||
app::Update,
|
||||
prelude::{
|
||||
AlignSelf, App, AssetServer, Color, Commands, Component, Entity, Plugin, Query, Res, Style,
|
||||
Text, TextBundle, TextSection, TextStyle, With,
|
||||
AlignSelf, App, AssetServer, Color, Commands, Component, Entity, Plugin, Query, Res, Text,
|
||||
With,
|
||||
},
|
||||
ui::TargetCamera,
|
||||
text::{TextColor, TextFont},
|
||||
ui::{Node, TargetCamera},
|
||||
};
|
||||
|
||||
use crate::bike::CyberBikeBody;
|
||||
|
@ -19,26 +20,20 @@ pub(crate) fn setup_ui(
|
|||
target_camera: Entity,
|
||||
) {
|
||||
commands
|
||||
.spawn(TextBundle {
|
||||
style: Style {
|
||||
.spawn((
|
||||
Node {
|
||||
align_self: AlignSelf::FlexEnd,
|
||||
..Default::default()
|
||||
},
|
||||
// Use `Text` directly
|
||||
text: Text {
|
||||
// Construct a `Vec` of `TextSection`s
|
||||
sections: vec![TextSection {
|
||||
value: "".to_string(),
|
||||
style: TextStyle {
|
||||
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
||||
font_size: 40.0,
|
||||
color: Color::srgba_u8(255, 215, 0, 230),
|
||||
},
|
||||
}],
|
||||
TextFont {
|
||||
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
||||
font_size: 40.0,
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
TextColor(Color::srgba_u8(255, 215, 0, 230)),
|
||||
Text::default(),
|
||||
))
|
||||
.insert((UpText, TargetCamera(target_camera)));
|
||||
}
|
||||
|
||||
|
@ -49,7 +44,7 @@ fn update_ui(
|
|||
let mut text = text_query.single_mut();
|
||||
let velocity = state_query.single();
|
||||
let speed = velocity.0.length();
|
||||
text.sections[0].value = format!("spd: {:.2}", speed);
|
||||
text.0 = format!("spd: {:.2}", speed);
|
||||
}
|
||||
|
||||
pub struct CyberUIPlugin;
|
||||
|
|
Loading…
Reference in a new issue