checkpoint, kinda fucked up
This commit is contained in:
parent
936e3591d1
commit
37ce29fd71
6 changed files with 112 additions and 66 deletions
|
@ -58,9 +58,9 @@ pub struct CatControllerSettings {
|
||||||
impl Default for CatControllerSettings {
|
impl Default for CatControllerSettings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
kp: 20.0,
|
kp: 40.0,
|
||||||
kd: 4.5,
|
kd: 5.0,
|
||||||
ki: 0.07,
|
ki: 0.1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ impl Default for CatControllerState {
|
||||||
pitch_prev: Default::default(),
|
pitch_prev: Default::default(),
|
||||||
decay_factor: 0.99,
|
decay_factor: 0.99,
|
||||||
roll_limit: 1.5,
|
roll_limit: 1.5,
|
||||||
pitch_limit: 1.0,
|
pitch_limit: 0.8,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,23 @@
|
||||||
|
use std::f32::consts::PI;
|
||||||
#[cfg(feature = "inspector")]
|
#[cfg(feature = "inspector")]
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use bevy::prelude::{Commands, Entity, Query, Res, ResMut, Time, Transform, Vec3, With, Without};
|
use bevy::prelude::{
|
||||||
use bevy_rapier3d::prelude::{
|
Commands, Entity, Quat, Query, Res, ResMut, Time, Transform, Vec3, With, Without,
|
||||||
CollisionGroups, ExternalForce, Friction, Group, QueryFilter, RapierConfiguration,
|
};
|
||||||
|
use bevy_rapier3d::{
|
||||||
|
prelude::{
|
||||||
|
CollisionGroups, ExternalForce, Group, MultibodyJoint, QueryFilter, RapierConfiguration,
|
||||||
RapierContext, ReadMassProperties, Velocity,
|
RapierContext, ReadMassProperties, Velocity,
|
||||||
|
},
|
||||||
|
rapier::prelude::JointAxis,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "inspector")]
|
#[cfg(feature = "inspector")]
|
||||||
use super::ActionDebugInstant;
|
use super::ActionDebugInstant;
|
||||||
use super::{CatControllerSettings, CatControllerState, MovementSettings, Tunneling};
|
use super::{CatControllerSettings, CatControllerState, MovementSettings, Tunneling};
|
||||||
use crate::{
|
use crate::{
|
||||||
bike::{CyberBikeBody, CyberWheel, BIKE_WHEEL_COLLISION_GROUP},
|
bike::{CyberBikeBody, CyberSteering, CyberWheel, BIKE_WHEEL_COLLISION_GROUP},
|
||||||
input::InputState,
|
input::InputState,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,7 +67,7 @@ pub(super) fn falling_cat(
|
||||||
let (derivative, integral) = control_vars.update_pitch(pitch_error, time.delta_seconds());
|
let (derivative, integral) = control_vars.update_pitch(pitch_error, time.delta_seconds());
|
||||||
let mag = (settings.kp * pitch_error) + (settings.ki * integral) + (settings.kd * derivative);
|
let mag = (settings.kp * pitch_error) + (settings.ki * integral) + (settings.kd * derivative);
|
||||||
if mag.is_finite() {
|
if mag.is_finite() {
|
||||||
forces.torque += wright * mag;
|
forces.torque += wright * mag * 0.25;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "inspector")]
|
#[cfg(feature = "inspector")]
|
||||||
|
@ -79,25 +85,32 @@ pub(super) fn falling_cat(
|
||||||
pub(super) fn input_forces(
|
pub(super) fn input_forces(
|
||||||
settings: Res<MovementSettings>,
|
settings: Res<MovementSettings>,
|
||||||
input: Res<InputState>,
|
input: Res<InputState>,
|
||||||
mut cquery: Query<&mut Friction, With<CyberWheel>>,
|
mut braking_query: Query<&mut MultibodyJoint, With<CyberWheel>>,
|
||||||
mut bquery: Query<(&Transform, &mut ExternalForce), With<CyberBikeBody>>,
|
mut body_query: Query<
|
||||||
|
(&Transform, &mut ExternalForce),
|
||||||
|
(With<CyberBikeBody>, Without<CyberSteering>),
|
||||||
|
>,
|
||||||
|
mut steering_query: Query<&mut Transform, With<CyberSteering>>,
|
||||||
) {
|
) {
|
||||||
let (xform, mut forces) = bquery.single_mut();
|
let (xform, mut forces) = body_query.single_mut();
|
||||||
|
|
||||||
// thrust
|
// thrust
|
||||||
let thrust = xform.forward() * input.throttle * settings.accel;
|
let thrust = xform.forward() * input.throttle * settings.accel;
|
||||||
forces.force += thrust;
|
let point = xform.translation + xform.back();
|
||||||
|
let force = ExternalForce::at_point(thrust, point, xform.translation);
|
||||||
|
*forces += force;
|
||||||
|
|
||||||
// brake
|
// brake
|
||||||
for mut friction in cquery.iter_mut() {
|
for mut motor in braking_query.iter_mut() {
|
||||||
friction.coefficient = if input.brake { 2.0 } else { 0.0 };
|
let factor = if input.brake { settings.accel } else { 0.0 };
|
||||||
|
motor.data.set_motor_velocity(JointAxis::X, 0.0, factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// steering
|
// steering
|
||||||
let force = xform.right() * input.yaw * settings.sensitivity;
|
let angle = input.yaw * (PI / 2.0);
|
||||||
let point = xform.translation + xform.forward();
|
let mut steering = steering_query.single_mut();
|
||||||
let force = ExternalForce::at_point(force, point, xform.translation);
|
steering.rotation = Quat::from_axis_angle(Vec3::Y, angle + 90.0f32.to_radians());
|
||||||
*forces += force;
|
//dbg!(steering.rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Don't let the wheels get stuck underneat the planet
|
/// Don't let the wheels get stuck underneat the planet
|
||||||
|
@ -153,7 +166,7 @@ pub(super) fn tunnel_out(
|
||||||
}
|
}
|
||||||
tunneling.frames -= 1;
|
tunneling.frames -= 1;
|
||||||
force.force = tunneling.dir * settings.gravity * 1.5 * mprops.0.mass;
|
force.force = tunneling.dir * settings.gravity * 1.5 * mprops.0.mass;
|
||||||
#[cfg(feature = "inspector")]
|
//#[cfg(feature = "inspector")]
|
||||||
dbg!(&tunneling);
|
dbg!(&tunneling);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,5 +82,5 @@ pub(super) fn spawn_cyberbike(
|
||||||
.insert(CatControllerState::default())
|
.insert(CatControllerState::default())
|
||||||
.id();
|
.id();
|
||||||
|
|
||||||
spawn_tires(&mut commands, &xform, bike, &wheel_conf, &mut meshterials);
|
spawn_tires(&mut commands, bike, xform, &wheel_conf, &mut meshterials);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,9 @@ use bevy::{
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct CyberBikeBody;
|
pub struct CyberBikeBody;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct CyberSteering;
|
||||||
|
|
||||||
#[derive(Debug, Component)]
|
#[derive(Debug, Component)]
|
||||||
pub struct CyberWheel;
|
pub struct CyberWheel;
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
use bevy::prelude::{shape::UVSphere as Tire, *};
|
use bevy::prelude::{shape::UVSphere as Tire, *};
|
||||||
use bevy_rapier3d::prelude::{
|
use bevy_rapier3d::prelude::{
|
||||||
Ccd, Collider, ColliderMassProperties, CollisionGroups, Damping, ExternalForce, Friction,
|
Ccd, CoefficientCombineRule, Collider, ColliderMassProperties, CollisionGroups, Damping,
|
||||||
MultibodyJoint, PrismaticJointBuilder, Restitution, RigidBody, Sleeping,
|
ExternalForce, Friction, MultibodyJoint, PrismaticJointBuilder, Restitution,
|
||||||
TransformInterpolation,
|
RevoluteJointBuilder, RigidBody, Sleeping, TransformInterpolation,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
|
use super::{CyberSteering, CyberWheel, Meshterial, WheelConfig, BIKE_WHEEL_COLLISION_GROUP};
|
||||||
|
|
||||||
pub fn spawn_tires(
|
pub fn spawn_tires(
|
||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
xform: &Transform,
|
|
||||||
bike: Entity,
|
bike: Entity,
|
||||||
|
xform: Transform,
|
||||||
conf: &WheelConfig,
|
conf: &WheelConfig,
|
||||||
meshterials: &mut Meshterial,
|
meshterials: &mut Meshterial,
|
||||||
) {
|
) {
|
||||||
|
@ -47,8 +47,13 @@ pub fn spawn_tires(
|
||||||
};
|
};
|
||||||
|
|
||||||
let friction = Friction {
|
let friction = Friction {
|
||||||
coefficient: 0.0,
|
coefficient: 1.0,
|
||||||
..Default::default()
|
combine_rule: CoefficientCombineRule::Min,
|
||||||
|
};
|
||||||
|
|
||||||
|
let restitution = Restitution {
|
||||||
|
coefficient: 0.8,
|
||||||
|
combine_rule: CoefficientCombineRule::Average,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut wheel_poses = Vec::with_capacity(2);
|
let mut wheel_poses = Vec::with_capacity(2);
|
||||||
|
@ -58,7 +63,7 @@ pub fn spawn_tires(
|
||||||
let wheel_x = 0.0;
|
let wheel_x = 0.0;
|
||||||
let wheel_z = -conf.front_forward;
|
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);
|
wheel_poses.push((offset, Some(CyberSteering)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// rear
|
// rear
|
||||||
|
@ -66,12 +71,10 @@ pub fn spawn_tires(
|
||||||
let wheel_x = 0.0;
|
let wheel_x = 0.0;
|
||||||
let wheel_z = conf.rear_back;
|
let wheel_z = conf.rear_back;
|
||||||
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
|
let offset = Vec3::new(wheel_x, wheel_y, wheel_z);
|
||||||
wheel_poses.push(offset);
|
wheel_poses.push((offset, None));
|
||||||
}
|
}
|
||||||
|
|
||||||
for offset in wheel_poses {
|
for (offset, steering) 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 {
|
let wheel_damping = Damping {
|
||||||
linear_damping: 0.8,
|
linear_damping: 0.8,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
@ -83,42 +86,69 @@ pub fn spawn_tires(
|
||||||
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
let prismatic = PrismaticJointBuilder::new(Vec3::Y)
|
||||||
.local_anchor1(offset)
|
.local_anchor1(offset)
|
||||||
.limits(limits)
|
.limits(limits)
|
||||||
.motor_position(-0.1, stiffness, damping);
|
.motor_position(limits[0], stiffness, damping);
|
||||||
let joint = MultibodyJoint::new(bike, prismatic);
|
|
||||||
|
|
||||||
let spatial_bundle = SpatialBundle {
|
let fork = commands
|
||||||
transform: wheel_pos_in_world,
|
.spawn(SpatialBundle {
|
||||||
|
transform: Transform::from_translation(offset),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
})
|
||||||
|
.set_parent(bike)
|
||||||
|
.id();
|
||||||
|
|
||||||
let tire_spundle = SpatialBundle {
|
let suspension = MultibodyJoint::new(fork, prismatic);
|
||||||
transform: Transform::IDENTITY,
|
let sentity = commands
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
commands
|
|
||||||
.spawn(RigidBody::Dynamic)
|
.spawn(RigidBody::Dynamic)
|
||||||
.insert(spatial_bundle)
|
.insert(suspension)
|
||||||
.insert((
|
.insert(SpatialBundle {
|
||||||
|
transform: Transform::from_translation(offset),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.set_parent(bike)
|
||||||
|
.id();
|
||||||
|
|
||||||
|
if let Some(steering) = steering {
|
||||||
|
commands.entity(sentity).insert(steering);
|
||||||
|
}
|
||||||
|
|
||||||
|
let revolute = RevoluteJointBuilder::new(Vec3::X);
|
||||||
|
let axel = MultibodyJoint::new(sentity, revolute);
|
||||||
|
let wheel_bundle = (
|
||||||
wheel_collider,
|
wheel_collider,
|
||||||
mass_props,
|
mass_props,
|
||||||
wheel_damping,
|
wheel_damping,
|
||||||
ccd,
|
ccd,
|
||||||
not_sleeping,
|
|
||||||
joint,
|
|
||||||
wheels_collision_group,
|
wheels_collision_group,
|
||||||
friction,
|
friction,
|
||||||
|
restitution,
|
||||||
CyberWheel,
|
CyberWheel,
|
||||||
ExternalForce::default(),
|
ExternalForce::default(),
|
||||||
Restitution::new(0.2),
|
not_sleeping,
|
||||||
))
|
axel,
|
||||||
|
);
|
||||||
|
|
||||||
|
let _wentity = commands
|
||||||
|
.spawn(RigidBody::Dynamic)
|
||||||
|
.insert(SpatialBundle {
|
||||||
|
// transform: Transform::from_rotation(xform.rotation)
|
||||||
|
// .with_translation(xform.translation)
|
||||||
|
transform: Transform::from_rotation(Quat::from_axis_angle(
|
||||||
|
Vec3::Z,
|
||||||
|
90.0f32.to_radians(),
|
||||||
|
)),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.insert(wheel_bundle)
|
||||||
.with_children(|wheel| {
|
.with_children(|wheel| {
|
||||||
wheel.spawn(tire_spundle).insert(pbr_bundle.clone()).insert(
|
wheel
|
||||||
TransformInterpolation {
|
.spawn(SpatialBundle::default())
|
||||||
|
.insert(pbr_bundle.clone())
|
||||||
|
.insert(TransformInterpolation {
|
||||||
start: None,
|
start: None,
|
||||||
end: None,
|
end: None,
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.set_parent(sentity)
|
||||||
|
.id();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ fn spawn_planet(
|
||||||
let pcollide = (
|
let pcollide = (
|
||||||
shape,
|
shape,
|
||||||
Friction {
|
Friction {
|
||||||
coefficient: 0.05,
|
coefficient: 0.7,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Restitution::new(0.0),
|
Restitution::new(0.0),
|
||||||
|
|
Loading…
Reference in a new issue