Compare commits

..

5 commits
torii ... main

Author SHA1 Message Date
Joe Ardent
2358d53b7a done with it for now, bringing back into cyber rider 2024-07-25 14:51:08 -07:00
Joe Ardent
66a05ea8ef more tweak 2024-07-25 11:42:25 -07:00
Joe Ardent
9d82ddbf09 increase masses and control params 2024-07-25 11:22:11 -07:00
Joe Ardent
68449f2b6e checkpoint, mostly working 2024-07-24 22:08:16 -07:00
Joe Ardent
e4c491768c re-org bike mod, optimize external deps for fast startup 2024-07-24 16:02:54 -07:00
4 changed files with 134 additions and 84 deletions

View file

@ -10,3 +10,6 @@ bevy-inspector-egui = "0.25.1"
[features]
no-mesh = []
[profile.dev.package."*"]
opt-level = 3

View file

@ -8,6 +8,21 @@ use crate::physics::CatControllerState;
#[derive(Component)]
pub struct CyberBikeBody;
#[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_bike(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
@ -22,13 +37,14 @@ fn spawn_bike(
let bike = commands
.spawn(SpatialBundle::from_transform(xform))
.insert((
Name::new("body"),
RigidBody::Dynamic,
body_collider,
CollisionLayers::from_bits(1, 1),
SleepingDisabled,
CyberBikeBody,
CatControllerState::default(),
ColliderDensity(0.12),
ColliderDensity(20.0),
LinearDamping(0.1),
AngularDamping(2.0),
LinearVelocity::ZERO,
@ -53,28 +69,56 @@ fn spawn_bike(
spawn_wheels(commands, meshes, materials, xform, bike);
}
#[derive(Component)]
pub struct CyberWheel;
fn spawn_wheels(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
xform: Transform,
body: Entity,
) {
let front_rake = Vec3::new(0.0, -1.0, -0.57).normalize(); // about 30 degrees
let front_pos = xform.translation + front_rake;
// marker for front suspension joint
#[derive(Component)]
pub struct Steering;
#[derive(Component)]
pub struct FrontHub;
let (mesh, collider) = gen_tire();
// marker for rear suspension joint
#[derive(Component)]
pub struct Rearing;
#[derive(Component)]
pub struct RearHub;
let front_hub = wheels_helper(
&mut commands,
&mut meshes,
&mut materials,
front_pos,
mesh.clone(),
collider.clone(),
);
commands.entity(front_hub).insert(FrontHub);
commands.spawn((
Steering,
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + front_rake),
));
fn helper(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<StandardMaterial>>,
position: Vec3,
) -> Entity {
let mut tire_mesh: Mesh = Torus::new(0.30, 0.40).into();
let rear_rake = Vec3::new(0.0, -1.0, 0.57).normalize();
let rear_pos = xform.translation + rear_rake;
let rear_hub = wheels_helper(
&mut commands,
&mut meshes,
&mut materials,
rear_pos,
mesh,
collider,
);
commands.entity(rear_hub).insert(RearHub);
commands.spawn((
Rearing,
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rear_rake),
));
}
//-************************************************************************
// helper fns for the wheels
//-************************************************************************
fn gen_tire() -> (Mesh, Collider) {
let mut tire_mesh: Mesh = Torus::new(0.25, 0.40).into();
let tire_verts = tire_mesh
.attribute(Mesh::ATTRIBUTE_POSITION)
.unwrap()
@ -91,76 +135,71 @@ fn helper(
.collect::<Vec<[f32; 3]>>();
tire_mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION);
tire_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, tire_verts);
let wheel_material = StandardMaterial {
let collider = Collider::convex_hull_from_mesh(&tire_mesh).unwrap();
(tire_mesh, collider)
}
fn wheels_helper(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<StandardMaterial>>,
position: Vec3,
tire_mesh: Mesh,
collider: Collider,
) -> Entity {
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(&tire_mesh).unwrap();
let mut tire = None;
let tref = &mut tire;
let xform = Transform::from_translation(position);
let hub_mesh: Mesh = Sphere::new(0.1).into();
let hub = commands
.spawn((
SpatialBundle::from_transform(xform),
Name::new("hub"),
RigidBody::Dynamic,
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 0.01),
))
.with_children(move |parent| {
let mesh: Mesh = Sphere::new(0.1).into();
parent.spawn(PbrBundle {
mesh: meshes.add(mesh),
MassPropertiesBundle::new_computed(&Collider::sphere(0.1), 200.0),
PbrBundle {
mesh: meshes.add(hub_mesh),
material: materials.add(wheel_material.clone()),
transform: xform,
..Default::default()
});
let tire_id = parent
.spawn((
CyberWheel,
RigidBody::Dynamic,
collider.clone(),
ColliderDensity(0.05),
CollisionLayers::from_bits(2, 2),
ExternalTorque::ZERO.with_persistence(false),
))
.id();
let _ = tref.insert(tire_id);
})
},
))
.id();
let tire = commands
.spawn((
Name::new("tire"),
PbrBundle {
mesh: meshes.add(tire_mesh),
material: materials.add(wheel_material.clone()),
transform: xform,
..Default::default()
},
CyberWheel,
RigidBody::Dynamic,
collider,
Friction::new(0.9).with_dynamic_coefficient(0.6),
Restitution::new(0.1),
ColliderDensity(30.0),
CollisionLayers::from_bits(2, 2),
ExternalTorque::ZERO.with_persistence(false),
CollisionMargin(0.05),
SweptCcd::NON_LINEAR,
))
.id();
// connect hubs and tires to make wheels
commands.spawn(RevoluteJoint::new(hub, tire.unwrap()).with_aligned_axis(Vec3::X));
commands.spawn(RevoluteJoint::new(hub, tire).with_aligned_axis(Vec3::X));
hub
}
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 + rake_vec;
let front_hub = helper(&mut commands, &mut meshes, &mut materials, front_pos);
commands.entity(front_hub).insert(FrontHub);
commands.spawn((
Steering,
FixedJoint::new(front_hub, body).with_local_anchor_2(*xform.forward() + rake_vec),
));
let rear_pos = xform.translation + Vec3::new(0.0, -1.0, 0.57).normalize();
let rear_hub = helper(&mut commands, &mut meshes, &mut materials, rear_pos);
commands.entity(rear_hub).insert(RearHub);
commands.spawn((
Rearing,
FixedJoint::new(rear_hub, body).with_local_anchor_2(*xform.back() + rake_vec.y),
));
}
pub struct CyberBikePlugin;
impl Plugin for CyberBikePlugin {

View file

@ -41,16 +41,23 @@ fn ground_and_light(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
RigidBody::Static,
Collider::cuboid(50.0, 0.5, 50.0),
PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
material: materials.add(Color::from(SILVER)),
transform: Transform::from_xyz(0.0, -3., 0.0),
..default()
},
));
commands
.spawn((
SpatialBundle::default(),
RigidBody::Static,
Collider::cuboid(50.0, 0.5, 50.0),
CollisionMargin(0.1),
ColliderDensity(1000.0),
Friction::new(0.9),
))
.with_children(|p| {
p.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
material: materials.add(Color::from(SILVER)),
transform: Transform::from_xyz(0.0, 0.4, 0.0),
..default()
});
});
commands.spawn(PointLightBundle {
point_light: PointLight {

View file

@ -18,9 +18,9 @@ pub struct CatControllerSettings {
impl Default for CatControllerSettings {
fn default() -> Self {
Self {
kp: 10.0,
kd: 1.2,
ki: 0.2,
kp: 1200.0,
kd: 10.0,
ki: 50.0,
}
}
}
@ -174,6 +174,7 @@ impl Plugin for CyberPhysicsPlugin {
.init_resource::<CyberLean>()
.register_type::<CyberLean>()
.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()))
.insert_resource(SubstepCount(12))
.add_systems(Update, (apply_lean, calculate_lean));
}
}