re-org bike mod, don't spawn rear wheel
This commit is contained in:
parent
d66a26d63c
commit
c7fd8a57d7
2 changed files with 123 additions and 7 deletions
121
src/bike.rs
121
src/bike.rs
|
@ -13,11 +13,11 @@ fn spawn_bike(
|
|||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
let xform = Transform::from_xyz(0.0, 4.0, 0.0);
|
||||
let xform = Transform::from_xyz(4.0, 4.0, 0.0);
|
||||
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 _bike = commands
|
||||
let bike = commands
|
||||
.spawn(SpatialBundle::from_transform(xform))
|
||||
.insert((
|
||||
RigidBody::Dynamic,
|
||||
|
@ -47,6 +47,123 @@ fn spawn_bike(
|
|||
builder.spawn(pbr_bundle);
|
||||
})
|
||||
.id();
|
||||
|
||||
spawn_wheels(commands, meshes, materials, xform, bike);
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct CyberWheel;
|
||||
|
||||
// marker for front suspension joint
|
||||
#[derive(Component)]
|
||||
pub struct Steering;
|
||||
#[derive(Component)]
|
||||
pub struct FrontHub;
|
||||
|
||||
// marker for rear suspension joint
|
||||
#[derive(Component)]
|
||||
pub struct Rearing;
|
||||
#[derive(Component)]
|
||||
pub struct RearHub;
|
||||
|
||||
fn spawn_wheels(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
xform: Transform,
|
||||
body: Entity,
|
||||
) {
|
||||
let rake_vec = Vec3::new(0.0, -1.0, 0.57).normalize(); // about 30 degrees
|
||||
let front_pos = xform.translation + *xform.forward() + rake_vec;
|
||||
|
||||
let wheel_mesh: Mesh = Torus::new(0.30, 0.40).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 collider = Collider::convex_decomposition_from_mesh(&wheel_mesh).unwrap();
|
||||
|
||||
let front_hub = commands
|
||||
.spawn((
|
||||
FrontHub,
|
||||
SpatialBundle::from_transform(Transform::from_translation(front_pos)),
|
||||
RigidBody::Dynamic,
|
||||
MassPropertiesBundle::new_computed(&Collider::sphere(0.01), 1.0),
|
||||
))
|
||||
.id();
|
||||
|
||||
let mut tire_rot = Transform::default();
|
||||
tire_rot.rotate_z(FRAC_PI_2);
|
||||
|
||||
let front_tire = commands
|
||||
.spawn((
|
||||
SpatialBundle::from_transform(tire_rot),
|
||||
CyberWheel,
|
||||
RigidBody::Dynamic,
|
||||
collider.clone(),
|
||||
ColliderDensity(0.05),
|
||||
CollisionLayers::from_bits(2, 2),
|
||||
ExternalTorque::ZERO.with_persistence(false),
|
||||
))
|
||||
.with_children(|b| {
|
||||
b.spawn(PbrBundle {
|
||||
mesh: meshes.add(wheel_mesh.clone()),
|
||||
material: materials.add(wheel_material.clone()),
|
||||
// transform: Transform::from_translation(front_pos)
|
||||
// .with_rotation(Quat::from_rotation_z(FRAC_PI_2)),
|
||||
..Default::default()
|
||||
});
|
||||
})
|
||||
.id();
|
||||
|
||||
// connect hubs and tires to make wheels
|
||||
commands.spawn(RevoluteJoint::new(front_hub, front_tire).with_aligned_axis(Vec3::X));
|
||||
|
||||
// suspension joints connect the hubs and the body
|
||||
commands.spawn((
|
||||
Steering,
|
||||
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec),
|
||||
));
|
||||
|
||||
/*
|
||||
let rear_pos = *xform.back() + xform.translation + rake_vec.y;
|
||||
let rear_hub = commands
|
||||
.spawn((
|
||||
RearHub,
|
||||
SpatialBundle::from_transform(Transform::from_translation(front_pos)),
|
||||
RigidBody::Dynamic,
|
||||
MassPropertiesBundle::new_computed(&Collider::sphere(0.01), 1.0),
|
||||
))
|
||||
.id();
|
||||
|
||||
let rear_tire = commands
|
||||
.spawn((
|
||||
CyberWheel,
|
||||
RigidBody::Dynamic,
|
||||
collider,
|
||||
ColliderDensity(0.05),
|
||||
CollisionLayers::from_bits(2, 2),
|
||||
PbrBundle {
|
||||
mesh: meshes.add(wheel_mesh),
|
||||
material: materials.add(wheel_material),
|
||||
transform: Transform::from_translation(rear_pos)
|
||||
.with_rotation(Quat::from_rotation_z(FRAC_PI_2)),
|
||||
..Default::default()
|
||||
},
|
||||
ExternalTorque::ZERO.with_persistence(false),
|
||||
))
|
||||
.id();
|
||||
|
||||
commands.spawn(RevoluteJoint::new(rear_hub,
|
||||
rear_tire).with_aligned_axis(Vec3::X));
|
||||
|
||||
commands.spawn((
|
||||
Rearing,
|
||||
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + xform.translation),
|
||||
));
|
||||
*/
|
||||
}
|
||||
|
||||
pub struct CyberBikePlugin;
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::CyberBikePlugin;
|
||||
|
||||
#[derive(Resource, Default, Debug, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
struct CyberLean {
|
||||
|
@ -21,13 +19,13 @@ impl Default for CatControllerSettings {
|
|||
fn default() -> Self {
|
||||
Self {
|
||||
kp: 10.0,
|
||||
kd: 1.0,
|
||||
ki: 0.1,
|
||||
kd: 1.2,
|
||||
ki: 0.2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Debug, Clone, Copy)]
|
||||
#[derive(Component, Debug, Clone, Copy, Reflect)]
|
||||
pub struct CatControllerState {
|
||||
pub roll_integral: f32,
|
||||
pub roll_prev: f32,
|
||||
|
@ -158,6 +156,7 @@ impl Plugin for CyberPhysicsPlugin {
|
|||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<CatControllerSettings>()
|
||||
.register_type::<CatControllerSettings>()
|
||||
.register_type::<CatControllerState>()
|
||||
.init_resource::<CyberLean>()
|
||||
.register_type::<CyberLean>()
|
||||
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
|
||||
|
|
Loading…
Reference in a new issue