update to bevyu 0.15, avian 0.2

This commit is contained in:
Joe Ardent 2025-01-26 16:45:43 -08:00
parent cacb322fac
commit fc81b75dfe
10 changed files with 93 additions and 94 deletions

View file

@ -67,7 +67,7 @@ pub(super) fn apply_lean(
// only try to correct roll if we're not totally vertical // only try to correct roll if we're not totally vertical
if pitch_error.abs() < 0.95 { 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 = let mag =
(settings.kp * roll_error) + (settings.ki * integral) + (settings.kd * derivative); (settings.kp * roll_error) + (settings.ki * integral) + (settings.kd * derivative);
if mag.is_normal() { if mag.is_normal() {

View file

@ -5,8 +5,10 @@ use avian3d::prelude::{
}; };
use bevy::{ use bevy::{
core::Name, core::Name,
prelude::{AssetServer, BuildChildren, Commands, Quat, Res, SpatialBundle, Transform, Vec3}, prelude::{
scene::SceneBundle, AssetServer, BuildChildren, ChildBuild, Commands, Quat, Res, Transform, Vec3, Visibility,
},
scene::SceneRoot,
}; };
use super::{spawn_wheels, CyberBikeBody, Meshterial, WheelConfig}; use super::{spawn_wheels, CyberBikeBody, Meshterial, WheelConfig};
@ -44,8 +46,9 @@ pub(super) fn spawn_cyberbike(
let body_collider = let body_collider =
Collider::capsule_endpoints(0.5, Vec3::new(0.0, 0.0, -0.65), Vec3::new(0.0, 0.0, 0.8)); 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 let bike = commands
.spawn(SpatialBundle::from_transform(xform)) .spawn(spatial_bundle)
.insert(( .insert((
Name::new("bike body"), Name::new("bike body"),
RigidBody::Dynamic, RigidBody::Dynamic,
@ -64,10 +67,7 @@ pub(super) fn spawn_cyberbike(
ExternalTorque::ZERO.with_persistence(false), ExternalTorque::ZERO.with_persistence(false),
)) ))
.with_children(|rider| { .with_children(|rider| {
rider.spawn(SceneBundle { rider.spawn(SceneRoot(scene));
scene,
..Default::default()
});
}) })
.id(); .id();

View file

@ -5,9 +5,12 @@ use avian3d::{
Friction, Joint, MassPropertiesBundle, Restitution, RevoluteJoint, RigidBody, SweptCcd, Friction, Joint, MassPropertiesBundle, Restitution, RevoluteJoint, RigidBody, SweptCcd,
}, },
}; };
use bevy::prelude::{ use bevy::{
AlphaMode, Assets, Color, Commands, Entity, Mesh, Name, PbrBundle, Quat, ResMut, Sphere, pbr::MeshMaterial3d,
StandardMaterial, Torus, Transform, Vec3, prelude::{
AlphaMode, Assets, Color, Commands, Entity, Mesh, Mesh3d, Name, Quat, ResMut, Sphere,
StandardMaterial, Torus, Transform, Vec3, Visibility,
},
}; };
use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig}; use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig};
@ -99,30 +102,32 @@ fn wheels_helper(
let xform = Transform::from_translation(position); let xform = Transform::from_translation(position);
let hub_mesh: Mesh = Sphere::new(0.1).into(); 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 let hub = commands
.spawn(( .spawn((
Name::new(format!("{pos_name} hub")), Name::new(format!("{pos_name} hub")),
RigidBody::Dynamic, RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 1000.0), MassPropertiesBundle::from_shape(&Collider::sphere(0.1), 1000.0),
CollisionLayers::NONE, CollisionLayers::NONE,
PbrBundle { hub_bundle,
mesh: meshes.add(hub_mesh),
material: materials.add(wheel_material.clone()),
transform: xform,
..Default::default()
},
)) ))
.id(); .id();
let tire_bundle = (
Mesh3d(meshes.add(tire_mesh)),
MeshMaterial3d(materials.add(wheel_material.clone())),
xform,
Visibility::Visible,
);
let tire = commands let tire = commands
.spawn(( .spawn((
Name::new(format!("{pos_name} tire")), Name::new(format!("{pos_name} tire")),
PbrBundle { tire_bundle,
mesh: meshes.add(tire_mesh),
material: materials.add(wheel_material.clone()),
transform: xform,
..Default::default()
},
CyberWheel, CyberWheel,
RigidBody::Dynamic, RigidBody::Dynamic,
collider, collider,

View file

@ -45,16 +45,16 @@ fn setup_cybercams(mut commands: Commands, asset_server: Res<AssetServer>) {
}; };
let id = commands let id = commands
.spawn(Camera3dBundle { .spawn((
projection: bevy::render::camera::Projection::Perspective(hero_projection), Camera3d::default(),
..Default::default() bevy::render::camera::Projection::Perspective(hero_projection),
}) ))
.insert(CyberCameras::Hero) .insert((CyberCameras::Hero, Msaa::Sample4))
.id(); .id();
commands commands
.spawn(Camera3dBundle::default()) .spawn(Camera3d::default())
.insert(CyberCameras::Debug); .insert((CyberCameras::Debug, Msaa::Sample4));
setup_ui(commands, asset_server, id); setup_ui(commands, asset_server, id);
} }

View file

@ -54,13 +54,11 @@ fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputS
for pad_event in events.read() { for pad_event in events.read() {
match pad_event { match pad_event {
GamepadEvent::Button(button_event) => { GamepadEvent::Button(button_event) => {
let GamepadButtonChangedEvent { let GamepadButtonChangedEvent { button, value, .. } = button_event;
button_type, value, .. match button {
} = button_event; GamepadButton::RightTrigger2 => istate.throttle = *value,
match button_type { GamepadButton::LeftTrigger2 => istate.throttle = -value,
GamepadButtonType::RightTrigger2 => istate.throttle = *value, GamepadButton::East => {
GamepadButtonType::LeftTrigger2 => istate.throttle = -value,
GamepadButtonType::East => {
if value > &0.5 { if value > &0.5 {
istate.brake = true; istate.brake = true;
} else { } else {
@ -71,14 +69,12 @@ fn update_input(mut events: EventReader<GamepadEvent>, mut istate: ResMut<InputS
} }
} }
GamepadEvent::Axis(axis_event) => { GamepadEvent::Axis(axis_event) => {
let GamepadAxisChangedEvent { let GamepadAxisChangedEvent { axis, value, .. } = axis_event;
axis_type, value, .. match axis {
} = axis_event; GamepadAxis::LeftStickX => {
match axis_type {
GamepadAxisType::LeftStickX => {
istate.yaw = *value; istate.yaw = *value;
} }
GamepadAxisType::RightStickY => { GamepadAxis::RightStickY => {
istate.pitch = *value; istate.pitch = *value;
} }
_ => {} _ => {}

View file

@ -13,17 +13,18 @@ pub mod lights;
pub mod planet; pub mod planet;
pub mod ui; pub mod ui;
#[derive(PhysicsLayer)] #[derive(PhysicsLayer, Default)]
pub enum ColliderGroups { pub enum ColliderGroups {
BikeBody, BikeBody,
Wheels, Wheels,
#[default]
Planet, Planet,
} }
pub fn disable_mouse_trap(mut window: Query<&mut Window, With<PrimaryWindow>>) { pub fn disable_mouse_trap(mut window: Query<&mut Window, With<PrimaryWindow>>) {
let mut window = window.get_single_mut().unwrap(); let mut window = window.get_single_mut().unwrap();
window.cursor.grab_mode = bevy::window::CursorGrabMode::None; window.cursor_options.grab_mode = bevy::window::CursorGrabMode::None;
window.cursor.visible = true; window.cursor_options.visible = true;
} }
pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 { pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {

View file

@ -44,39 +44,41 @@ fn spawn_static_lights(
// up light // up light
commands commands
.spawn(PointLightBundle { .spawn((
transform: Transform::from_xyz(0.0, PLANET_RADIUS + 30.0, 0.0), Transform::from_xyz(0.0, PLANET_RADIUS + 30.0, 0.0),
point_light: pink_light, pink_light,
..Default::default() Visibility::Visible,
}) ))
.with_children(|builder| { .with_children(|builder| {
builder.spawn(PbrBundle { builder.spawn((
mesh: meshes.add(Mesh::from(Sphere::new(10.0))), Mesh3d(meshes.add(Mesh::from(Sphere::new(10.0)))),
material: materials.add(StandardMaterial { MeshMaterial3d(materials.add(StandardMaterial {
base_color: BLUE, base_color: BLUE,
emissive: PINK.into(), emissive: PINK.into(),
..Default::default() ..Default::default()
}), })),
..Default::default() Transform::default(),
}); Visibility::Inherited,
));
}); });
// down light // down light
commands commands
.spawn(PointLightBundle { .spawn((
transform: Transform::from_xyz(0.0, -PLANET_RADIUS - 30.0, 0.0), Transform::from_xyz(0.0, -PLANET_RADIUS - 30.0, 0.0),
point_light: blue_light, blue_light,
..Default::default() Visibility::Visible,
}) ))
.with_children(|builder| { .with_children(|builder| {
builder.spawn(PbrBundle { builder.spawn((
mesh: meshes.add(Mesh::from(Sphere::new(10.0))), Mesh3d(meshes.add(Mesh::from(Sphere::new(10.0)))),
material: materials.add(StandardMaterial { MeshMaterial3d(materials.add(StandardMaterial {
base_color: PINK, base_color: PINK,
emissive: BLUE.into(), emissive: BLUE.into(),
..Default::default() ..Default::default()
}), })),
..Default::default() Transform::default(),
}); Visibility::Inherited,
));
}); });
} }

View file

@ -12,8 +12,7 @@ const CYBER_SKY: Color = Color::srgb(0.64, 0.745, 0.937); // a light blue sky
fn main() { fn main() {
let mut app = App::new(); let mut app = App::new();
app.insert_resource(Msaa::Sample4) app.insert_resource(ClearColor(CYBER_SKY))
.insert_resource(ClearColor(CYBER_SKY))
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
resolution: (2560.0, 1440.0).into(), resolution: (2560.0, 1440.0).into(),

View file

@ -35,11 +35,12 @@ fn spawn_planet(
); );
commands commands
.spawn(PbrBundle { .spawn((
mesh: meshes.add(mesh), Mesh3d(meshes.add(mesh)),
material: materials.add(Color::WHITE), MeshMaterial3d(materials.add(Color::WHITE)),
..Default::default() Transform::default(),
}) Visibility::Visible,
))
.insert(( .insert((
RigidBody::Static, RigidBody::Static,
pcollide, pcollide,

View file

@ -2,10 +2,11 @@ use avian3d::prelude::LinearVelocity;
use bevy::{ use bevy::{
app::Update, app::Update,
prelude::{ prelude::{
AlignSelf, App, AssetServer, Color, Commands, Component, Entity, Plugin, Query, Res, Style, AlignSelf, App, AssetServer, Color, Commands, Component, Entity, Plugin, Query, Res, Text,
Text, TextBundle, TextSection, TextStyle, With, With,
}, },
ui::TargetCamera, text::{TextColor, TextFont},
ui::{Node, TargetCamera},
}; };
use crate::bike::CyberBikeBody; use crate::bike::CyberBikeBody;
@ -19,26 +20,20 @@ pub(crate) fn setup_ui(
target_camera: Entity, target_camera: Entity,
) { ) {
commands commands
.spawn(TextBundle { .spawn((
style: Style { Node {
align_self: AlignSelf::FlexEnd, align_self: AlignSelf::FlexEnd,
..Default::default() ..Default::default()
}, },
// Use `Text` directly // Use `Text` directly
text: Text { TextFont {
// Construct a `Vec` of `TextSection`s
sections: vec![TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"), font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 40.0, font_size: 40.0,
color: Color::srgba_u8(255, 215, 0, 230),
},
}],
..Default::default() ..Default::default()
}, },
..Default::default() TextColor(Color::srgba_u8(255, 215, 0, 230)),
}) Text::default(),
))
.insert((UpText, TargetCamera(target_camera))); .insert((UpText, TargetCamera(target_camera)));
} }
@ -49,7 +44,7 @@ fn update_ui(
let mut text = text_query.single_mut(); let mut text = text_query.single_mut();
let velocity = state_query.single(); let velocity = state_query.single();
let speed = velocity.0.length(); let speed = velocity.0.length();
text.sections[0].value = format!("spd: {:.2}", speed); text.0 = format!("spd: {:.2}", speed);
} }
pub struct CyberUIPlugin; pub struct CyberUIPlugin;