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]
|
[dependencies.bevy_rapier3d]
|
||||||
features = ["simd-nightly"]
|
features = ["simd-nightly", "debug-render-3d"]
|
||||||
version = "0.20"
|
version = "0.20"
|
||||||
|
|
||||||
# Maybe also enable only a small amount of optimization for our code:
|
# 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)]
|
#[reflect(Resource)]
|
||||||
struct CatControllerSettings {
|
struct CatControllerSettings {
|
||||||
pub kp: f32,
|
pub kp: f32,
|
||||||
|
@ -36,6 +36,16 @@ struct CatControllerSettings {
|
||||||
pub kws: f32,
|
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>) {
|
fn zero_gravity(mut config: ResMut<RapierConfiguration>) {
|
||||||
config.gravity = Vec3::ZERO;
|
config.gravity = Vec3::ZERO;
|
||||||
}
|
}
|
||||||
|
|
122
src/bike.rs
122
src/bike.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::{shape::Capsule as Tire, *};
|
||||||
use bevy_rapier3d::{
|
use bevy_rapier3d::{
|
||||||
geometry::Group,
|
geometry::Group,
|
||||||
prelude::{
|
prelude::{
|
||||||
|
@ -14,6 +14,11 @@ use crate::planet::PLANET_RADIUS;
|
||||||
|
|
||||||
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 0.2;
|
pub(crate) const SPAWN_ALTITUDE: f32 = PLANET_RADIUS * 0.2;
|
||||||
|
|
||||||
|
type Meshterial<'a> = (
|
||||||
|
ResMut<'a, Assets<Mesh>>,
|
||||||
|
ResMut<'a, Assets<StandardMaterial>>,
|
||||||
|
);
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CyberBikeBody;
|
pub struct CyberBikeBody;
|
||||||
|
|
||||||
|
@ -23,6 +28,9 @@ pub struct CyberBikeCollider;
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct CyberBikeModel;
|
pub struct CyberBikeModel;
|
||||||
|
|
||||||
|
#[derive(Debug, Component)]
|
||||||
|
pub struct CyberWheel;
|
||||||
|
|
||||||
#[derive(Component, Debug, Default, Clone, Copy)]
|
#[derive(Component, Debug, Default, Clone, Copy)]
|
||||||
pub struct CyberBikeControl {
|
pub struct CyberBikeControl {
|
||||||
pub error_sum: f32,
|
pub error_sum: f32,
|
||||||
|
@ -39,6 +47,7 @@ pub struct WheelConfig {
|
||||||
pub limits: [f32; 2],
|
pub limits: [f32; 2],
|
||||||
pub stiffness: f32,
|
pub stiffness: f32,
|
||||||
pub damping: f32,
|
pub damping: f32,
|
||||||
|
pub radius: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for WheelConfig {
|
impl Default for WheelConfig {
|
||||||
|
@ -51,6 +60,7 @@ impl Default for WheelConfig {
|
||||||
limits: [0.1, 1.0],
|
limits: [0.1, 1.0],
|
||||||
stiffness: 10.0,
|
stiffness: 10.0,
|
||||||
damping: 0.7,
|
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_BODY_COLLISION_GROUP: (Group, Group) = (Group::GROUP_1, Group::GROUP_1);
|
||||||
const BIKE_WHEEL_COLLISION_GROUP: (Group, Group) = (Group::GROUP_10, Group::GROUP_10);
|
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);
|
let xform = Transform::from_translation(Vec3::Y * SPAWN_ALTITUDE);
|
||||||
//.with_rotation(Quat::from_axis_angle(Vec3::Z, -89.0f32.to_radians()));
|
//.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,
|
linear_damping: 0.1,
|
||||||
};
|
};
|
||||||
let not_sleeping = Sleeping::disabled();
|
let not_sleeping = Sleeping::disabled();
|
||||||
|
|
||||||
let ccd = Ccd { enabled: true };
|
let ccd = Ccd { enabled: true };
|
||||||
|
|
||||||
let bcollider_shape =
|
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 {
|
let friction = Friction {
|
||||||
coefficient: 0.0,
|
coefficient: 0.0,
|
||||||
|
@ -127,21 +141,78 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
.id();
|
.id();
|
||||||
|
|
||||||
//return;
|
//return;
|
||||||
let wheel_z_positions = vec![-1.0, 1.2, -1.0];
|
re_tire(&mut commands, &xform, bike, &wheel_conf, &mut meshterials);
|
||||||
let wheel_y = -1.0f32;
|
}
|
||||||
|
|
||||||
|
fn re_tire(
|
||||||
|
commands: &mut Commands,
|
||||||
|
xform: &Transform,
|
||||||
|
bike: Entity,
|
||||||
|
conf: &WheelConfig,
|
||||||
|
meshterials: &mut Meshterial,
|
||||||
|
) {
|
||||||
// re-set the collision group
|
// re-set the collision group
|
||||||
let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP;
|
let (membership, filter) = BIKE_WHEEL_COLLISION_GROUP;
|
||||||
let wheels_collision_group = CollisionGroups::new(membership, filter);
|
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 tire = Tire {
|
||||||
let (wheel_x, wheel_rad, stiffness) = match i {
|
radius: wheel_rad,
|
||||||
0 => (-1.1, 0.5, 2.0),
|
rings: 1,
|
||||||
2 => (1.1, 0.5, 2.0),
|
depth: 0.2,
|
||||||
1 => (0.0, 0.5, 1.8),
|
..Default::default()
|
||||||
_ => unreachable!(),
|
};
|
||||||
};
|
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);
|
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 trans = xform.translation + offset;
|
||||||
let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans);
|
let wheel_pos_in_world = Transform::from_rotation(xform.rotation).with_translation(trans);
|
||||||
let wheel_damping = Damping {
|
let wheel_damping = Damping {
|
||||||
|
@ -154,13 +225,27 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
let damping = 0.3;
|
let damping = 0.3;
|
||||||
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
||||||
.local_anchor2(offset)
|
.local_anchor2(offset)
|
||||||
.limits([-1.0, 0.9])
|
.limits(limits)
|
||||||
.motor_position(0.0, stiffness, damping);
|
.motor_position(0.0, stiffness, damping);
|
||||||
let joint = ImpulseJoint::new(bike, prismatic);
|
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)
|
.spawn(RigidBody::Dynamic)
|
||||||
.insert((wheel_pos_in_world, GlobalTransform::default()))
|
.insert(spatial_bundle)
|
||||||
.insert((
|
.insert((
|
||||||
wheel_collider,
|
wheel_collider,
|
||||||
mass_props,
|
mass_props,
|
||||||
|
@ -170,7 +255,10 @@ fn spawn_cyberbike(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
joint,
|
joint,
|
||||||
wheels_collision_group,
|
wheels_collision_group,
|
||||||
))
|
))
|
||||||
.id();
|
.with_children(|wheel| {
|
||||||
|
wheel.spawn(tire_spundle).insert(pbr_bundle.clone());
|
||||||
|
})
|
||||||
|
.insert(CyberWheel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
use bevy::{
|
use bevy::prelude::{shape::Plane, *};
|
||||||
prelude::{shape::Plane, *},
|
|
||||||
render::mesh::Indices,
|
|
||||||
};
|
|
||||||
use bevy_rapier3d::prelude::*;
|
use bevy_rapier3d::prelude::*;
|
||||||
use wgpu::PrimitiveTopology;
|
|
||||||
|
|
||||||
use crate::Label;
|
use crate::Label;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue