add wheels maybe
This commit is contained in:
parent
3e9b079b58
commit
ff64bbeff3
4 changed files with 118 additions and 24 deletions
|
@ -28,7 +28,7 @@ features = [
|
|||
]
|
||||
|
||||
[dependencies.bevy_rapier3d]
|
||||
features = ["simd-nightly"]
|
||||
features = ["simd-nightly", "debug-render-3d"]
|
||||
version = "0.20"
|
||||
|
||||
# Maybe also enable only a small amount of optimization for our code:
|
||||
|
|
|
@ -28,7 +28,7 @@ impl Default for MovementSettings {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Resource, Reflect)]
|
||||
#[derive(Debug, Resource, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
struct CatControllerSettings {
|
||||
pub kp: f32,
|
||||
|
@ -36,6 +36,16 @@ struct CatControllerSettings {
|
|||
pub kws: f32,
|
||||
}
|
||||
|
||||
impl Default for CatControllerSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
kp: 10.0,
|
||||
kd: 4.0,
|
||||
kws: 0.85,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn zero_gravity(mut config: ResMut<RapierConfiguration>) {
|
||||
config.gravity = Vec3::ZERO;
|
||||
}
|
||||
|
|
122
src/bike.rs
122
src/bike.rs
|
@ -1,6 +1,6 @@
|
|||
use std::fmt::Debug;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy::prelude::{shape::Capsule as Tire, *};
|
||||
use bevy_rapier3d::{
|
||||
geometry::Group,
|
||||
prelude::{
|
||||
|
@ -14,6 +14,11 @@ use crate::planet::PLANET_RADIUS;
|
|||
|
||||
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 0.2;
|
||||
|
||||
type Meshterial<'a> = (
|
||||
ResMut<'a, Assets<Mesh>>,
|
||||
ResMut<'a, Assets<StandardMaterial>>,
|
||||
);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct CyberBikeBody;
|
||||
|
||||
|
@ -23,6 +28,9 @@ pub struct CyberBikeCollider;
|
|||
#[derive(Component, Debug)]
|
||||
pub struct CyberBikeModel;
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct CyberWheel;
|
||||
|
||||
#[derive(Component, Debug, Default, Clone, Copy)]
|
||||
pub struct CyberBikeControl {
|
||||
pub error_sum: f32,
|
||||
|
@ -39,6 +47,7 @@ pub struct WheelConfig {
|
|||
pub limits: [f32; 2],
|
||||
pub stiffness: f32,
|
||||
pub damping: f32,
|
||||
pub radius: f32,
|
||||
}
|
||||
|
||||
impl Default for WheelConfig {
|
||||
|
@ -51,6 +60,7 @@ impl Default for WheelConfig {
|
|||
limits: [0.1, 1.0],
|
||||
stiffness: 10.0,
|
||||
damping: 0.7,
|
||||
radius: 0.3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +68,12 @@ impl Default for WheelConfig {
|
|||
const BIKE_BODY_COLLISION_GROUP: (Group, Group) = (Group::GROUP_1, Group::GROUP_1);
|
||||
const BIKE_WHEEL_COLLISION_GROUP: (Group, Group) = (Group::GROUP_10, Group::GROUP_10);
|
||||
|
||||
fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
fn spawn_cyberbike(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
wheel_conf: Res<WheelConfig>,
|
||||
mut meshterials: Meshterial,
|
||||
) {
|
||||
let xform = Transform::from_translation(Vec3::Y * SPAWN_ALTITUDE);
|
||||
//.with_rotation(Quat::from_axis_angle(Vec3::Z, -89.0f32.to_radians()));
|
||||
|
||||
|
@ -67,11 +82,10 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
linear_damping: 0.1,
|
||||
};
|
||||
let not_sleeping = Sleeping::disabled();
|
||||
|
||||
let ccd = Ccd { enabled: true };
|
||||
|
||||
let bcollider_shape =
|
||||
Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.7);
|
||||
Collider::capsule(Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, 0.0, 1.0), 0.50);
|
||||
|
||||
let friction = Friction {
|
||||
coefficient: 0.0,
|
||||
|
@ -127,21 +141,78 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
.id();
|
||||
|
||||
//return;
|
||||
let wheel_z_positions = vec![-1.0, 1.2, -1.0];
|
||||
let wheel_y = -1.0f32;
|
||||
re_tire(&mut commands, &xform, bike, &wheel_conf, &mut meshterials);
|
||||
}
|
||||
|
||||
fn re_tire(
|
||||
commands: &mut Commands,
|
||||
xform: &Transform,
|
||||
bike: Entity,
|
||||
conf: &WheelConfig,
|
||||
meshterials: &mut Meshterial,
|
||||
) {
|
||||
// re-set the collision group
|
||||
let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP;
|
||||
let wheels_collision_group = CollisionGroups::new(membership, filter);
|
||||
let wheel_y = conf.y;
|
||||
let wheel_rad = conf.radius;
|
||||
let stiffness = conf.stiffness;
|
||||
let not_sleeping = Sleeping::disabled();
|
||||
let ccd = Ccd { enabled: true };
|
||||
let limits = conf.limits;
|
||||
let (meshes, materials) = meshterials;
|
||||
|
||||
for (i, &wheel_z) in wheel_z_positions.iter().enumerate() {
|
||||
let (wheel_x, wheel_rad, stiffness) = match i {
|
||||
0 => (-1.1, 0.5, 2.0),
|
||||
2 => (1.1, 0.5, 2.0),
|
||||
1 => (0.0, 0.5, 1.8),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let tire = Tire {
|
||||
radius: wheel_rad,
|
||||
rings: 1,
|
||||
depth: 0.2,
|
||||
..Default::default()
|
||||
};
|
||||
let material = StandardMaterial {
|
||||
base_color: Color::Rgba {
|
||||
red: 0.01,
|
||||
green: 0.01,
|
||||
blue: 0.01,
|
||||
alpha: 1.0,
|
||||
},
|
||||
alpha_mode: AlphaMode::Opaque,
|
||||
perceptual_roughness: 0.5,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let pbr_bundle = PbrBundle {
|
||||
material: materials.add(material),
|
||||
mesh: meshes.add(Mesh::from(tire)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let mut wheel_poses = Vec::with_capacity(3);
|
||||
|
||||
// left front
|
||||
{
|
||||
let wheel_x = -conf.front_stance;
|
||||
let wheel_z = conf.front_forward;
|
||||
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
|
||||
wheel_poses.push(offset);
|
||||
}
|
||||
|
||||
// right front
|
||||
{
|
||||
let wheel_x = conf.front_stance;
|
||||
let wheel_z = conf.front_forward;
|
||||
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
|
||||
wheel_poses.push(offset);
|
||||
}
|
||||
|
||||
// rear
|
||||
{
|
||||
let wheel_x = -conf.front_stance;
|
||||
let wheel_z = conf.front_forward;
|
||||
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
|
||||
wheel_poses.push(offset);
|
||||
}
|
||||
|
||||
for offset in wheel_poses {
|
||||
let trans = xform.translation + offset;
|
||||
let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans);
|
||||
let wheel_damping = Damping {
|
||||
|
@ -154,13 +225,27 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
let damping = 0.3;
|
||||
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
||||
.local_anchor2(offset)
|
||||
.limits([-1.0, 0.9])
|
||||
.limits(limits)
|
||||
.motor_position(0.0, stiffness, damping);
|
||||
let joint = ImpulseJoint::new(bike, prismatic);
|
||||
|
||||
let _wheel_rb = commands
|
||||
let spatial_bundle = SpatialBundle {
|
||||
transform: wheel_pos_in_world,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let txform = wheel_pos_in_world.with_rotation(Quat::from_axis_angle(
|
||||
wheel_pos_in_world.forward(),
|
||||
90.0f32.to_radians(),
|
||||
));
|
||||
let tire_spundle = SpatialBundle {
|
||||
transform: txform,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
commands
|
||||
.spawn(RigidBody::Dynamic)
|
||||
.insert((wheel_pos_in_world, GlobalTransform::default()))
|
||||
.insert(spatial_bundle)
|
||||
.insert((
|
||||
wheel_collider,
|
||||
mass_props,
|
||||
|
@ -170,7 +255,10 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
joint,
|
||||
wheels_collision_group,
|
||||
))
|
||||
.id();
|
||||
.with_children(|wheel| {
|
||||
wheel.spawn(tire_spundle).insert(pbr_bundle.clone());
|
||||
})
|
||||
.insert(CyberWheel);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
use bevy::{
|
||||
prelude::{shape::Plane, *},
|
||||
render::mesh::Indices,
|
||||
};
|
||||
use bevy::prelude::{shape::Plane, *};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
use wgpu::PrimitiveTopology;
|
||||
|
||||
use crate::Label;
|
||||
|
||||
|
|
Loading…
Reference in a new issue