Compare commits
No commits in common. "b54e0a637f858befe79a6d39551d6a0b329a1ab6" and "a675f3e52f1c8b484ef3fadbf4c15d0bb5f2e947" have entirely different histories.
b54e0a637f
...
a675f3e52f
6 changed files with 1146 additions and 787 deletions
1590
Cargo.lock
generated
1590
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -4,14 +4,12 @@ version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { version = "0.16", features = ["bevy_dev_tools", "configurable_error_handler"] }
|
bevy = { version = "0.15", features = ["bevy_dev_tools"] }
|
||||||
|
bevy-inspector-egui = "0.29"
|
||||||
|
|
||||||
[dependencies.avian3d]
|
[dependencies.avian3d]
|
||||||
default-features = false
|
default-features = false
|
||||||
#version = "0.2"
|
version = "0.2"
|
||||||
git = "https://github.com/Jondolf/avian"
|
|
||||||
branch = "main"
|
|
||||||
features = ["3d", "f32", "parry-f32", "debug-plugin", "default-collider", "collider-from-mesh"]
|
features = ["3d", "f32", "parry-f32", "debug-plugin", "default-collider", "collider-from-mesh"]
|
||||||
|
|
||||||
|
|
||||||
|
|
212
src/bike.rs
212
src/bike.rs
|
@ -1,24 +1,12 @@
|
||||||
use avian3d::{
|
use avian3d::{
|
||||||
math::{Scalar, FRAC_PI_2},
|
math::{Scalar, FRAC_PI_2},
|
||||||
prelude::{
|
prelude::*,
|
||||||
AngularDamping, Collider, ColliderDensity, CollisionLayers, ExternalForce, ExternalTorque,
|
|
||||||
LinearDamping, RayCaster, RigidBody, SleepingDisabled, SpatialQueryFilter,
|
|
||||||
TransformInterpolation,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use bevy::{
|
|
||||||
asset::Handle,
|
|
||||||
ecs::{bundle::Bundle, entity::Entity},
|
|
||||||
prelude::{
|
|
||||||
children, AlphaMode, App, Assets, Capsule3d, Color, Commands, Component, Dir3, Mesh,
|
|
||||||
Mesh3d, MeshMaterial3d, Name, Plugin, Reflect, ResMut, SpawnRelated, Sphere,
|
|
||||||
StandardMaterial, Startup, Transform, Vec3, Visibility,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::physics::CatControllerState;
|
use crate::physics::CatControllerState;
|
||||||
|
|
||||||
pub const SPRING_CONSTANT: Scalar = 50.0;
|
pub const SPRING_CONSTANT: Scalar = 60.0;
|
||||||
pub const DAMPING_CONSTANT: Scalar = 10.0;
|
pub const DAMPING_CONSTANT: Scalar = 10.0;
|
||||||
pub const WHEEL_RADIUS: Scalar = 0.4;
|
pub const WHEEL_RADIUS: Scalar = 0.4;
|
||||||
pub const REST_DISTANCE: Scalar = 1.5 + WHEEL_RADIUS;
|
pub const REST_DISTANCE: Scalar = 1.5 + WHEEL_RADIUS;
|
||||||
|
@ -36,6 +24,7 @@ pub enum CyberWheel {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Reflect, Default)]
|
#[derive(Component, Clone, Copy, Debug, Reflect, Default)]
|
||||||
|
#[reflect(Component)]
|
||||||
pub struct WheelState {
|
pub struct WheelState {
|
||||||
pub displacement: Scalar,
|
pub displacement: Scalar,
|
||||||
pub force_normal: Scalar,
|
pub force_normal: Scalar,
|
||||||
|
@ -51,6 +40,7 @@ impl WheelState {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Clone, Copy, Debug, Reflect)]
|
#[derive(Component, Clone, Copy, Debug, Reflect)]
|
||||||
|
#[reflect(Component)]
|
||||||
#[require(WheelState)]
|
#[require(WheelState)]
|
||||||
pub struct WheelConfig {
|
pub struct WheelConfig {
|
||||||
pub attach: Vec3,
|
pub attach: Vec3,
|
||||||
|
@ -92,105 +82,99 @@ fn spawn_bike(
|
||||||
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 mut body = commands.spawn((xform, Visibility::default()));
|
commands
|
||||||
body.insert((
|
.spawn((xform, Visibility::default()))
|
||||||
Name::new("body"),
|
.insert((
|
||||||
RigidBody::Dynamic,
|
Name::new("body"),
|
||||||
body_collider,
|
RigidBody::Dynamic,
|
||||||
CollisionLayers::from_bits(1, 1),
|
body_collider,
|
||||||
SleepingDisabled,
|
CollisionLayers::from_bits(1, 1),
|
||||||
CyberBikeBody,
|
SleepingDisabled,
|
||||||
CatControllerState::default(),
|
CyberBikeBody,
|
||||||
ColliderDensity(1.4),
|
CatControllerState::default(),
|
||||||
AngularDamping(0.2),
|
ColliderDensity(1.2),
|
||||||
LinearDamping(0.1),
|
AngularDamping(0.2),
|
||||||
ExternalForce::ZERO.with_persistence(false),
|
LinearDamping(0.1),
|
||||||
ExternalTorque::ZERO.with_persistence(false),
|
ExternalForce::ZERO.with_persistence(false),
|
||||||
));
|
ExternalTorque::ZERO.with_persistence(false),
|
||||||
|
))
|
||||||
let pbr_bundle = {
|
.with_children(|builder| {
|
||||||
let color = Color::srgb(0.7, 0.05, 0.7);
|
let color = Color::srgb(0.7, 0.05, 0.7);
|
||||||
let mut xform = Transform::default();
|
let mut xform = Transform::default();
|
||||||
xform.rotate_x(FRAC_PI_2);
|
xform.rotate_x(FRAC_PI_2);
|
||||||
(
|
let pbr_bundle = (
|
||||||
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.45))),
|
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.45))),
|
||||||
xform,
|
xform,
|
||||||
Visibility::Visible,
|
MeshMaterial3d(materials.add(color)),
|
||||||
MeshMaterial3d(materials.add(color)),
|
);
|
||||||
TransformInterpolation,
|
builder.spawn(pbr_bundle);
|
||||||
)
|
spawn_wheels(builder, meshes, materials, builder.parent_entity());
|
||||||
};
|
});
|
||||||
let parent = body.id();
|
|
||||||
body.insert(spawn_children(parent, pbr_bundle, meshes, materials));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_children(
|
fn spawn_wheels(
|
||||||
parent: Entity,
|
commands: &mut ChildBuilder,
|
||||||
body: impl Bundle,
|
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
) -> impl Bundle {
|
body: Entity,
|
||||||
|
) {
|
||||||
let mesh: Mesh = Sphere::new(WHEEL_RADIUS).into();
|
let mesh: Mesh = Sphere::new(WHEEL_RADIUS).into();
|
||||||
let wheel_material = StandardMaterial {
|
|
||||||
base_color: Color::srgb(0.01, 0.01, 0.01),
|
|
||||||
alpha_mode: AlphaMode::Opaque,
|
|
||||||
perceptual_roughness: 0.5,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mesh = meshes.add(mesh);
|
|
||||||
let material = materials.add(wheel_material);
|
|
||||||
|
|
||||||
let front_rake = Vec3::new(0.0, -1.0, -0.9).normalize(); // about 30 degrees
|
let front_rake = Vec3::new(0.0, -1.0, -0.9).normalize(); // about 30 degrees
|
||||||
let front_wheel_pos = FRONT_ATTACH + (front_rake * REST_DISTANCE);
|
let front_wheel_pos = FRONT_ATTACH + (front_rake * REST_DISTANCE);
|
||||||
|
|
||||||
|
wheel_caster(
|
||||||
|
commands,
|
||||||
|
FRONT_ATTACH,
|
||||||
|
Dir3::new_unchecked(front_rake),
|
||||||
|
body,
|
||||||
|
REST_DISTANCE,
|
||||||
|
CyberWheel::Front,
|
||||||
|
);
|
||||||
|
wheel_mesh(
|
||||||
|
commands,
|
||||||
|
&mut meshes,
|
||||||
|
&mut materials,
|
||||||
|
front_wheel_pos,
|
||||||
|
mesh.clone(),
|
||||||
|
WheelConfig::new(
|
||||||
|
FRONT_ATTACH,
|
||||||
|
REST_DISTANCE,
|
||||||
|
SPRING_CONSTANT,
|
||||||
|
DAMPING_CONSTANT,
|
||||||
|
FRICTION_COEFF,
|
||||||
|
WHEEL_RADIUS,
|
||||||
|
),
|
||||||
|
CyberWheel::Front,
|
||||||
|
);
|
||||||
|
|
||||||
let rear_rake = Vec3::new(0.0, -1.0, 0.9).normalize();
|
let rear_rake = Vec3::new(0.0, -1.0, 0.9).normalize();
|
||||||
let rear_wheel_pos = REAR_ATTACH + (rear_rake * REST_DISTANCE);
|
let rear_wheel_pos = REAR_ATTACH + (rear_rake * REST_DISTANCE);
|
||||||
|
|
||||||
children!(
|
wheel_caster(
|
||||||
|
commands,
|
||||||
|
REAR_ATTACH,
|
||||||
|
Dir3::new_unchecked(rear_rake),
|
||||||
body,
|
body,
|
||||||
wheel_caster(
|
REST_DISTANCE,
|
||||||
parent,
|
CyberWheel::Rear,
|
||||||
FRONT_ATTACH,
|
);
|
||||||
Dir3::new_unchecked(front_rake),
|
wheel_mesh(
|
||||||
REST_DISTANCE,
|
commands,
|
||||||
CyberWheel::Front,
|
&mut meshes,
|
||||||
),
|
&mut materials,
|
||||||
wheel_mesh(
|
rear_wheel_pos,
|
||||||
mesh.clone(),
|
mesh,
|
||||||
material.clone(),
|
WheelConfig::new(
|
||||||
front_wheel_pos,
|
|
||||||
WheelConfig::new(
|
|
||||||
FRONT_ATTACH,
|
|
||||||
REST_DISTANCE,
|
|
||||||
SPRING_CONSTANT,
|
|
||||||
DAMPING_CONSTANT,
|
|
||||||
FRICTION_COEFF,
|
|
||||||
WHEEL_RADIUS,
|
|
||||||
),
|
|
||||||
CyberWheel::Front,
|
|
||||||
),
|
|
||||||
wheel_caster(
|
|
||||||
parent,
|
|
||||||
REAR_ATTACH,
|
REAR_ATTACH,
|
||||||
Dir3::new_unchecked(rear_rake),
|
|
||||||
REST_DISTANCE,
|
REST_DISTANCE,
|
||||||
CyberWheel::Rear,
|
SPRING_CONSTANT,
|
||||||
|
DAMPING_CONSTANT,
|
||||||
|
FRICTION_COEFF,
|
||||||
|
WHEEL_RADIUS,
|
||||||
),
|
),
|
||||||
wheel_mesh(
|
CyberWheel::Rear,
|
||||||
mesh,
|
);
|
||||||
material,
|
|
||||||
rear_wheel_pos,
|
|
||||||
WheelConfig::new(
|
|
||||||
REAR_ATTACH,
|
|
||||||
REST_DISTANCE,
|
|
||||||
SPRING_CONSTANT,
|
|
||||||
DAMPING_CONSTANT,
|
|
||||||
FRICTION_COEFF,
|
|
||||||
WHEEL_RADIUS,
|
|
||||||
),
|
|
||||||
CyberWheel::Rear,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
|
@ -198,27 +182,37 @@ fn spawn_children(
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
|
|
||||||
fn wheel_caster(
|
fn wheel_caster(
|
||||||
parent: Entity,
|
commands: &mut ChildBuilder,
|
||||||
origin: Vec3,
|
origin: Vec3,
|
||||||
direction: Dir3,
|
direction: Dir3,
|
||||||
|
parent: Entity,
|
||||||
rest_dist: Scalar,
|
rest_dist: Scalar,
|
||||||
wheel: CyberWheel,
|
wheel: CyberWheel,
|
||||||
) -> impl Bundle {
|
) {
|
||||||
let caster = RayCaster::new(origin, direction)
|
let caster = RayCaster::new(origin, direction)
|
||||||
.with_max_distance(rest_dist)
|
.with_max_distance(rest_dist)
|
||||||
.with_max_hits(1)
|
.with_max_hits(1)
|
||||||
.with_query_filter(SpatialQueryFilter::from_excluded_entities([parent]));
|
.with_query_filter(SpatialQueryFilter::from_excluded_entities([parent]));
|
||||||
|
|
||||||
(caster, wheel)
|
commands.spawn((caster, wheel));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wheel_mesh(
|
fn wheel_mesh(
|
||||||
mesh: Handle<Mesh>,
|
commands: &mut ChildBuilder,
|
||||||
material: Handle<StandardMaterial>,
|
meshes: &mut ResMut<Assets<Mesh>>,
|
||||||
|
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||||
position: Vec3,
|
position: Vec3,
|
||||||
|
tire_mesh: Mesh,
|
||||||
config: WheelConfig,
|
config: WheelConfig,
|
||||||
wheel: CyberWheel,
|
wheel: CyberWheel,
|
||||||
) -> impl Bundle {
|
) {
|
||||||
|
let wheel_material = &StandardMaterial {
|
||||||
|
base_color: Color::srgb(0.01, 0.01, 0.01),
|
||||||
|
alpha_mode: AlphaMode::Opaque,
|
||||||
|
perceptual_roughness: 0.5,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
let xform = Transform::from_translation(position);
|
let xform = Transform::from_translation(position);
|
||||||
|
|
||||||
let name = match wheel {
|
let name = match wheel {
|
||||||
|
@ -226,18 +220,18 @@ fn wheel_mesh(
|
||||||
CyberWheel::Rear => "rear tire",
|
CyberWheel::Rear => "rear tire",
|
||||||
};
|
};
|
||||||
|
|
||||||
(
|
commands.spawn((
|
||||||
Name::new(name),
|
Name::new(name),
|
||||||
config,
|
config,
|
||||||
Mesh3d(mesh),
|
Mesh3d(meshes.add(tire_mesh)),
|
||||||
MeshMaterial3d(material),
|
MeshMaterial3d(materials.add(wheel_material.clone())),
|
||||||
xform,
|
xform,
|
||||||
Collider::sphere(WHEEL_RADIUS),
|
Collider::sphere(WHEEL_RADIUS),
|
||||||
ColliderDensity(0.5),
|
ColliderDensity(0.5),
|
||||||
CollisionLayers::NONE,
|
CollisionLayers::NONE,
|
||||||
TransformInterpolation,
|
TransformInterpolation,
|
||||||
wheel,
|
wheel,
|
||||||
)
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CyberBikePlugin;
|
pub struct CyberBikePlugin;
|
||||||
|
|
|
@ -1,10 +1,4 @@
|
||||||
use bevy::{
|
use bevy::{prelude::*, utils::HashSet};
|
||||||
platform::collections::HashSet,
|
|
||||||
prelude::{
|
|
||||||
App, ButtonInput, Camera3d, Commands, Component, KeyCode, Plugin, Quat, Query, Res, ResMut,
|
|
||||||
Resource, Result, Startup, Transform, Update, Vec, Vec3, With, Without,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::bike::CyberBikeBody;
|
use crate::bike::CyberBikeBody;
|
||||||
|
|
||||||
|
@ -71,8 +65,8 @@ fn follow_bike(
|
||||||
mut camera: Query<&mut Transform, (With<CyberCameras>, Without<CyberBikeBody>)>,
|
mut camera: Query<&mut Transform, (With<CyberCameras>, Without<CyberBikeBody>)>,
|
||||||
bike: Query<&Transform, (With<CyberBikeBody>, Without<CyberCameras>)>,
|
bike: Query<&Transform, (With<CyberBikeBody>, Without<CyberCameras>)>,
|
||||||
offset: Res<DebugCamOffset>,
|
offset: Res<DebugCamOffset>,
|
||||||
) -> Result {
|
) {
|
||||||
let bike_xform = *bike.single()?;
|
let bike_xform = *bike.single();
|
||||||
let up = Vec3::Y;
|
let up = Vec3::Y;
|
||||||
|
|
||||||
let mut ncx = Transform::from_translation(bike_xform.translation);
|
let mut ncx = Transform::from_translation(bike_xform.translation);
|
||||||
|
@ -81,10 +75,8 @@ fn follow_bike(
|
||||||
ncx.translation += ncx.up() * offset.alt;
|
ncx.translation += ncx.up() * offset.alt;
|
||||||
ncx.look_at(bike_xform.translation, up);
|
ncx.look_at(bike_xform.translation, up);
|
||||||
|
|
||||||
let mut cam_xform = camera.single_mut()?;
|
let mut cam_xform = camera.single_mut();
|
||||||
*cam_xform = ncx;
|
*cam_xform = ncx;
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CamPlug;
|
pub struct CamPlug;
|
||||||
|
|
49
src/main.rs
49
src/main.rs
|
@ -3,15 +3,15 @@ use avian3d::prelude::{
|
||||||
};
|
};
|
||||||
use bevy::{
|
use bevy::{
|
||||||
color::{palettes::css::SILVER, Alpha},
|
color::{palettes::css::SILVER, Alpha},
|
||||||
ecs::error::{warn, GLOBAL_ERROR_HANDLER},
|
|
||||||
pbr::MeshMaterial3d,
|
pbr::MeshMaterial3d,
|
||||||
prelude::{
|
prelude::{
|
||||||
children, default, App, AppGizmoBuilder, Assets, ButtonInput, Color, Commands,
|
default, App, AppGizmoBuilder, Assets, BuildChildren, ButtonInput, ChildBuild, Color,
|
||||||
DefaultPlugins, Entity, GizmoConfig, KeyCode, Mesh, Mesh3d, Meshable, Plane3d, PointLight,
|
Commands, DefaultPlugins, Entity, GizmoConfig, KeyCode, Mesh, Mesh3d, Meshable, Plane3d,
|
||||||
Query, Res, ResMut, SpawnRelated, Srgba, StandardMaterial, Startup, Transform, Update,
|
PointLight, Query, Res, ResMut, Srgba, StandardMaterial, Startup, Transform, Update, Vec3,
|
||||||
Vec3, Visibility, Window,
|
Visibility, Window,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
||||||
|
|
||||||
mod bike;
|
mod bike;
|
||||||
mod camera;
|
mod camera;
|
||||||
|
@ -24,10 +24,6 @@ use input::CyberInputPlugin;
|
||||||
use physics::CyberPhysicsPlugin;
|
use physics::CyberPhysicsPlugin;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
GLOBAL_ERROR_HANDLER
|
|
||||||
.set(warn)
|
|
||||||
.expect("The error handler can only be set once, globally.");
|
|
||||||
// initialize Bevy App here
|
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
app.add_plugins((
|
app.add_plugins((
|
||||||
DefaultPlugins,
|
DefaultPlugins,
|
||||||
|
@ -35,6 +31,7 @@ fn main() {
|
||||||
CyberBikePlugin,
|
CyberBikePlugin,
|
||||||
CyberInputPlugin,
|
CyberInputPlugin,
|
||||||
CyberPhysicsPlugin,
|
CyberPhysicsPlugin,
|
||||||
|
WorldInspectorPlugin::new(),
|
||||||
))
|
))
|
||||||
.insert_gizmo_config(
|
.insert_gizmo_config(
|
||||||
PhysicsGizmos {
|
PhysicsGizmos {
|
||||||
|
@ -59,21 +56,25 @@ fn ground_and_light(
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
) {
|
) {
|
||||||
let spatial_bundle = (Transform::default(), Visibility::default());
|
let spatial_bundle = (Transform::default(), Visibility::default());
|
||||||
commands.spawn((
|
commands
|
||||||
spatial_bundle,
|
.spawn((
|
||||||
RigidBody::Static,
|
spatial_bundle,
|
||||||
Collider::cuboid(500.0, 5., 500.0),
|
RigidBody::Static,
|
||||||
CollisionMargin(0.1),
|
Collider::cuboid(500.0, 5., 500.0),
|
||||||
ColliderDensity(1000.0),
|
CollisionMargin(0.1),
|
||||||
CollisionLayers::ALL,
|
ColliderDensity(1000.0),
|
||||||
Friction::new(0.9),
|
CollisionLayers::ALL,
|
||||||
children![(
|
Friction::new(0.9),
|
||||||
Mesh3d(meshes.add(Plane3d::default().mesh().size(500.0, 500.0))),
|
))
|
||||||
MeshMaterial3d(materials.add(Color::from(SILVER).with_alpha(0.7))),
|
.with_children(|p| {
|
||||||
Transform::from_xyz(0.0, 2.5, 0.0),
|
let bundle = (
|
||||||
Visibility::Visible,
|
Mesh3d(meshes.add(Plane3d::default().mesh().size(500.0, 500.0))),
|
||||||
)],
|
MeshMaterial3d(materials.add(Color::from(SILVER).with_alpha(0.7))),
|
||||||
));
|
Transform::from_xyz(0.0, 2.5, 0.0),
|
||||||
|
Visibility::Visible,
|
||||||
|
);
|
||||||
|
p.spawn(bundle);
|
||||||
|
});
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
PointLight {
|
PointLight {
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
use avian3d::{math::Scalar, prelude::*};
|
use avian3d::{math::Scalar, prelude::*};
|
||||||
use bevy::prelude::{
|
use bevy::prelude::*;
|
||||||
App, Component, FixedUpdate, IntoScheduleConfigs, Plugin, ResMut, Resource, Startup, Update,
|
|
||||||
};
|
|
||||||
|
|
||||||
const DRAG_COEFF: Scalar = 0.00001;
|
const DRAG_COEFF: Scalar = 0.00001;
|
||||||
|
|
||||||
#[derive(Resource, Default, Debug)]
|
#[derive(Resource, Default, Debug, Reflect)]
|
||||||
|
#[reflect(Resource)]
|
||||||
struct CyberLean {
|
struct CyberLean {
|
||||||
pub lean: f32,
|
pub lean: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Resource)]
|
#[derive(Debug, Resource, Reflect)]
|
||||||
|
#[reflect(Resource)]
|
||||||
pub struct CatControllerSettings {
|
pub struct CatControllerSettings {
|
||||||
pub kp: f32,
|
pub kp: f32,
|
||||||
pub kd: f32,
|
pub kd: f32,
|
||||||
|
@ -27,7 +27,7 @@ impl Default for CatControllerSettings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
#[derive(Component, Debug, Clone, Copy, Reflect)]
|
||||||
pub struct CatControllerState {
|
pub struct CatControllerState {
|
||||||
pub roll_integral: f32,
|
pub roll_integral: f32,
|
||||||
pub roll_prev: f32,
|
pub roll_prev: f32,
|
||||||
|
@ -58,13 +58,7 @@ mod systems {
|
||||||
use std::f32::consts::{FRAC_PI_3, FRAC_PI_4};
|
use std::f32::consts::{FRAC_PI_3, FRAC_PI_4};
|
||||||
|
|
||||||
use avian3d::prelude::*;
|
use avian3d::prelude::*;
|
||||||
use bevy::{
|
use bevy::prelude::*;
|
||||||
ecs::error::BevyError,
|
|
||||||
prelude::{
|
|
||||||
ButtonInput, Color, Gizmos, GlobalTransform, KeyCode, Quat, Query, Res, ResMut, Result,
|
|
||||||
Time, Transform, Vec, Vec3, With, Without,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{CatControllerSettings, CatControllerState, CyberLean, DRAG_COEFF};
|
use super::{CatControllerSettings, CatControllerState, CyberLean, DRAG_COEFF};
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -89,12 +83,12 @@ mod systems {
|
||||||
input: Res<InputState>,
|
input: Res<InputState>,
|
||||||
gravity: Res<Gravity>,
|
gravity: Res<Gravity>,
|
||||||
mut lean: ResMut<CyberLean>,
|
mut lean: ResMut<CyberLean>,
|
||||||
) -> Result {
|
) {
|
||||||
let mut wheels = wheels.iter();
|
let mut wheels = wheels.iter();
|
||||||
let w1 = wheels.next().ok_or(BevyError::from("oops"))?;
|
let w1 = wheels.next().unwrap();
|
||||||
let w2 = wheels.next().ok_or(BevyError::from("oops"))?;
|
let w2 = wheels.next().unwrap();
|
||||||
let base = (w1.translation() - w2.translation()).length().abs();
|
let base = (w1.translation() - w2.translation()).length().abs();
|
||||||
let (velocity, xform) = bike_state.single()?;
|
let (velocity, xform) = bike_state.single();
|
||||||
let vel = velocity.dot(*xform.forward());
|
let vel = velocity.dot(*xform.forward());
|
||||||
let v_squared = vel.powi(2);
|
let v_squared = vel.powi(2);
|
||||||
let steering_angle = yaw_to_angle(input.yaw);
|
let steering_angle = yaw_to_angle(input.yaw);
|
||||||
|
@ -108,7 +102,6 @@ mod systems {
|
||||||
} else {
|
} else {
|
||||||
//lean.lean = 0.0;
|
//lean.lean = 0.0;
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn apply_lean(
|
pub(super) fn apply_lean(
|
||||||
|
@ -118,8 +111,8 @@ mod systems {
|
||||||
settings: Res<CatControllerSettings>,
|
settings: Res<CatControllerSettings>,
|
||||||
lean: Res<CyberLean>,
|
lean: Res<CyberLean>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
) -> Result {
|
) {
|
||||||
let (xform, mut force, mut control_vars) = bike_query.single_mut()?;
|
let (xform, mut force, mut control_vars) = bike_query.single_mut();
|
||||||
|
|
||||||
let mut factor = 1.0;
|
let mut factor = 1.0;
|
||||||
for wheel in wheels.iter() {
|
for wheel in wheels.iter() {
|
||||||
|
@ -170,7 +163,6 @@ mod systems {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn yaw_to_angle(yaw: f32) -> f32 {
|
fn yaw_to_angle(yaw: f32) -> f32 {
|
||||||
|
@ -191,7 +183,7 @@ mod systems {
|
||||||
caster_query: Query<(&RayCaster, &RayHits, &CyberWheel)>,
|
caster_query: Query<(&RayCaster, &RayHits, &CyberWheel)>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
) -> Result {
|
) {
|
||||||
let dt = time.delta().as_secs_f32();
|
let dt = time.delta().as_secs_f32();
|
||||||
|
|
||||||
let mut front_caster = &RayCaster::default();
|
let mut front_caster = &RayCaster::default();
|
||||||
|
@ -213,7 +205,7 @@ mod systems {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let (bike_xform, mut bike_forces) = bike_body_query.single_mut()?;
|
let (bike_xform, mut bike_forces) = bike_body_query.single_mut();
|
||||||
|
|
||||||
for (mut xform, mut state, config, wheel) in wheel_mesh_query.iter_mut() {
|
for (mut xform, mut state, config, wheel) in wheel_mesh_query.iter_mut() {
|
||||||
let (caster, hits) = match wheel {
|
let (caster, hits) = match wheel {
|
||||||
|
@ -255,7 +247,6 @@ mod systems {
|
||||||
state.reset();
|
state.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn steering(
|
pub(super) fn steering(
|
||||||
|
@ -272,8 +263,12 @@ mod systems {
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
input: Res<InputState>,
|
input: Res<InputState>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
) -> Result {
|
) {
|
||||||
let (bike_xform, bike_vel, mut bike_force, bike_body) = bike_query.single_mut()?;
|
let Ok((bike_xform, bike_vel, mut bike_force, bike_body)) = bike_query.get_single_mut()
|
||||||
|
else {
|
||||||
|
bevy::log::warn!("No entity for bike_query.");
|
||||||
|
return;
|
||||||
|
};
|
||||||
let bike_vel = bike_vel.0;
|
let bike_vel = bike_vel.0;
|
||||||
|
|
||||||
let dt = time.delta().as_secs_f32();
|
let dt = time.delta().as_secs_f32();
|
||||||
|
@ -331,8 +326,6 @@ mod systems {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn drag(
|
pub(super) fn drag(
|
||||||
|
@ -342,8 +335,8 @@ mod systems {
|
||||||
>,
|
>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) -> Result {
|
) {
|
||||||
let (xform, vel, mut force) = bike_query.single_mut()?;
|
let (xform, vel, mut force) = bike_query.single_mut();
|
||||||
|
|
||||||
let dt = time.delta_secs();
|
let dt = time.delta_secs();
|
||||||
|
|
||||||
|
@ -366,8 +359,6 @@ mod systems {
|
||||||
xform.translation + drag * 10.,
|
xform.translation + drag * 10.,
|
||||||
Color::linear_rgb(1.0, 0.0, 0.0),
|
Color::linear_rgb(1.0, 0.0, 0.0),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn tweak(
|
pub(super) fn tweak(
|
||||||
|
@ -415,7 +406,10 @@ pub struct CyberPhysicsPlugin;
|
||||||
impl Plugin for CyberPhysicsPlugin {
|
impl Plugin for CyberPhysicsPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.init_resource::<CatControllerSettings>()
|
app.init_resource::<CatControllerSettings>()
|
||||||
|
.register_type::<CatControllerSettings>()
|
||||||
|
.register_type::<CatControllerState>()
|
||||||
.init_resource::<CyberLean>()
|
.init_resource::<CyberLean>()
|
||||||
|
.register_type::<CyberLean>()
|
||||||
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
||||||
.insert_resource(SubstepCount(12))
|
.insert_resource(SubstepCount(12))
|
||||||
.add_systems(Startup, |mut gravity: ResMut<Gravity>| {
|
.add_systems(Startup, |mut gravity: ResMut<Gravity>| {
|
||||||
|
|
Loading…
Reference in a new issue